日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

httpclient 实现文件上传中转

發布時間:2023/12/9 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 httpclient 实现文件上传中转 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

開發功能:?
web前端提交上傳文件 —> a服務器接收 —> 轉發到b服務器進行文件處理?
下面是簡單實現的代碼,具體細節優化根本自己的需求更改。

public String handleResponse(HttpServletRequest request, HttpServletResponse response)throws UnsupportedEncodingException, IOException {String method = request.getMethod();String url = "b服務器的api url";if (method.equals("POST")) { String contentType = "application/json; charset=UTF-8"; if (request.getContentType() != null) contentType = request.getContentType();// 會獲取到空指針 Map<String, String[]> tmp = new HashMap(request.getParameterMap()); if (contentType.toLowerCase().startsWith("multipart/")) { MultipartHttpServletRequest multipartRequest = WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class); MultipartFile file = multipartRequest.getFile("file"); return httpClientUpload(url, file, tmp); } } return null; }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
public String httpClientUpload(String url, MultipartFile file, Map<String, String[]> params)throws ClientProtocolException, IOException {HttpClient httpclient = new DefaultHttpClient();// 請求處理頁面 HttpPost httppost = new HttpPost(url); // 創建待處理的文件 String fileName = file.getOriginalFilename(); ContentBody files = new ByteArrayBody(file.getBytes(), fileName); // 對請求的表單域進行填充 MultipartEntity reqEntity = new MultipartEntity(); reqEntity.addPart("file", files); if (params != null) {//這里草草處理values[] for (String key : params.keySet()) { String[] values = params.get(key); for (int i = 0; i < values.length; i++) { String value = values[i]; try { value = URLEncoder.encode(value, "UTF-8"); reqEntity.addPart(key, new StringBody(value)); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } // 設置請求 httppost.setEntity(reqEntity); // 執行 HttpResponse response = httpclient.execute(httppost); if (HttpStatus.SC_OK == response.getStatusLine().getStatusCode()) { HttpEntity entity = response.getEntity(); return EntityUtils.toString(entity, Charset.forName("UTF-8")); } return null; }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

http://bbs.bxzc123.com/forum.php?mod=viewthread&tid=244545
http://bbs.bxzc123.com/forum.php?mod=viewthread&tid=244541
http://bbs.bxzc123.com/forum.php?mod=viewthread&tid=244538
http://bbs.bxzc123.com/forum.php?mod=viewthread&tid=244527
http://bbs.bxzc123.com/forum.php?mod=viewthread&tid=244528
http://bbs.bxzc123.com/forum.php?mod=viewthread&tid=244529
http://bbs.bxzc123.com/forum.php?mod=viewthread&tid=244530

轉載于:https://www.cnblogs.com/sy646et/p/7266017.html

總結

以上是生活随笔為你收集整理的httpclient 实现文件上传中转的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。