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

时间: 2024-07-28 13:48:32

httpclient 实现文件上传中转的相关文章

Android使用HttpClient实现文件上传到PHP服务器,并监控进度条

上传 服务器端PHP 代码如下 : <?php $target_path = "./tmp/";//接收文件目录 $target_path = $target_path.($_FILES['file']['name']); $target_path = iconv("UTF-8","gb2312", $target_path); if(move_uploaded_file($_FILES['file']['tmp_name'], $targ

Android开发之网络请求通信专题(二):基于HttpClient的文件上传下载

上一篇专题Android开发之网络请求通信专题(一):基于HttpURLConnection的请求通信我们讲解了如何使用httpurlconnection来实现基本的文本数据传输.一般在实际开发中我们可以用于传输xml或者json格式的数据.今天我们来讲解另外一种http网络请求的方式:httpclient,并实现文件的上传和下载. 在这里插个题外话,其实这些网络请求有很多第三方jar包可以使用,这些包都封装得很好了.如果只是想使用,我们就直接拿别人得jar包来用就好.博主这里推荐一个叫xuti

HttpClient构造文件上传

在项目中我们有时候需要使用到其他第三方的api,而有些api要求我们上传文件,search一下,下面将结果记录一下喽! 含义 ENCTYPE="multipart/form-data" 说明: 通过 http 协议上传文件 rfc1867协议概述,jsp 应用举例,客户端发送内容构造 1.概述在最初的 http 协议中,没有上传文件方面的功能. rfc1867 (http://www.ietf.org/rfc/rfc1867.txt) 为 http 协议添加了这个功能.客户端的浏览器,

HttpClient多文件上传

这辈子没办法做太多事情,所以每一件都要做到精彩绝伦! People can't do too many things in my life,so everything will be wonderful 项目使用技术SpringMVC + spring + mybatis HttpClient技术 1       HttpClientService工具类 该工具类封装了get.post.put.delete以及post上传多个文件等方法:包含了有无参数.日常开发,够用了! 一般来说,有些公司会有

使用 HttpClient 进行文件上传

1.使用 AddPart 方法 public static void upload(String authorization,String baseUrl,String filePath,String userId,String isOverWrite,String remotePath){ CloseableHttpClient client = HttpClients.createDefault(); String uploadFile_url = Utils.getValue("uploa

Android网络编程之使用HttpClient批量上传文件(二)AsyncTask+HttpClient并实现上传进度监听

请尊重他人的劳动成果,转载请注明出处: Android网络编程之使用HttpClient批量上传文件(二)AsyncTask+HttpClient并实现上传进度监听 运行效果图: 我曾在<Android网络编程之使用HttpClient批量上传文件>一文中介绍过如何通过HttpClient实现多文件上传和服务器的接收.在上一篇主要使用Handler+HttpClient的方式实现文件上传.这一篇将介绍使用AsyncTask+HttpClient实现文件上传并监听上传进度. 监控进度实现: 首先

Android开发之httpclient文件上传实现

文件上传可能是一个比較耗时的操作,假设为上传操作带上进度提示则能够更好的提高用户体验,最后效果例如以下图: 项目源代码:http://download.csdn.net/detail/shinay/4965230 这里仅仅贴出代码,可依据实际情况自行改动. [java] view plaincopy package com.lxb.uploadwithprogress.http; import java.io.File; import org.apache.http.HttpResponse;

转 使用 HttpClient 4 进行文件上传

http://www.tuicool.com/articles/Y7reYb 1. 概述 本教程我们将描述如何使用 HttpClient 4进行一次多文件上传操作 . 我们将使用  http://echo.200please.com  作为测试服务器,因为它是面向公众的,并且接受大多数类型的内容. 如果你想要深入学习并了解你可以使用  HttpClient 做到的其它很棒的事情  – 那就去看看 首要的 HttpClient 教程吧   . 2. 使用  AddPart  方法 让我们开始研究研

springMVC + hadoop + httpclient 文件上传请求直接写入hdfs

springMVC + hadoop + httpclient 文件上传请求直接写入hdfs