httpClient上传下载文件
2016-04-07 10:18
549 查看
/** * 上传文件 * @throws ParseException * @throws IOException */ public static void postFile(InputStream inputStream)throws ParseException, IOException{ CloseableHttpClient httpClient = HttpClients.createDefault(); try{ // 要上传的文件的路径 // String filePath = new String("F:/pic/001.jpg"); // 把一个普通参数和文件上传给下面这个地址 是一个servlet // HttpPost httpPost =new HttpPost("http://localhost:8080/xxx/xxx.action"); HttpPost httpPost =new HttpPost("http://localhost:8088/spider/testFile.do"); // 把文件转换成流对象 FileBody // FileBody bin =new FileBody(file); StringBody uploadFileName =new StringBody("把我修改成文件名称", ContentType.create("text/plain", Consts.UTF_8)); //以浏览器兼容模式运行,防止文件名乱码。 HttpEntity reqEntity = MultipartEntityBuilder.create().setMode(HttpMultipartMode.BROWSER_COMPATIBLE) // .addBinaryBody("uploadFile", inputStream) .addBinaryBody("uploadFile", inputStream ,ContentType.create("jpg") ,"_ddddddd.png") // .addPart("uploadFile", inputStream)//uploadFile对应服务端类的同名属性<File类型> .addPart("uploadFileName", uploadFileName)//uploadFileName对应服务端类的同名属性<String类型> .setCharset(CharsetUtils.get("UTF-8")).build(); httpPost.setEntity(reqEntity); System.out.println("发起请求的页面地址 "+ httpPost.getRequestLine()); // 发起请求 并返回请求的响应 CloseableHttpResponse response = httpClient.execute(httpPost); try{ System.out.println("----------------------------------------"); // 打印响应状态 System.out.println(response.getStatusLine()); // 获取响应对象 HttpEntity resEntity = response.getEntity(); if(resEntity !=null) { // 打印响应长度 System.out.println("Response content length: " + resEntity.getContentLength()); // 打印响应内容 System.out.println(EntityUtils.toString(resEntity, Charset.forName("UTF-8"))); } // 销毁 EntityUtils.consume(resEntity); }finally{ response.close(); } }finally{ httpClient.close(); } } public static void postFile(String filePath)throws ParseException, IOException{ CloseableH 4000 ttpClient httpClient = HttpClients.createDefault(); try{ // 要上传的文件的路径 // String filePath = new String("F:/pic/001.jpg"); // 把一个普通参数和文件上传给下面这个地址 是一个servlet // HttpPost httpPost =new HttpPost("http://localhost:8080/xxx/xxx.action"); HttpPost httpPost =new HttpPost("http://localhost:8088/spider/testFile.do"); // 把文件转换成流对象 FileBody File file =new File(filePath); FileBody bin =new FileBody(file); StringBody uploadFileName =new StringBody("把我修改成文件名称", ContentType.create("text/plain", Consts.UTF_8)); //以浏览器兼容模式运行,防止文件名乱码。 HttpEntity reqEntity = MultipartEntityBuilder.create().setMode(HttpMultipartMode.BROWSER_COMPATIBLE) .addPart("uploadFile", bin)//uploadFile对应服务端类的同名属性<File类型> .addPart("uploadFileName", uploadFileName)//uploadFileName对应服务端类的同名属性<String类型> .setCharset(CharsetUtils.get("UTF-8")).build(); httpPost.setEntity(reqEntity); System.out.println("发起请求的页面地址 "+ httpPost.getRequestLine()); // 发起请求 并返回请求的响应 CloseableHttpResponse response = httpClient.execute(httpPost); try{ System.out.println("----------------------------------------"); // 打印响应状态 System.out.println(response.getStatusLine()); // 获取响应对象 HttpEntity resEntity = response.getEntity(); if(resEntity !=null) { // 打印响应长度 System.out.println("Response content length: " + resEntity.getContentLength()); // 打印响应内容 System.out.println(EntityUtils.toString(resEntity, Charset.forName("UTF-8"))); } // 销毁 EntityUtils.consume(resEntity); }finally{ response.close(); } }finally{ httpClient.close(); } } /** * 下载文件 * @param url * @param destFileName xxx.jpg/xxx.png/xxx.txt * @throws ClientProtocolException * @throws IOException */ public static void getFile(String url, String destFileName) throws ClientProtocolException, IOException { // 生成一个httpclient对象 CloseableHttpClient httpclient = HttpClients.createDefault(); HttpGet httpget =new HttpGet(url); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); InputStream in = entity.getContent(); File file =new File(destFileName); try{ FileOutputStream fout =new FileOutputStream(file); int l = -1; byte[] tmp =new byte[1024]; while((l = in.read(tmp)) != -1) { fout.write(tmp,0, l); // 注意这里如果用OutputStream.write(buff)的话,图片会失真,大家可以试试 } fout.flush(); fout.close(); }finally{ // 关闭低层流。 in.close(); } httpclient.close(); }
相关文章推荐
- 自动化运维平台 | 新炬网络
- CocoaAsyncSocket网络通信使用之tcp连接(一)
- 神经网络基本概念
- gitlab 添加https://gitlab.com/ci gitlab-ci-multi-runner
- TCP/IP常见问题总结(二)
- 【windows socket+TCPserverclient】
- http状态码
- http请求方法
- 3.6 网络编程与reactor模式
- 3.2.7 INET_PROTO_INFO结构体:记录网络协议
- 映射网络驱动盘开机不需输入密码
- 浅谈Tomcat、web程序结构与Http协议
- 解决HttpServletResponse输出中文乱码问题
- SSL与TLS 区别 以及介绍
- 3.1.3 网络编程
- 虚拟机主机网络通信
- 什么是HTTPS
- PRML-贝叶斯网络
- Linux C高级编程——网络编程之包裹函数
- IOS混合编程 - Http for IOS (二)