欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2016年11月19日 10:21:37   投稿:jingxian  
下面小編就為大家?guī)?lái)一篇利用HttpUrlConnection 上傳 接收文件的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

如下所示:

//客戶端代碼

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請(qǐng)求必須設(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 上傳 接收文件的實(shí)現(xiàn)方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • springboot 返回json格式數(shù)據(jù)時(shí)間格式配置方式

    springboot 返回json格式數(shù)據(jù)時(shí)間格式配置方式

    這篇文章主要介紹了springboot 返回json格式數(shù)據(jù)時(shí)間格式配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Maven之pom.xml文件中的Build配置解析

    Maven之pom.xml文件中的Build配置解析

    這篇文章主要介紹了Maven之pom.xml文件中的Build配置解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • java開(kāi)發(fā)微信分享到朋友圈功能

    java開(kāi)發(fā)微信分享到朋友圈功能

    這篇文章主要為大家詳細(xì)介紹了java開(kāi)發(fā)微信發(fā)送給朋友和分享到朋友圈功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-07-07
  • Mybatis游標(biāo)查詢大量數(shù)據(jù)方式

    Mybatis游標(biāo)查詢大量數(shù)據(jù)方式

    這篇文章主要介紹了Mybatis游標(biāo)查詢大量數(shù)據(jù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • Spring Boot JPA中java 8 的應(yīng)用實(shí)例

    Spring Boot JPA中java 8 的應(yīng)用實(shí)例

    這篇文章主要介紹了Spring Boot JPA中java 8 的應(yīng)用實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • Springmvc的運(yùn)行流程圖文詳解

    Springmvc的運(yùn)行流程圖文詳解

    今天小編就為大家分享一篇關(guān)于Springmvc的運(yùn)行流程圖文詳解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-01-01
  • Java圖片批量壓縮像素的實(shí)現(xiàn)方法

    Java圖片批量壓縮像素的實(shí)現(xiàn)方法

    我們開(kāi)發(fā)中經(jīng)常會(huì)遇到原圖清晰度高,考慮到效率問(wèn)題,我們不可能拿原圖進(jìn)行顯示,服務(wù)端一般都要對(duì)圖片進(jìn)行壓縮處理,然后發(fā)送給客戶端顯示,這篇文章主要給大家介紹了關(guān)于Java圖片批量壓縮像素的實(shí)現(xiàn)方法,需要的朋友可以參考下
    2021-12-12
  • 淺談spring DI 依賴注入方式和區(qū)別

    淺談spring DI 依賴注入方式和區(qū)別

    Spring框架對(duì)Java開(kāi)發(fā)的重要性不言而喻,本文主要介紹了spring DI 依賴注入方式和區(qū)別,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • Java String的intern用法解析

    Java String的intern用法解析

    這篇文章主要介紹了Java String的intern用法解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • 基于logback實(shí)現(xiàn)純java版本的SDK組件

    基于logback實(shí)現(xiàn)純java版本的SDK組件

    這篇文章主要介紹了基于logback實(shí)現(xiàn)純java版本的SDK組件,在項(xiàng)目開(kāi)發(fā)過(guò)程中通常會(huì)使用logback作為日志記錄的依賴工具,使用方式是引入logback相關(guān)jar包,然后配置logback.xml配置文件的方式來(lái)實(shí)現(xiàn),需要的朋友可以參考下
    2023-11-11

最新評(píng)論