用httpclient4.3 post方式推送文件到服务端  
准备:httpclient-4.3.3.jar;httpcore-4.3.2.jar;httpmime-4.3.3.jar
/**
* 上传文件
* @throws ParseException
* @throws IOException
*/
publicstaticvoidpostFile()throwsParseException, IOException{
CloseableHttpClient httpClient = HttpClients.createDefault();
try{
// 要上传的文件的路径
String filePath =newString("F:/pic/001.jpg");
// 把一个普通参数和文件上传给下面这个地址 是一个servlet
HttpPost httpPost =newHttpPost(
"http://localhost:8080/xxx/xxx.action");
// 把文件转换成流对象FileBody
File file =newFile(filePath);
FileBody bin =newFileBody(file);
StringBody uploadFileName =newStringBody(
"把我修改成文件名称", 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
*/
publicstaticvoidgetFile(String url, String destFileName)
throwsClientProtocolException, IOException {
// 生成一个httpclient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpget =newHttpGet(url);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
InputStream in = entity.getContent();
File file =newFile(destFileName);
try{
FileOutputStream fout =newFileOutputStream(file);
intl = -1;
byte[] tmp =newbyte[1024];
while((l = in.read(tmp)) != -1) {
fout.write(tmp,0, l);
// 注意这里如果用OutputStream.write(buff)的话,图片会失真,大家可以试试
}
fout.flush();
fout.close();
}finally{
// 关闭低层流。
in.close();
}
httpclient.close();
}

最新文章

  1. xcode 一般插件
  2. Good Practices to Write Stored Procedures in SQL Server
  3. Thinkphp大纲——基础参考
  4. angularJS支持的事件
  5. css基本知识框架(转)
  6. leveldb - menifest文件格式
  7. cocos2d-x之物理引擎初试
  8. Ubuntu使用tcpdump工具
  9. linux mount命令的用法详细解析
  10. 深入剖析AutoreleasePool
  11. vijos P1375 大整数(高精不熟的一定要做!)
  12. Spring MVC PageNotFound.noHandlerFound No mapping found for HTTP request with URI
  13. uva 1291(dp)
  14. Oracle使用imp导入dmp数据提示:只有DBA才能导入有其他DBA导入的文件
  15. first-,second- and third-class value
  16. JavaScript学习一之数据校验
  17. URL控制器
  18. loj#6029. 「雅礼集训 2017 Day1」市场(线段树)
  19. kbmmw 5.04 发布
  20. Linux adduser

热门文章

  1. 无法识别的配置节 system.serviceModel
  2. URLEncode和URLDecoder作用
  3. angular 搜索记录保留
  4. hdu_1513_Palindrome(LCS+滚动数组)
  5. LeetCode OJ 257. Binary Tree Paths
  6. 老鸟需要知道的一些php系统类函数
  7. linux 配置tomcat服务器
  8. C++设计模式-Visitor访问者模式
  9. Drivers Dissatisfaction
  10. The Rings Akhaten