之前分享过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 Mappost(@RequestParam(value = "host") String host, @RequestParam("port") String port, @RequestParam("name") String name, @RequestParam("pw") String pw, @RequestParam("formFile") MultipartFile formFile) { Map map = new HashMap (); 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); } }