之前分享过HttpClient模拟登录第三方项目的代码,这次分享一下HttpClient访问第三方项目接口上传文件的实现方案,并解决中文乱码问题,并且在上传文件的时候传输多个其他参数;
第一种方案,中文参数在url内部,使用URLEncoder类对中文进行encode:
public static void postTwo() throws Exception{ HttpClient client = new HttpClient(); //登录 PostMethod post = new PostMethod("http://127.0.0.1:8080/HelloWorld/post?host=172.16.2.183&port=10086&name=test&pw=test" + URLEncoder.encode("北京", "utf-8")); FilePart fp = new FilePart("formFile", new File("D://PTOC_SCENIC_DAY_2015043015.txt")); Part[] parts = {fp}; MultipartRequestEntity entity = new MultipartRequestEntity(parts, new HttpMethodParams()); post.setRequestEntity(entity); client.executeMethod(post); //释放连接,以免超过服务器负荷 System.out.println("============================"); System.out.println(post.getResponseBodyAsString()); System.out.println("============================"); //释放连接,以免超过服务器负荷 post.releaseConnection(); }
第二种方案,使用Part类,该类有两个子类,一个StringPart,一个FilePart,StringPart用于传输普通字符串参数,FilePart用于上传文件:
public static void postThree() throws Exception{ HttpClient client = new HttpClient(); //登录 PostMethod post = new PostMethod("http://127.0.0.1:8080/HelloWorld/post"); FilePart fp = new FilePart("formFile", "JNDI.txt", new File("D://JNDI配置数据库连接池_获取数据库连接.txt")); fp.setCharSet("utf-8"); System.out.println(fp.getCharSet()); Part[] parts = { fp, new StringPart("host", "172.16.2.183", "UTF-8"), new StringPart("port", "10086", "UTF-8"), new StringPart("name", "test色鬼", "UTF-8"), new StringPart("pw", "test特殊", "UTF-8") }; MultipartRequestEntity entity = new MultipartRequestEntity(parts, new HttpMethodParams()); post.setRequestEntity(entity); System.out.println(entity.getContentType()); client.executeMethod(post); //释放连接,以免超过服务器负荷 System.out.println("============================"); System.out.println(post.getResponseBodyAsString()); System.out.println("============================"); //释放连接,以免超过服务器负荷 post.releaseConnection(); }
以上两种方案对应的第三方项目的接口代码如下:
@RequestMapping(value = "/post", method = { RequestMethod.POST }) public @ResponseBody Map<String, Object> post(@RequestParam(value = "host") String host, @RequestParam("port") String port, @RequestParam("name") String name, @RequestParam("pw") String pw, @RequestParam("formFile") MultipartFile formFile) { Map<String, Object> map = new HashMap<String, Object>(); try{ map.put("tag", "1"); map.put("msg", "上传成功!"); System.out.println(host + ":" + port + " " + name + " " + pw); String fileName = formFile.getOriginalFilename(); FileOutputStream write = new FileOutputStream("D://ww" + fileName); InputStream read = formFile.getInputStream(); byte data[] = new byte[1024]; int count; while ((count = read.read(data, 0, 1024)) != -1) { write.write(data, 0, count); } read.close(); write.close(); }catch (Exception e) { e.printStackTrace(); map.put("tag", "0"); map.put("msg", "上传失败:" + e.getMessage()); } return map; }
这两只方案都只能解决普通字符串参数的中文乱码问题,对于上传的文件其文件名中文乱码问题不能使用setCharSet方法设置编码方式解决,至于原因,查看源码就可以知道,StringPart和FilePart类中对中午的处理方式不一样,FilePart在转换文件名时,用的方法是EncodingUtil.getAsciiBytes(),查看这个方法源码为data.getBytes("US-ASCII"),因此中文文件名必定乱码,不管你是否调用了setCharSet("GBK")方法。 解决很简单:out.write(EncodingUtil.getBytes(filename, getCharSet()));想要解决附件文件名中文乱码只能靠修改源码了,FilePart中对中午乱码起至关重要的部分为:
protected void sendDispositionHeader(OutputStream out) throws IOException { LOG.trace("enter sendDispositionHeader(OutputStream out)"); super.sendDispositionHeader(out); String filename = this.source.getFileName(); if (filename != null) { out.write(FILE_NAME_BYTES); out.write(QUOTE_BYTES); out.write(EncodingUtil.getAsciiBytes(filename)); out.write(QUOTE_BYTES); } }
相关推荐
文档中详细演示了如何使用HttpClient4进行文件上传
服务器端自己写,本示例为客户端代码,可以解决httpClient4乱码问题,是替代修改源码的最佳写法。
HttpClient4.2.5实现文件上传,无中文文件名乱码问题。亲测无问题。有问题可吐槽!
解决HttpClient中文乱码问题jar文件
httpclient3 httpclient4 上传文件 乱码问题
下载这个工具类,然后直接调用createHtmlPage方法就可以了。最关键一点是,网上的方法没有一个解决了乱码问题,费了我不少功夫。
用HttpClient实现文件下载需要满足:有一台web服务器,要下载的源文件需存放在web服务器的特定目录下。通过发起http请求实现文件下载功能。
很全面的代码介绍,很完整的乱码解决说明,让你一目了然,值得研究
使用httpclient上传文件, CloseableHttpClient httpClient = HttpClientBuilder.create().build();
本功能支持文件上传 下载,支持请求流,支持JSON、XML请求,本版本是httpclient 4,其它jar包自行下载
HttpClient 上传文件 Servlet 处理文件上传 commons-fileupload 处理文件上传 commons-fileupload 处理文件上传,在struts中可以不用对应actionform,在jsp,servelet中应用都很方便
使用HttpClient实现文件的上传所需lib
花了大半天写的测试类,用的时候记得改下引用的包名,可以当util工具类用,其他说明见博客 ...流方式发送接收: ...觉得可以的话,又不想复制粘贴,就花点积分下载吧,o(* ̄︶ ̄*)o
httpClient上传文件,能直接运行的代码,appache commons
文件上传 HttpClient
HttpClient Post提交多文件及多个普通参数,已经封装成工具类。 需传入 要请求的url 普通参数map 例 map.put("param1","张三"); 需要传入的文件流map 其中key为文件名 服务端接收无乱码。
NULL 博文链接:https://wangwang-163-com.iteye.com/blog/728831
用httpcient上传文件,用httpclient上传文件
该文件中包含了arduino的文件库函数,包含向服务器以及网页等接收传输数据。
httpclient和RestfuL服务风格上传下载文件,可改为JAVA分布式系统进行交互,具体用法和改法详见代码里的注释