如下所示:

//客户端代码

public static void main(String[] args) throws IOException {
 DataInputStream in = null;
 OutputStream out = null;
 HttpURLConnection conn = null;
 JSONObject resposeTxt = null;
 InputStream ins = null;
 ByteArrayOutputStream outStream = null;
 try {
  URL url = new URL("http://10.28.160.160:9080/main/uploadFile?fileName=列表.txt");
  conn = (HttpURLConnection) url.openConnection();
  // 发送POST请求必须设置如下两行
  conn.setDoOutput(true);
  conn.setUseCaches(false);
  conn.setRequestMethod("POST");
  conn.setRequestProperty("Content-Type", "text/html");
  conn.setRequestProperty("Cache-Control", "no-cache");
  conn.setRequestProperty("Charsert", "UTF-8");
  conn.connect();
  conn.setConnectTimeout(10000);
  out = conn.getOutputStream();

  File file = new File("H:/Users/chengtingyu/Desktop/test/list.txt");
  in = new DataInputStream(new FileInputStream(file));

  int bytes = 0;
  byte[] buffer = new byte[1024];
  while ((bytes = in.read(buffer)) != -1) {
  out.write(buffer, 0, bytes);
  }
  out.flush();

  // 返回流
  if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
  ins = conn.getInputStream();
  outStream = new ByteArrayOutputStream();
  byte[] data = new byte[1024];
  int count = -1;
  while ((count = ins.read(data, 0, 1024)) != -1) {
   outStream.write(data, 0, count);
  }
  data = null;
  resposeTxt = JSONObject.parseObject(new String(outStream
   .toByteArray(), "UTF-8"));
  }
 } catch (Exception e) {
  e.printStackTrace();
 } finally {
  if (in != null) {
  in.close();
  }
  if (out != null) {
  out.close();
  }
  if (ins != null) {
  ins.close();
  }
  if (outStream != null) {
  outStream.close();
  }
  if (conn != null) {
  conn.disconnect();
  }
 }
 }

 

//服务端代码

 

public String uploadFile() throws Exception{
    String fileName = request.getParameter("fileName");  
    String fileFullPath = "H:/Users/chengtingyu/Desktop/" + fileName;
    InputStream input = null;
    FileOutputStream fos = null;
 try {
  input = request.getInputStream();
  File file = new File("H:/Users/chengtingyu/Desktop");  
     if(!file.exists()){  
       file.mkdirs();  
     }  
     fos = new FileOutputStream(fileFullPath);  
     int size = 0;  
     byte[] buffer = new byte[1024];  
     while ((size = input.read(buffer,0,1024)) != -1) {  
       fos.write(buffer, 0, size);  
     }
     
     //响应信息 json字符串格式
     Map<String,Object> responseMap = new HashMap<String,Object>();
     responseMap.put("flag", true);
     
     //生成响应的json字符串
      String jsonResponse = JSONObject.toJSONString(responseMap);
     sendResponse(jsonResponse);
 } catch (IOException e) {
  //响应信息 json字符串格式
     Map<String,Object> responseMap = new HashMap<String,Object>();
     responseMap.put("flag", false);
     responseMap.put("errorMsg", e.getMessage());
     String jsonResponse = JSONObject.toJSONString(responseMap);
     sendResponse(jsonResponse);
 } finally{
  if(input != null){
  input.close();
  }
  if(fos != null){
  fos.close();
  }
 }  
      
    return null;
 }
 
 
 
 /**
   * 返回响应
   * 
   * @throws Exception
   */
  private void sendResponse(String responseString) throws Exception {
   response.setContentType("application/json;charset=UTF-8");
    PrintWriter pw = null;
    try {
      pw = response.getWriter();
      pw.write(responseString);
      pw.flush();
    } finally {
      IOUtils.closeQuietly(pw);
    }
  }

以上这篇利用HttpUrlConnection 上传 接收文件的实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
  • 谈谈Java利用原始HttpURLConnection发送POST数据
  • java后台调用HttpURLConnection类模拟浏览器请求实例(可用于接口调用)
  • Android HttpURLConnection.getResponseCode()错误解决方法
  • Java HttpURLConnection超时和IO异常处理
  • Android 中HttpURLConnection与HttpClient使用的简单实例
  • Android中HttpURLConnection与HttpClient的使用与封装
  • Android中使用HttpURLConnection实现GET POST JSON数据与下载图片
  • Android程序开发通过HttpURLConnection上传文件到服务器
  • Android通过HttpURLConnection和HttpClient接口实现网络编程
  • Android网络技术HttpURLConnection详解

转载请注明出处:http://www.dannyxu.com/article/20230526/33560.html

随机推荐

  1. 利用Python库Scapy解析pcap文件的方法

    每次写博客都是源于纳闷,python解析pcap这么常用的例子网上竟然没有,全是一堆命令行执行的python,能用吗?玩呢? pip安装scapy,然后解析pcap: import scapy from scapy.all impor...

  2. 利用计划任务和VBS脚本实现自动WEB共享文件夹里的文件

    复制代码 代码如下:Option Explicit On Error Resume Next '生成列表的文件类型 Const sListFileType = "wmv,rm,wma" '文件所在的相对路径 Const sShowPath...

  3. 利用HttpUrlConnection 上传 接收文件的实现方法

    如下所示: //客户端代码 public static void main(String[] args) throws IOException { DataInputStream in = null; OutputStream ...

  4. 利用Python实现在同一网络中的本地文件共享方法

    本文利用Python3启动简单的HTTP服务器,以实现在同一网络中共享本地文件。 启动HTTP服务器 打开终端,转入目标文件所在文件夹,键入以下命令: $ cd /Users/zero/Documents/localFiles # p...

  5. 利用swift实现卡片横向滑动动画效果的方法示例

    本文主要给大家介绍了关于利用swift实现卡片横向滑动动画效果的相关资料,分享出来供大家参考学习,下面来一起看看详细的介绍吧。 根据惯例,首先上效果图: 那天去面试,面试官突然拿出手机点开了一个app,自个在那点了一会,然后问我 这个...

  6. 利用PHPExcel实现Excel文件的写入和读取

    作为一个原本的Java党,用过PHP才知道,原来对于Excel文件的写入和读取原来可以这么简单! 利用PHP实现对于Excel的读取,主要借助于PHPExcel插件来完成。 PHPExcel下载地址:PHPExcel下载 一、PHPExc...

  7. 利用rapidjson实现解析嵌套的json的方法示例

    利用rapidjson解析嵌套的json 看json串1:{"system":{"version":"v2.6.1", "name":"value"}} 废话少说, 直接撸代码: #include iostream #include ...

  8. 利用.net core实现反向代理中间件的方法

    最近在将一些项目的rest api迁移到.net core中,最开始是用的Nginx做反向代理,将已经完成切换的部分切入系统,如下图所示: 由于迁移过程中也在进行代码重构,需要经常比较频繁的测试,以保证能及时发现引入的问题。从而导致我们...

  9. 利用Python+阿里云实现DDNS动态域名解析的方法

    引子 我想大家应该都很熟悉DNS了,这回在DNS前面加了一个D又变成了什么呢?这个D就是Dynamic(动态),也就是说,按照传统,一个域名所对应的IP地址应该是定死的,而使用了DDNS后,域名所对应的IP是可以动态变化的。那这个有什么用...

  10. 利用laravel+ajax实现文件上传功能方法示例

    前言 大家都知道,早期的XMLHttpRequest不支持文件上传,一般用第三方js插件或者flash,现在可以借助XMLHttpRequest Level 2 的FormData对象实现二进制文件上传,正好最近工作中遇到了这个需求,所以...