---
title: "android中模拟http协议表单上传"
author: "445822357"
date: "2014-10-24T15:21:00Z"
tags: []
slug: "android-http"
draft: false
lastmod: "2014-10-29T11:15:46Z"
id: 2149149
url: "https://445822357.iteye.com/blog/2149149"
---

转自：http://helloandroid.iteye.com/blog/1183853

利用ie浏览器插件httpwatch查看form表单上传时的数据封装格式，然后照着这数据格式自己一步一步封装

![](http://dl.iteye.com/upload/attachment/563149/6fff3f74-b521-3ce7-9064-79049bc35521.jpg "点击查看原始大小图片")

![](http://dl.iteye.com/upload/attachment/563151/3ec4be92-3780-36b0-ae18-ae12e353f954.jpg)

![](http://dl.iteye.com/upload/attachment/563153/afd61265-e591-3f99-95db-258e0b2d8762.jpg)

Java代码[![收藏代码](http://helloandroid.iteye.com/images/icon_star.png)]()

01. packagecom.android.cist.network.form;
02. importjava.io.DataOutputStream;
03. importjava.io.InputStream;
04. importjava.io.UnsupportedEncodingException;
05. importjava.net.HttpURLConnection;
06. importjava.net.URL;
07. importjava.net.URLEncoder;
08. importjava.util.Iterator;
09. importjava.util.Map;
10. importjava.util.Set;
11. publicclassHttpFormUtil{
12. publicstaticStringpost(StringactionUrl,Map&lt;String,String&gt;params,FormFile\[]files){
13. try{
14. StringenterNewline="\\r\\n";
15. Stringfix="--";
16. Stringboundary="######";
17. StringMULTIPART\_FORM\_DATA="multipart/form-data";
18. URLurl=newURL(actionUrl);
19. HttpURLConnectioncon=(HttpURLConnection)url.openConnection();
20. con.setDoInput(true);
21. con.setDoOutput(true);
22. con.setUseCaches(false);
23. con.setRequestMethod("POST");
24. con.setRequestProperty("Connection","Keep-Alive");
25. con.setRequestProperty("Accept","image/gif,image/x-xbitmap,image/jpeg,image/pjpeg,application/x-shockwave-flash,application/msword,application/vnd.ms-excel,application/vnd.ms-powerpoint,\*/\*");
26. con.setRequestProperty("Accept-Encoding","gzip,deflate");
27. con.setRequestProperty("Charset","UTF-8");
28. con.setRequestProperty("Content-Type",MULTIPART\_FORM\_DATA+";boundary="+boundary);
29. DataOutputStreamds=newDataOutputStream(con.getOutputStream());
30. Set&lt;String&gt;keySet=params.keySet();
31. Iterator&lt;String&gt;it=keySet.iterator();
32. while(it.hasNext()){
33. Stringkey=it.next();
34. Stringvalue=params.get(key);
35. ds.writeBytes(fix+boundary+enterNewline);
36. ds.writeBytes("Content-Disposition:form-data;"+"name=\\""+key+"\\""+enterNewline);
37. ds.writeBytes(enterNewline);
38. //ds.write(value.getBytes("UTF-8"));
39. ds.writeBytes(value);//如果有中文乱码，保存改用上面的ds.writeBytes(enterNewline);那句代码
40. ds.writeBytes(enterNewline);
41. }
42. if(files!=null&&files.length&gt;0){
43. ds.writeBytes(fix+boundary+enterNewline);
44. ds.writeBytes("Content-Disposition:form-data;"+"name=\\""+files\[0].getFormname()+"\\""+";filename=\\""+files\[0].getFilname()+"\\""+enterNewline);
45. ds.writeBytes(enterNewline);
46. ds.write(files\[0].getData());
47. ds.writeBytes(enterNewline);
48. }
49. ds.writeBytes(fix+boundary+fix+enterNewline);
50. ds.flush();
51. InputStreamis=con.getInputStream();
52. intch;
53. StringBufferb=newStringBuffer();
54. while((ch=is.read())!=-1){
55. b.append((char)ch);
56. }
57. ds.close();
58. returnb.toString().trim();
59. }catch(Exceptione){
60. thrownewRuntimeException(e);
61. }
62. }
63. publicstaticStringencode(Stringurl){
64. try{
65. returnURLEncoder.encode(url,"UTF-8");
66. }catch(UnsupportedEncodingExceptionex){
67. returnurl;
68. }
69. }
70. }