国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

利用HttpUrlConnection 上傳 接收文件的實現方法

2019-11-26 13:32:39
字體:
來源:轉載
供稿:網友

如下所示:

//客戶端代碼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 上傳 接收文件的實現方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新沂市| 河间市| 宁武县| 新野县| 五莲县| 景谷| 常德市| 二连浩特市| 铜梁县| 金坛市| 昭平县| 娱乐| 滦平县| 萨嘎县| 凤庆县| 湄潭县| 兴文县| 渭源县| 大理市| 岳池县| 聊城市| 玛曲县| 靖江市| 富蕴县| 萨迦县| 涪陵区| 鄂托克旗| 会宁县| 溆浦县| 阿勒泰市| 郎溪县| 永川市| 封丘县| 安徽省| 乌海市| 安宁市| 吉林省| 临汾市| 定陶县| 通江县| 宝鸡市|