使用HttpClient发送HttpPost请求包含上传本地图片和远程图片的传输实现

在实际项目中需要在当前系统中模拟浏览器发送一个post请求,正常情况下传文字没多大问题,但是如果带上传文件功能的话,

网上的资料不太好找,好在经过我多方寻找,加上自由发挥,真让我搞出来了。

下面代码为核心代码,

可以上传 

File对象,

转换成byte数组类型(String类型Base64编码的信息的图片)

String类型 的数据,

满足了,发送post请求大部分上传需要,每一步都有详细说明,并且都有log打出。希望能帮到需要的人。微笑

需要的包我会放在链接中,解压密码为: nihaocsdn 供大家使用,亲测可用。

 点击打开链接

 
  1. package httpClientRequest;

  2.  
  3. import java.io.BufferedReader;

  4. import java.io.File;

  5. import java.io.IOException;

  6. import java.io.InputStream;

  7. import java.io.InputStreamReader;

  8.  
  9. import org.apache.commons.codec.binary.Base64;

  10. import org.apache.commons.lang3.StringUtils;

  11. import org.apache.http.HttpEntity;

  12. import org.apache.http.client.ClientProtocolException;

  13. import org.apache.http.client.methods.CloseableHttpResponse;

  14. import org.apache.http.client.methods.HttpPost;

  15. import org.apache.http.entity.ContentType;

  16. import org.apache.http.entity.mime.MultipartEntityBuilder;

  17. import org.apache.http.entity.mime.content.ByteArrayBody;

  18. import org.apache.http.entity.mime.content.FileBody;

  19. import org.apache.http.entity.mime.content.StringBody;

  20. import org.apache.http.impl.client.CloseableHttpClient;

  21. import org.apache.http.impl.client.HttpClients;

  22. import org.apache.http.util.EntityUtils;

  23.  
  24. public class httpTest {

  25.  
  26. public String start() throws ClientProtocolException, IOException{

  27. // 1. 创建上传需要的元素类型

  28. // 1.1 装载本地上传图片的文件

  29. File imageFile = new File("C:/Intel/test.jpg");

  30. FileBody imageFileBody = new FileBody(imageFile);

  31. // 1.2 装载经过base64编码的图片的数据

  32. String imageBase64Data = "vnweovinsldkjosdfgvndlgkdfgjsdfdfg";

  33. ByteArrayBody byteArrayBody = null;

  34. if(StringUtils.isNotEmpty(imageBase64Data)){

  35. byte[] byteImage = Base64.decodeBase64(imageBase64Data);

  36. byteArrayBody = new ByteArrayBody(byteImage,"image_name");

  37. }

  38. // 1.3 装载上传字符串的对象

  39. StringBody name = new StringBody("admin",ContentType.TEXT_PLAIN);

  40. System.out.println("装载数据完成");

  41. // 2. 将所有需要上传元素打包成HttpEntity对象

  42. HttpEntity reqEntity = MultipartEntityBuilder.create()

  43. .addPart("name", name)

  44. .addPart("image1",imageFileBody)

  45. .addPart("image2",byteArrayBody).build();

  46. System.out.println("打包数据完成");

  47. // 3. 创建HttpPost对象,用于包含信息发送post消息

  48. HttpPost httpPost = new HttpPost("http://www.cctv.com");

  49. httpPost.setEntity(reqEntity);

  50. System.out.println("创建post请求并装载好打包数据");

  51. // 4. 创建HttpClient对象,传入httpPost执行发送网络请求的动作

  52. CloseableHttpClient httpClient = HttpClients.createDefault();

  53. CloseableHttpResponse response = httpClient.execute(httpPost);

  54. System.out.println("发送post请求并获取结果");

  55. // 5. 获取返回的实体内容对象并解析内容

  56. HttpEntity resultEntity = response.getEntity();

  57. String responseMessage = "";

  58. try{

  59. System.out.println("开始解析结果");

  60. if(resultEntity!=null){

  61. InputStream is = resultEntity.getContent();

  62. BufferedReader br = new BufferedReader(new InputStreamReader(is));

  63. StringBuffer sb = new StringBuffer();

  64. String line = "";

  65. while((line = br.readLine()) != null){

  66. sb.append(line);

  67. }

  68. responseMessage = sb.toString();

  69. System.out.println("解析完成,解析内容为"+ responseMessage);

  70. }

  71. EntityUtils.consume(resultEntity);

  72. }finally{

  73. if (null != response){

  74. response.close();

  75. }

  76. }

  77. return responseMessage;

  78. }

  79.  
  80. public static void main(String[] args) throws ClientProtocolException, IOException {

  81. httpTest ht = new httpTest();

  82. ht.start();

  83. }

  84.  
  85. }

猜你喜欢

转载自blog.csdn.net/weixin_42273775/article/details/82108131
今日推荐