码迷,mamicode.com
首页 > 其他好文 > 详细

httpclient 4.3 psot方法上传文件与参数 中文乱码解决

时间:2014-08-01 10:43:51      阅读:337      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   java   os   io   文件   

废话不多说,直接上有码的!

 1 package httpclient;
 2 
 3 import java.io.File;
 4 import java.nio.charset.Charset;
 5 
 6 import org.apache.http.Consts;
 7 import org.apache.http.Header;
 8 import org.apache.http.HttpEntity;
 9 import org.apache.http.client.methods.CloseableHttpResponse;
10 import org.apache.http.client.methods.HttpPost;
11 import org.apache.http.entity.ContentType;
12 import org.apache.http.entity.mime.HttpMultipartMode;
13 import org.apache.http.entity.mime.MultipartEntityBuilder;
14 import org.apache.http.entity.mime.content.FileBody;
15 import org.apache.http.entity.mime.content.StringBody;
16 import org.apache.http.impl.client.CloseableHttpClient;
17 import org.apache.http.impl.client.HttpClients;
18 import org.apache.http.util.CharsetUtils;
19 import org.apache.http.util.EntityUtils;
20 
21 public class HttpDemo {
22     public static void main(String[] args) throws Exception {
23         CloseableHttpClient httpclient = HttpClients.createDefault();
24         try {
25             HttpPost httppost = new HttpPost("http://localhost:28080"
26                     + "/pm/restful/trip/savePTripRecord");
27 
28             // FileBody bin =// new FileBody(new File("D://树.jpg"));
29             File file = new File("D://树.jpg");
30             FileBody bin = new FileBody(file);
31             //普通文本參數編碼
32             StringBody userId = new StringBody(
33                     "f39d2a3e466b292b01466f2388900020", ContentType.create(
34                             "text/plain", Consts.UTF_8));
35             StringBody startdate = new StringBody("2014-8-1 09:13:12",
36                     ContentType.create("text/plain", Consts.UTF_8));
37             StringBody enddate = new StringBody("2014-8-1 09:13:12",
38                     ContentType.create("text/plain", Consts.UTF_8));
39             StringBody triptime = new StringBody("6.8", ContentType.create(
40                     "text/plain", Consts.UTF_8));
41             StringBody tripaddress = new StringBody("什么啊", ContentType.create(
42                     "text/plain", Consts.UTF_8));
43             StringBody tripresons = new StringBody("什么啊", ContentType.create(
44                     "text/plain", Consts.UTF_8));
45             HttpEntity reqEntity = MultipartEntityBuilder.create()
46                     .setMode(HttpMultipartMode.BROWSER_COMPATIBLE)//設置以瀏覽器兼容模式
47                     .setCharset(CharsetUtils.get("utf8"))//設置編碼
48                     .addPart("userId", userId).addPart("startdate", startdate)
49                     .addPart("enddate", enddate).addPart("triptime", triptime)
50                     .addPart("tripaddress", tripaddress)
51                     .addPart("tripresons", tripresons).addPart("bin", bin)
52                     .build();
53 
54             httppost.setEntity(reqEntity);
55             System.out
56                     .println("executing request " + httppost.getRequestLine());
57             CloseableHttpResponse response = httpclient.execute(httppost);
58             try {
59                 System.out.println("----------------------------------------");
60                 System.out.println(response.getStatusLine());
61                 HttpEntity resEntity = response.getEntity();
62                 if (resEntity != null) {
63                     System.out.println("Response content length: "
64                             + resEntity.getContentLength());
65                 }
66                 System.out.println(EntityUtils.toString(resEntity,
67                         Charset.forName("UTF-8")));
68                 EntityUtils.consume(resEntity);
69             } finally {
70                 response.close();
71             }
72         } finally {
73             httpclient.close();
74         }
75     }
76 
77 }

 

httpclient 4.3 psot方法上传文件与参数 中文乱码解决,布布扣,bubuko.com

httpclient 4.3 psot方法上传文件与参数 中文乱码解决

标签:style   blog   http   color   java   os   io   文件   

原文地址:http://www.cnblogs.com/gaopan/p/3884307.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!