最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

利用HttpUrlConnection 上傳 接收文件的實現(xiàn)方法

 更新時間:2016年11月19日 10:21:37   投稿:jingxian  
下面小編就為大家?guī)硪黄肏ttpUrlConnection 上傳 接收文件的實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

如下所示:

//客戶端代碼

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();
  // 發(fā)送POST請求必須設(shè)置如下兩行
  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();
  }
 }
 }

 

//服務(wù)端代碼

 

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);  
     }
     
     //響應(yīng)信息 json字符串格式
     Map<String,Object> responseMap = new HashMap<String,Object>();
     responseMap.put("flag", true);
     
     //生成響應(yīng)的json字符串
      String jsonResponse = JSONObject.toJSONString(responseMap);
     sendResponse(jsonResponse);
 } catch (IOException e) {
  //響應(yīng)信息 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;
 }
 
 
 
 /**
   * 返回響應(yīng)
   * 
   * @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 上傳 接收文件的實現(xiàn)方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • mybatis插入與批量插入返回ID的原理詳解

    mybatis插入與批量插入返回ID的原理詳解

    這篇文章主要給大家介紹了關(guān)于mybatis插入與批量插入返回ID的原理的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用mybatis具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • SpringSecurity表單配置之登錄成功及頁面跳轉(zhuǎn)原理解析

    SpringSecurity表單配置之登錄成功及頁面跳轉(zhuǎn)原理解析

    這篇文章主要介紹了SpringSecurity表單配置之登錄成功及頁面跳轉(zhuǎn)原理解析,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2023-12-12
  • Spring源碼解析 Bean屬性填充

    Spring源碼解析 Bean屬性填充

    這篇文章主要介紹了Spring源碼解析 Bean屬性填充,文章圍繞主題展開想詳細(xì)的內(nèi)容詳情,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-07-07
  • java Date裝成英文String后,無法再轉(zhuǎn)回Date的解決方案

    java Date裝成英文String后,無法再轉(zhuǎn)回Date的解決方案

    本文介紹了java Date裝成英文String后,無法再轉(zhuǎn)回Date的解決方案。具有一定的參考價值,下面跟著小編一起來看下吧
    2017-01-01
  • Java底層基于二叉搜索樹實現(xiàn)集合和映射/集合Set功能詳解

    Java底層基于二叉搜索樹實現(xiàn)集合和映射/集合Set功能詳解

    這篇文章主要介紹了Java底層基于二叉搜索樹實現(xiàn)集合和映射/集合Set功能,結(jié)合實例形式分析了Java使用二叉搜索樹實現(xiàn)集合和映射相關(guān)操作技巧,需要的朋友可以參考下
    2020-03-03
  • IDEA 去除 mybatis.xml 文件黃色警告的圖文教程

    IDEA 去除 mybatis.xml 文件黃色警告的圖文教程

    這篇文章主要介紹了IDEA 去除 mybatis.xml 文件黃色警告的方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-07-07
  • java微信公眾號支付開發(fā)之現(xiàn)金紅包

    java微信公眾號支付開發(fā)之現(xiàn)金紅包

    這篇文章主要為大家詳細(xì)介紹了java微信公眾號支付開發(fā)之現(xiàn)金紅包,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • spring security在分布式項目下的配置方法(案例詳解)

    spring security在分布式項目下的配置方法(案例詳解)

    這篇文章主要介紹了spring security在分布式項目下的配置方法,本文通過一個項目案例給大家詳細(xì)介紹,通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-10-10
  • Java ArrayList如何實現(xiàn)生成不重復(fù)隨機數(shù)

    Java ArrayList如何實現(xiàn)生成不重復(fù)隨機數(shù)

    這篇文章主要介紹了Java ArrayList如何實現(xiàn)生成不重復(fù)隨機數(shù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-09-09
  • 基于java Files類和Paths類的用法(詳解)

    基于java Files類和Paths類的用法(詳解)

    下面小編就為大家分享一篇基于java Files類和Paths類的用法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-11-11

最新評論

轮台县| 游戏| 日照市| 静海县| 涞源县| 永新县| 郑州市| 公主岭市| 巴彦淖尔市| 增城市| 宜君县| 建始县| 韶关市| 镇原县| 吉安市| 大兴区| 中江县| 桐庐县| 泽普县| 泸水县| 湖南省| 宝清县| 普定县| 平湖市| 阳山县| 城市| 宜良县| 遂昌县| 房产| 延津县| 昂仁县| 保德县| 绥芬河市| 昆明市| 垣曲县| 金寨县| 芦山县| 凤山市| 安国市| 哈尔滨市| 昭觉县|