httpclient 實現檔案上傳中轉

來源:互聯網
上載者:User

標籤:eterm   ntb   前端   iter   share   key   coding   nbsp   with   

開發功能: 
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();// 會擷取到null 指標            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

httpclient 實現檔案上傳中轉

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.