首页 > 代码库 > httpclient 4.3 psot方法上传文件与参数 中文乱码解决
httpclient 4.3 psot方法上传文件与参数 中文乱码解决
废话不多说,直接上有码的!
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.out56 .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 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。