httpclient 实现文件上传中转

摘要:
=null)contentType=请求。getContentType();//将获得空指针Maptmp=newHashMap;如果{MultipartHttpServletRequestmultipartRequest=WebUtils.getNativeRequest;MultipartFilefile=multipartRequest.getFile;returnhttpClientUpload;}}returnnull;}123456789101112131415161718123456789101112131415161718 publicStringhttpClientUploadthrowsClientProtocolException,IOException{HttpClienthttpclient=newDefaultHttpClient();//请求处理页面HttpPosthttppost=newHttpPost;//创建要处理的文件StringfileName=file.getOriginalFilename();ContentBodyfiles=newByteArrayBody;//使用MultipartEntityreqEntity=newMultipartEntity();reqEntity.addPart;if(params!=null)]values=params.get;for{Stringvalue=values[i];尝试{value=URLEncoder.encode;reqEntity.addPart;}catch{//TODOAuto-generatedcatchblocke.printStackTrace();}}}}//设置请求httppost。setEntity//执行HttpResponseresponse=httpclient。处决如果{HttpEntityentity=response.getEntity();returnEntityUtils.toString;}returnnull;}1234567891011121314151617181920212223242526272829303132333435363712345678910111213141516171819202122232425262728293031323334353637 http://bbs.bxzc123.com/forum.php?

开发功能: 
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
httpclient 实现文件上传中转第1张
  • 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 实现文件上传中转》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Java从入门到实战之(26)Java网络实例之练习docker安装7.4.2版本kibana与es下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

c# 常用 Common

/// <summary> /// md5加密字符串 /// </summary> /// <param name="message"></param> /// <returns></returns>...

java微信二次第三方开发,token认证,消息事件接收,图文素材库,自定义菜单等功能

基于之前的文章SSM配置的项目:http://www.cnblogs.com/mangyang/p/5168291.html 来进行微信第三方开发, 微信二次开发,官方还是网上有很多介绍了,这里就不在进行讲述了 直接上干货。 首先 与微信对接,服务器配置,需要80端口和443端口开放的服务器,这里推荐 使用 python 的pagekite,一款反向代理...

(转)string中的format

JDK1.5中,String类新增了一个很有用的静态方法String.format():format(Locale l, String format, Object... args) 使用指定的语言环境、格式字符串和参数返回一个格式化字符串。format(String format, Object... args) 使用指定的格式字符串和参数返回一个格式...

Java中判断String不为空的问题性能比较

 function 1: 最多人使用的一个方法, 直观, 方便, 但效率很低. function 2: 比较字符串长度, 效率高, 是我知道的最好一个方法. function 3: Java SE 6.0 才开始提供的方法, 效率和方法二几乎相等, 但出于兼容性考虑, 不推荐使用    以下是三种方法在机器上的运行结果: (机器性能不一, 仅供参考) fu...

Cookie和Session详解

Session和Cookie详解 会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话。常用的会话跟踪技术是Cookie与Session。 Cookie通过在客户端记录信息确定用户身份,Session通过在服务器端记录信息确定用户身份。 1.1  Cookie机制   在程序中,会话跟踪是很重要的事情。理论上,一个用户的所有请求操作都...

Spring cloud 超时及重试配置【ribbon及其它http client】

开启重试在某些情况下是有问题的,比如当压力过大,一个实例停止响应时,路由将流量转到另一个实例,很有可能导致最终所有的实例全被压垮。说到底,断路器的其中一个作用就是防止故障或者压力扩散。用了retry,断路器就只有在该服务的所有实例都无法运作的情况下才能起作用。这种时候,断路器的形式更像是提供一种友好的错误信息,或者假装服务正常运行的假象给使用者。 不用re...