ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

httpclient 上传、下载文件,美团Android研发岗二面

2021-11-19 17:01:32  阅读:132  来源: 互联网

标签:美团 xxx System println new Android out response httpclient


  1. // 把一个普通参数和文件上传给下面这个地址 是一个servlet

  2. HttpPost httpPost = new HttpPost(

  3. “http://localhost:8080/xxx/xxx.action”);

  4. // 把文件转换成流对象FileBody

  5. File file = new File(filePath);

  6. FileBody bin = new FileBody(file);

  7. StringBody uploadFileName = new StringBody(

  8. “把我修改成文件名称”, ContentType.create(

  9. “text/plain”, Consts.UTF_8));

  10. //以浏览器兼容模式运行,防止文件名乱码。

  11. HttpEntity reqEntity = MultipartEntityBuilder.create().setMode(HttpMultipartMode.BROWSER_COMPATIBLE)

  12. .addPart(“uploadFile”, bin) //uploadFile对应服务端类的同名属性<File类型>

  13. .addPart(“uploadFileName”, uploadFileName)//uploadFileName对应服务端类的同名属性<String类型>

  14. .setCharset(CharsetUtils.get(“UTF-8”)).build();

  15. httpPost.setEntity(reqEntity);

  16. System.out.println("发起请求的页面地址 " + httpPost.getRequestLine());

  17. // 发起请求 并返回请求的响应

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

  19. try {

  20. System.out.println("----------------------------------------");

  21. // 打印响应状态

  22. System.out.println(response.getStatusLine());

  23. // 获取响应对象

  24. HttpEntity resEntity = response.getEntity();

  25. if (resEntity != null) {

  26. // 打印响应长度

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

开源分享完整内容戳这里

System.out.println("Response content length: "

  1. + resEntity.getContentLength());

  2. // 打印响应内容

  3. System.out.println(EntityUtils.toString(resEntity,

  4. Charset.forName(“UTF-8”)));

  5. }

  6. // 销毁

  7. EntityUtils.consume(resEntity);

  8. } finally {

  9. response.close();

  10. }

  11. } finally {

  12. httpClient.close();

  13. }

  14. }

  15. /**

  16. * 下载文件

  17. * @param url            http://www.xxx.com/img/333.jpg

  18. * @param destFileName   xxx.jpg/xxx.png/xxx.txt

  19. * @throws ClientProtocolException

  20. * @throws IOException

  21. */

  22. public static void getFile(String url, String destFileName)

  23. throws ClientProtocolException, IOException {

  24. // 生成一个httpclient对象

  25. CloseableHttpClient httpclient = HttpClients.createDefault();

  26. HttpGet httpget = new HttpGet(url);

  27. HttpResponse response = httpclient.execute(httpget);

  28. HttpEntity entity = response.getEntity();

  29. InputStream in = entity.getContent();

  30. File file = new File(destFileName);

  31. try {

  32. FileOutputStream fout = new FileOutputStream(file);

标签:美团,xxx,System,println,new,Android,out,response,httpclient
来源: https://blog.csdn.net/m0_64319455/article/details/121426554

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有