zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    [LEETCODE] 初级算法/数组 1.1删除排序数组中的重复项
    [LeetCode]1.Two Sum 两数之和&&第一次刷题感想
    Panda的学习之路(3)——pandas 设置特定的值&处理没有数据的部分
    Panda的学习之路(2)——pandas选择数据
    Panda的学习之路(1)——series 和 Dataframe
    NUMPY的学习之路(2)——索引,合并,分割,赋值
    numpy的学习之路(1)——创建数组以及基本运算
    SpringBoot外部配置夹加载顺序
    SpringBoot2.0官方文档的位置
    @RestController注解
  • 原文地址:https://www.cnblogs.com/sy646et/p/7266017.html
Copyright © 2011-2022 走看看