/** * 上传文件 * @throws ParseException * @throws IOException */ public static void postFile() 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"); // 把文件转换成流对象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 http://www.xxx.com/img/333.jpg * @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(); }
相关推荐
httpClient上传文件,能直接运行的代码,appache commons
httpclient和RestfuL服务风格上传下载文件,可改为JAVA分布式系统进行交互,具体用法和改法详见代码里的注释
Eclipse下完整的java程序,包含HttpClient的全部jar包。通过java类文件,实现通过链接将文件下载本地
本功能支持文件上传 下载,支持请求流,支持JSON、XML请求,本版本是httpclient 4,其它jar包自行下载
文档中详细演示了如何使用HttpClient4进行文件上传
httpclient3 httpclient4 上传文件 乱码问题
使用httpclient上传文件, CloseableHttpClient httpClient = HttpClientBuilder.create().build();
本人自己总结的httpClient PostMethod 上传文件完整实例 以及 使用java PostMethod 和GetMethod 发送请求实例 及乱码处理
使用HttpClient方式进行文件上传时需要用到的jar包,亲测可用。
NULL 博文链接:https://wangwang-163-com.iteye.com/blog/728831
用httpcient上传文件,用httpclient上传文件
文件上传 HttpClient
HttpClient 上传文件 Servlet 处理文件上传 commons-fileupload 处理文件上传 commons-fileupload 处理文件上传,在struts中可以不用对应actionform,在jsp,servelet中应用都很方便
花了大半天写的测试类,用的时候记得改下引用的包名,可以当util工具类用,其他说明见博客 ...流方式发送接收: ...觉得可以的话,又不想复制粘贴,就花点积分下载吧,o(* ̄︶ ̄*)o
HttpClient4.2.5实现文件上传,无中文文件名乱码问题。亲测无问题。有问题可吐槽!
Android使用HttpClient实现上传下载功能并监控进度
struts2 android httpclient 上传文件 客服端 服务器端 亲测通过
Android使用HttpClient上传文件到服务器完整实例,包括要用到的jar都下libs下面,服务器的源码也有
该工具类使用httpclient进行http or https请求,包括requestbody格式和form表单格式,另外含文件服务器中转上传方法,几乎支持所有常用接口调用,内含详细注释和说明文件,含jar包,及maven方式引用,拿过去直接用吧
使用HttpClient实现文件的上传所需lib