标签:
情境:web端上传文件到本地,本地使用httpclient转发到接口服务器存储。
1 //前期经过处理,其中list中为表单数据,ins中为文件流 2 PostMethod post = new PostMethod(url); 3 post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8"); 4 5 List<Part> parts = new ArrayList<Part>(); 6 7 if (list != null) { 8 int len = list.size(); 9 for (int i = 0; i < len; i++) { 10 parts.add(new StringPart(list.get(i).getName(),list.get(i).getValue())); 11 } 12 } 13 if(ins!=null && ins.size()>0){ 14 for (Map.Entry<String, InputStream> entry : ins.entrySet()) { 15 //entry.getKey();文件名 entry.getValue();文件流 16 //将InputStream转到FilePart格式 17 parts.add(new FilePart(entry.getKey(), new ByteArrayPartSource(entry.getKey(), getByte(entry.getValue())))); 18 } 19 } 20 HttpMethodParams params = post.getParams(); 21 Part[] partArray = new Part[parts.size()]; 22 MultipartRequestEntity mlEntity = new MultipartRequestEntity(parts.toArray(partArray),params); 23 post.setRequestEntity(mlEntity);
标签:
原文地址:http://www.cnblogs.com/chiying/p/5485964.html