Android Post mode upload file (simulate form format data submission)

Source: Internet
Author: User

The form submission includes:

Post/upload.php?zp_id=ab46ca6d703e3a1580c1c9b8b3a8fb39 http/1.1
Accept:image/gif, Image/jpeg, Image/pjpeg, Image/pjpeg, Application/x-ms-application, APPLICATION/X-MS-XBAP, Application/vnd.ms-xpsdocument, Application/xaml+xml, Application/vnd.ms-excel, Application/vnd.ms-powerpoint, Application/msword, */*
Referer:http://campus.eastmoney.com/upload/uploadf.php?zp_id=ab46ca6d703e3a1580c1c9b8b3a8fb39
Accept-language:zh-cn
user-agent:mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; trident/4.0;. NET CLR 1.1.4322;. NET CLR 2.0.50727;. NET CLR 3.0.4506.2152;. NET CLR 3.5.30729;. net4.0c;. NET4.0E)
Content-type:multipart/form-data; boundary=---------------------------7de8c1a80910
Accept-encoding:gzip, deflate
Host:campus.eastmoney.com
content-length:8636
Connection:keep-alive
Cache-control:no-cache
cookie:emstat_bc_emcount=7843405732305291404; emstat_ss_emcount=104_1395241887_3077552069; zp_id=ab46ca6d703e3a1580c1c9b8b3a8fb39; ZP_TAG=%C8%CB%CA%C2%D6%FA%C0%ED%A3%A8%C8%CB%C1%A6%D7%CA%D4%B4%B2%BF%A3%A9; Phpsessid=e4820e25c1ced88e0e677ed9610f3f56

-----------------------------7de8c1a80910
Content-disposition:form-data; Name= "__viewstate"


-----------------------------7de8c1a80910
Content-disposition:form-data; Name= "Txtfileser"


-----------------------------7de8c1a80910
Content-disposition:form-data; Name= "Txtseqid"

94564504-c3bd-44c1-8048-e1195f701c9b
-----------------------------7de8c1a80910
Content-disposition:form-data; Name= "Txtalias"


-----------------------------7de8c1a80910
Content-disposition:form-data; Name= "Txtsourceid"

8619075e-952c-45d5-90e7-43d80e1e7f40
-----------------------------7de8c1a80910
Content-disposition:form-data; Name= "Txtcname"


-----------------------------7de8c1a80910
Content-disposition:form-data; Name= "Txtupload"; Filename= "Ss.jpg"
Content-type:image/pjpeg

籿 湈 x=* maturity?;? Hermeneutical Fang Tube o*kbpu#m 噡


-----------------------------7de8c1a80910
Content-disposition:form-data; Name= "Btnupload"

Upload
-----------------------------7de8c1a80910--

Import Java.io.bufferedreader;import java.io.dataoutputstream;import java.io.file;import java.io.FileInputStream; Import Java.io.fileoutputstream;import Java.io.ioexception;import Java.io.inputstream;import Java.io.inputstreamreader;import Java.net.httpurlconnection;import Java.net.malformedurlexception;import Java.net.url;import Java.util.hashmap;import Java.util.iterator;import Com.eastmoney.util.fileutil;public class Httpassistant implements Runnable {public static final int request_error = 0xfff;public static final int url_marlformed_er ROR = 0xffe;public static final int io_exception_reeor = 0xffd;public static final int response_success = 0xffc;final int connect_time_out = 10000;final int read_time_out = 10000; String requeststring; Httpresponsecallback Mcallback; RequestType type; hashmap<string, string> params;/** * @param requeststring * Request String * @param callback * Request Result callback * The @param type * Request Type (get/post) * @param params * POST request parameter */publiC Httpassistant (String requeststring, Httpresponsecallback callback,requesttype type, hashmap<string, String> params) {this.requeststring = Requeststring;this.mcallback = Callback;this.type = Type;this.params = params;} @Overridepublic void Run () {if (type = = null) {throw new IllegalArgumentException ("Networkrequest must specify a request T Ype ");} StringBuilder sb = new StringBuilder (); HttpURLConnection conn = null;if (type = = Requesttype.get | | type = = requesttype.get_thumbnail| | type = = REQUESTTYPE.GET_A udio| | Type = = Requesttype.get_image) {//GET request try {URL url = new URL (requeststring); conn = (httpurlconnection) url.openconnectio n (); conn.setconnecttimeout (connect_time_out); Conn.setreadtimeout (read_time_out); Conn.connect (); InputStream is = Conn.getinputstream (); if (Conn.getresponsecode ()! = HTTPURLCONNECTION.HTTP_OK) {if (mcallback! = null) { Mcallback.onresponse (request_error, null);} Conn.disconnect (); return;} if (type = = Requesttype.get) {bufferedreader br = new BufferedReader (NEW InputStreamReader (IS)); String line = null;sb.setlength (0), while (line = Br.readline ())! = null) {sb.append (line);} if (mcallback! = null) {Mcallback.onresponse (response_success, sb.tostring ());}} else {File File = null;if (type = = requesttype.get_thumbnail) {file = Fileutil.createcachefile (string.valueof ( Requeststring.hashcode ()), FileUtil.Type.THUMBNAIL);} else if (type = = requesttype.get_image) {file = Fileutil.createcachefile (string.valueof (Requeststring.hashcode ()), FileUtil.Type.IMAGE);} else if (type = = Requesttype.get_audio) {file = Fileutil.createcachefile (string.valueof (Requeststring.hashcode ()), FileUtil.Type.AUDIO);} if (file = = null) {if (mcallback! = null) {mcallback.onresponse (io_exception_reeor, null);} return;} FileOutputStream fos = new FileOutputStream (file), byte[] buffer = new Byte[1024];int length = 0;while (length = Is.read (b Uffer))! =-1) {fos.write (buffer, 0, length);} Fos.flush (); Fos.close (); if (mcallback! = null) {mcallback.onresponse (response_success, file);} BuffeR = null; System.GC ();} Conn.disconnect (); return;} catch (Malformedurlexception e) {if (mcallback! = null) {mcallback.onresponse (url_marlformed_error, NULL);}} catch ( IOException e) {if (mcallback! = null) {mcallback.onresponse (io_exception_reeor, NULL);}} finally {if (conn! = NULL) {conn . Disconnect ();}}} else if (type = = requesttype.post_image| | type = = Requesttype.post_audio) {if (params = = null) {throw new Illegalargumente Xception ("Post request must specify arguments");}  Data Split Line string boundary = "---------------------------7de8c1a80910";//Post request try {URL url = new URL (requeststring); conn = (httpurlconnection) url.openconnection (); Conn.setconnecttimeout (connect_time_out); Conn.setdooutput (true); Conn.setdoinput (True); Conn.setusecaches (false); Conn.setreadtimeout (read_time_out); Conn.setrequestmethod ("POST" ); Conn.setrequestproperty ("Connection", "keep-alive"); Conn.setrequestproperty ("Charset", "UTF-8"); Conn.setrequestproperty ("User-agent", "mozilla/4.0" (compatible; MSIE 8.0; Windows NT 5.2;trident/4.0;. NET CLR 1.1.4322;. NET CLR 2.0.50727;. NET CLR 3.0.4506.2152;. NET CLR 3.5.30729;. net4.0c;. net4.0e) Conn.setrequestproperty ("Content-type", "multipart/form-data; boundary=" + boundary);//End data delimiter byte[] Enddata = ("\r\n--" + boundary + "--\r\n"). GetBytes ();//Data output stream DataOutputStream dos = new DataOutputStream (conn.getoutputs Tream ());//Build Post data sb.setlength (0);iterator<string> i = Params.keyset (). Iterator (); while (I.hasnext ()) { String key = I.next (), if (!key.equals ("Txtupload")) {//To fill the output stream with plain text data sb.append ("\r\n\r\n\r\n--" + boundary + "\ r \ n"); Sb.append ("Content-disposition:form-data; Name=\ "" + key + "\" "+" \r\n\r\n ") Sb.append (Params.get (key) +" \ r \ n ");d Os.write (Sb.tostring (). GetBytes ()); Sb.setlength (0);} else {//to the output stream to fill in the uploaded file data sb.append ("\r\n\r\n--" + boundary + "\ r \ n"); Sb.append ("Content-disposition:form-data; Name=\ "txtupload\"; Filename=\ "" + Params.get ("txtupload") + "\" "+" \ r \ n "), if (type = = Requesttype.post_image) {sb.append (" Content-type: Image/*\r\n\r\n ");} else {sb.append ("content-type:audio/*\r\n\r\n");} Dos.write (Sb.tostring (). GetBytes ()); sb.setlength (0);//Transfer file data FileInputStream FIS = new FileInputStream (new file Params.get ("Txtupload"))); byte[] buffer = new Byte[1024];int length = 0;while ((length = fis.read (buffer))! =-1) {Dos.wri Te (buffer, 0, length);} Fis.close ();}} Dos.write (enddata);//Refresh Output stream Dos.flush ();d os.close ();//Get Return data InputStream is = Conn.getinputstream (); if ( Conn.getresponsecode ()! = HTTPURLCONNECTION.HTTP_OK) {if (mcallback! = null) {mcallback.onresponse (request_error, NULL );} Conn.disconnect (); return;} Sb.setlength (0); BufferedReader br = new BufferedReader (new InputStreamReader (IS)); String line = null;while (line = Br.readline ())! = null) {sb.append (line);} if (mcallback! = null) {Mcallback.onresponse (response_success, sb.tostring ());} Conn.disconnect ();} catch (Malformedurlexception e) {if (mcallback! = null) {mcallback.onresponse (url_marlformed_error, NULL);}} catch ( IOException e) {if (mcallback! = null) {MCALLBACK.ONREsponse (Io_exception_reeor, NULL);}} finally {if (conn! = null) {Conn.disconnect ();}}}} /** set the network request callback. */public void Setcallback (Httpresponsecallback callback) {this.mcallback = callback;} /** Network request callback interface. */public static interface Httpresponsecallback {public void onresponse (int responsecode, Object responseobject);} public static enum RequestType {/** General GET request, the return data is a string */get,/** gets the avatar thumbnail. */get_thumbnail,/** get the picture file. */get_image,/** Get Audio text */get_audio,/** POST request, upload data for IMAGE */post_image,/** POST request, upload data for AUDIO */post_audio;}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.