使用HttpClient发送InputStream对象及发送文件
来源:互联网 发布:mac修容粉怎么用 编辑:程序博客网 时间:2024/05/23 21:49
最近在做的东西中有这样一个需求要把一个文件上传到服务器A上,再由服务器A上传到服务器B上,而服务器A上传到服务器B的这个请求是通过HttpClient发送的。如果是发送文件的话很好办,但是现在问题的难点是服务器A通过HttpClient发送的不是一个文件,而是一个InputStream对象。一个折中的办法是先把文件临时存储在服务器A上,然后再从服务器A上读取文件发送到服务器B上,那么有没有办法直接发送InputStream对象呢?答案是肯定的,我在这里找到了答案:http://www.baeldung.com/httpclient-multipart-upload。
接着自己写了一个例子测试了一下果断拿来用了。现在把这个小例子写一下,希望能帮到遇到同样问题的同学。
准备工作
我们需要引入这两个Maven依赖:
<dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.3.6</version></dependency><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpmime</artifactId><version>4.3.6</version></dependency>另外我们还需要写一个接收文件上传的处理类。这里我们用上一篇文章中写好的。链接在这里:http://blog.csdn.net/zknxx/article/details/72633444。
HttpClient发送InputStream对象
接着进入我们的正题,使用HttpClient发送InputStream对象,程序如下:
@Test public void inputStreamUpload() { //创建HttpClient对象 CloseableHttpClient client = HttpClients.createDefault(); //构建POST请求 请求地址请更换为自己的。 //1) HttpPost post = new HttpPost("http://localhost:8003/uploadAndDownload/uploadFileAction"); InputStream inputStream = null; try { //文件路径请换成自己的 //2) inputStream = new FileInputStream("G:\\LearnVideo\\text01.txt"); MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); //第一个参数为 相当于 Form表单提交的file框的name值 第二个参数就是我们要发送的InputStream对象了 //第三个参数是文件名 //3) builder.addBinaryBody("uploadFile", inputStream, ContentType.create("multipart/form-data"), "text01.txt"); //4)构建请求参数 普通表单项 StringBody stringBody = new StringBody("12",ContentType.MULTIPART_FORM_DATA); builder.addPart("id",stringBody); HttpEntity entity = builder.build(); post.setEntity(entity); //发送请求 HttpResponse response = client.execute(post); entity = response.getEntity(); if (entity != null) { inputStream = entity.getContent(); //转换为字节输入流 BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, Consts.UTF_8)); String body = null; while ((body = br.readLine()) != null) { System.out.println(body); } } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { if(inputStream != null){ try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }请注意上面程序中的1)、2)、3)、4)处。下面再写一个用HttpClient发送文件的小例子:
HttpClient发送文件
@Test public void fileUpload() { //构建HttpClient对象 CloseableHttpClient client = HttpClients.createDefault(); //构建POST请求 HttpPost httpPost = new HttpPost("http://localhost:8003/uploadAndDownload/uploadFileAction"); InputStream inputStream = null; try { //要上传的文件 File file = null; file = new File("G:\\qqq.txt"); //构建文件体 FileBody fileBody = new FileBody(file,ContentType.MULTIPART_FORM_DATA,"qqq.txt"); StringBody stringBody = new StringBody("12",ContentType.MULTIPART_FORM_DATA); HttpEntity httpEntity = MultipartEntityBuilder .create() .setMode(HttpMultipartMode.BROWSER_COMPATIBLE) .addPart("uploadFile", fileBody) .addPart("id",stringBody).build(); httpPost.setEntity(httpEntity); //发送请求 HttpResponse response = client.execute(httpPost); httpEntity = response.getEntity(); if(httpEntity != null){ inputStream = httpEntity.getContent(); //转换为字节输入流 BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, Consts.UTF_8)); String body = null; while ((body = br.readLine()) != null) { System.out.println(body); } } } catch (IOException e) { e.printStackTrace(); } finally { if(inputStream != null){ try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }
希望能帮助到需要的朋友。
阅读全文
0 0
- 使用HttpClient发送InputStream对象及发送文件
- httpClient发送key_value、json参数及文件
- HttpClient远程发送文件
- HttpClient发送文件与报文
- 使用HttpClient发送请求参数
- Java使用HttpClient发送请求
- 使用HttpClient发送GET请求
- 使用HttpClient发送POST请求
- 使用httpclient发送get请求
- 使用httpclient发送post请求
- 使用HttpClient携带文件发送请求简单实例
- httpclient 发送文件和字符串信息
- HttpClient 向tomcat服务器发送文件
- HttpClient发送http请求以及上传文件
- 在使用httpclient发送信息时报错解决
- 使用httpClient发送短信大体格式
- android使用apache httpclient发送post请求
- 使用HttpClient发送Post Http请求
- Java权限修饰符
- 使用Notification实现点赞、收藏功能
- Broadcast详解:有序广播
- <3> 自定义view 时钟
- the area hdu
- 使用HttpClient发送InputStream对象及发送文件
- Kotlin 语法特性
- Java中的重载与重写
- Lua知识点五 协同程序
- Android控件的隐藏与显示
- UNICODE,GBK,UTF-8区别
- Apache Shiro 学习目录
- STL中的set和multiset使用(一)_boolan(博览网)_第三门课_第一周_作业
- winpcap库使用