httpclient上传文件及文件名中文乱码问题
2016-09-14 09:15
666 查看
logger.info("请求url: "+url); HttpClient httpclient = new DefaultHttpClient(); HttpPost http = new HttpPost(url); File file=null; try { List<NameValuePair> formparams = new ArrayList<NameValuePair>(); if(requestParameterMap.size()==0&¶ms!=null){ for (String[] param : params) { logger.info(param[0]+ "--->" +param[1]); formparams.add(new BasicNameValuePair(param[0], param[1])); } UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8"); http.setEntity(uefEntity); }else { // MultipartEntity mutiEntity = new MultipartEntity(); MultipartEntity mutiEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, null, Charset.forName("utf-8")); //文件名乱码设置 for(Map.Entry<String, String> entry:requestParameterMap.entrySet()){ //字符串请求参数 logger.info(entry.getKey() + "--->" + entry.getValue()); mutiEntity.addPart(entry.getKey(), new StringBody(entry.getValue(),Charset.forName("utf-8"))); } if(requestFileMap.size()>0){ //文件请求参数 for(Map.Entry<String, String> entry:requestFileMap.entrySet()){ logger.info(entry.getKey() + "--->" + entry.getValue()); file=new File(dir+"/"+entry.getValue()); FileBody fileBody = new FileBody(file); // mutiEntity.addPart(entry.getKey(),new FileBody(file,entry.getValue(),"application/octet-stream", "utf-8")); mutiEntity.addPart(entry.getKey(),fileBody); http.setEntity(mutiEntity); } } } HttpResponse response1 = httpclient.execute(http); HttpEntity entity = response1.getEntity(); if(file!=null&&file.exists()){ file.delete(); } String[] name=new File(fileName).getName().split("_"); String responseFileName="httpResponse_"+name[1]; responseFile=new File(dir+"/"+responseFileName); logger.info("响应文件路径: "+responseFile.getPath()); FileOutputStream fo=new FileOutputStream(responseFile); Header[] heads = response1.getAllHeaders(); // 打印所有响应头 for(Header h:heads){ logger.info(h.getName() + ":" + h.getValue()); fo.write((h.getName() + ":" + h.getValue() + System.getProperty("line.separator")).getBytes()); fo.flush(); } fo.write(("" + System.getProperty("line.separator")).getBytes()); if (entity != null) { /*result= EntityUtils.toString(entity,"utf-8"); logger.info("响应结果: "); logger.info(result); fo.write(result.getBytes());*/ byte[] responseByte=EntityUtils.toByteArray(entity); logger.info("responseByte.length: "+responseByte.length); logger.info("响应内容: "+new String(responseByte,"utf-8")); logger.info(bytesToHexString(responseByte).getBytes()); fo.write(bytesToHexString(responseByte).getBytes()); fo.flush(); fo.close(); try { sendFile(responseFile,fileName); } catch (InterruptedException e) { logger.info(e); } } } catch (ClientProtocolException e) { logger.info(e); }catch(UnsupportedEncodingException e1) { logger.info(e1); }catch (IOException e) { logger.info(e); }finally{ httpclient.getConnectionManager().shutdown(); }
相关文章推荐
- 解决httpclient上传文件的时候中文文件名乱码的问题
- 解决HttpClient的FilePart上传文件中使用中文名称文件名乱码问题
- 解决HttpClient的FilePart上传文件中使用中文名称文件名乱码问题
- HttpClient 4.3.* 上传带中文文件名文件文件名乱码问题的解决
- commons.httpclient上传文件的文件名的中文问题
- PHP中上传含有中文的文件名的文件出现乱码问题
- PHP文件上传并解决中文文件名乱码问题
- 解决上传文件时服务端中文文件名乱码问题
- 使用edtftpnet上传文件到Serv-U出现乱码的问题,并且不能创建中文文件名或文件夹。
- 中文名文件上传到linux服务器文件名乱码问题(转)
- php上传中文文件文件名乱码问题
- FCKeditor for Java 中文文件名文件上传乱码问题
- 解决上传文件时服务端中文文件名乱码问题
- 解决COS、FileUpload上传文件时中文文件名乱码问题
- 中文乱码问题(使用Servlet3.0新特性实现文件上传——上传文件名中文乱码问题)
- PHP——上传中文文件名乱码问题解决
- httpclient 应用 文件上传 中文编码问题
- 解决中文文件名上传到模拟器变乱码的问题
- FCKeditor 在上传中文文件时文件名出现乱码
- c# web 下载文件(解决中文文件名乱码问题)