HttpClient上传下载Demo
2016-03-11 17:38
295 查看
importorg.apache.http.Consts; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.entity.mime.content.StringBody; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.CharsetUtils; import org.apache.http.util.EntityUtils; import org.apache.http.entity.mime.content.FileBody; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.nio.charset.Charset; import java.text.ParseException; public class HttpClient { /** * 上传文件 * @throws ParseException * @throws IOException */ public static void postFile() throws ParseException, IOException{ CloseableHttpClient httpClient = HttpClients.createDefault(); try { // 要上传的文件的路径 String filePath = new String("D:/ceshi.png"); // 把一个普通参数和文件上传给下面这个地址 是一个servlet HttpPost httpPost = new HttpPost( "http://localhost:8080/xxx/xxx.action"); // 把文件转换成流对象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(); } } //下载 public static void getFile() throws ClientProtocolException, IOException { // 生成一个httpclient对象 CloseableHttpClient httpclient = HttpClients.createDefault(); HttpGet httpget = new HttpGet("http://www.baidu.com/img/bd_logo1.png"); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); InputStream in = entity.getContent(); File file = new File("D:\\ceshi.png"); 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(); } public static void main(String[] args) throws IOException { HttpClient.getFile(); System.out.println("done!"); } }
相关文章推荐
- 小米手机TCP连接一些奇怪现象
- 关于文件、网络传输以及内存存储的大小端问题
- HttpClient模拟登陆
- 替换Servlet容器的HttpSession实现集群中Session共享(With Redis)
- TCP的连接建立和释放(三次握手和四次握手)
- Http协议
- IOS 使用AF网络请求统一RSA加密+URL不识别中文的解决办法
- TCP传输控制协议 读书笔记
- 关于TCP和UDP的异同点
- iOS----网络开发之第三方库AFNetworking
- [转]Linux服务器上11种网络连接状态 和 TCP三次握手/四次挥手详解
- 计算机网络之运输层
- Netscaler 10.5 VPX与XenApp XenDesktop 集成配置系列之一基本网络配置
- 【网络流+线段树】[CQBZOJ3065]生死游戏((A+B)^2 Problem)
- Linux环境下基于条件约束的HTTP/TCP透明代理和流量牵引方案
- 肖哥教你快速子网划分
- 网络时延——发送时延和传播时延
- http://bbs.pediy.com/showthread.php?p=1365122
- TCP和UDP
- nc:网络黑客瑞士军刀