In the implementation of Multipart/form-data image upload, you need to use \ r \ n to separate each line, in Java implementation of Multipart/form-data image upload can use System.getproperty (" Line.separator ") to split each line. But if you're using code for Android, remember that you can't use System.getproperty ("Line.separator") to split each line, because in Android System.getproperty (" Line.separator "), the result is \ n, in Java it is \ r \ n.
It is not recommended that you use System.getproperty ("Line.separator"), there will be some unexpected problems, and these problems are not easy to trace back to System.getproperty ("Line.separator") Use up.
The following is affixed to the use of Java and Android upload code, the code is for NIMG implementation, if you need to use with other environments, change the "Upload parameters" code for the environment.
Uploadimage.java
Package cloud.test;import java.io.bufferedreader;import java.io.bytearrayoutputstream;import java.io.DataOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.ioexception;import java.io.inputstreamreader;import java.net.httpurlconnection;import java.net.url;import java.util.uuid;/** * image upload * @author cloud * @ version 1.0 */public class uploadimage { private static Final string multipart_form_data= "Multipart/form-data"; private static final string twohyphens = "--"; private static final String BOUNDARY = "---------------------------" +uuid.randomuuid (); private static final String LINEEND = "\ r \ n"; Private static final string&nBsp Formname= "UserFile"; private string actionurl; private int timeout; private String filename; private byte[] data; private string imagetype; /** * Initialize image upload Tool * @param actionUrl - image storage Address * @param timeout - Picture upload time-out (milliseconds) */ public uploadimage (string actionurl,int timeout) { this.actionUrl=actionUrl; this.timeout=timeout; } /** * Set upload content * @param fileName - picture name * @param data - picture content * @param imageType - picture formats */ public void setdata (string filename,byte[] Data,imagetype imagetype) throws Exception{ this.filename=filename; this.data=data; this.imagetype=imagetype.getvalue (); if (imagetype!=null) { string extension =imagetype.getvalue (). substring (Imagetype.getvalue (). IndexOf ("/") +1, Imagetype.getvalue (). Length ()); this.filename= Filename+ "." +extension; } } /** * upload * @return - format <br/> * {<br/> * & nbsp; " Code ": 200,<br/> * " Msg ": " upload success! ",<br/> * & nbsp; " Data ": {<br/> *   ; " T ": " PNG ",<br/> * & nbsp; " UserPath ": ",<br/> * " MD5 ": " 6F8B47EC3B8EA08335CB6E13CBBE96DC ",<br/> * & nbsp; " URL ": " 01/6f8b47ec3b8ea08335cb6e13cbbe96dc.png "<br/> * } <br/> * }<br/> */ public string upload () { return upload (null,null) ; } /** * upload * @param actType - image processing commands * @param param - Image processing Parameters * @return - Formats <br/> * {<br/> * " Code ": 200,<br/> * " Msg ": " upload success! ",<br/> * & nbsp; " Data ": {<br/> *   ; " T ": " PNG ",<br/> * & nbsp; " UserPath ": ",<br/> * " MD5 ": " 6F8B47EC3B8EA08335CB6E13CBBE96DC ",<br/> * & nbsp; " URL ": " 01/6f8b47ec3b8ea08335cb6e13cbbe96dc.png "<br/> * } <br/> * }<br/> */ public string upload (Acttype acttype,string param) { HttpURLConnection conn = null; Dataoutputstream output = null; bufferedreader input = null; try { url url = new url (ActionURL); conn = (httpurlconnection) url.openconnection (); conn.setconnecttimeout (timeout); conn.setdoinput (true); &nbsP; conn.setdooutput (True); conn.setusecaches (False); conn.setrequestmethod ("POST"); conn.setrequestproperty ("Connection", "keep-alive"); conn.setrequestproperty ("Content-Type", multipart_form_data + "; boundary=" + boundary); conn.connect (); output = new dataoutputstream (Conn.getoutputstream ()); StringBuilder Sb = new stringbuilder (); if (acttype!=null&¶m!=null) { /** * Upload parameter */ sb.append (TWOHYPHENS + boundary + lineend); sb.append ("content-disposition: form-data; name=\" act\ "" + lineend); sb.append (LINEEND); sb.append (ActType.getValue () + lineend); &Nbsp; output.writebytes (Sb.toString ()); sb = new stringbuilder (); sb.append (TWOHYPHENS + BOUNDARY + lineend); Sb.append ("content-disposition: form-data; name=\" param\ "" + lineend); sb.append (LINEEND); sb.append (param + lineend); output.writebytes (Sb.toString ()); } /** * Upload Images */ sb = new stringbuilder (); sb.append (Twohyphens + boundary + lineend) ; sb.append ("content-disposition: form-data; name=\ " + FORMNAME + " \ "; filename=\" " + fileName + " \ "" + lineend); sb.append ("Content-Type: " + imagetype + lineend); sb.append (lineend); output.writebytes (Sb.tostring ()); output.write (data, 0, data.length); output.writebytes (lineend); /** * End of upload */ output.writebytes (TWOHYPHENS + boundary + twohyphens + lineend); output.flush (); /** * return information */ int code = conn.getresponsecode (); &NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN (Code); if (code != 200) { throw new runtimeexception ("Request" + actionUrl + "' Failed!") "); } input = new bufferedreader (New inputstreamreader ( Conn.getinputstream ())); stringbuilder Response = new stringbuilder (); string oneline; while ((Oneline = input.readline ()) != null) { Response.append (oneline + lineend); } return response.tostring (); } catch (ioexception e) { throw new runtimeexception (e); } finally { try { if (output != null) { &nBsp; output.close (); } if (input != null) { input.close (); } } catch (ioexception e) { throw new RuntimeException (e); } if (conn != null) { &nBsp; conn.disconnect (); } } }}
Acttype.java
Package cloud.test;/** * Image upload Command * @author cloud * @version 1.0 */public enum Acttype {/** * rotate */ROTATE ("Rotat E "),/** * crop */CROP (" CROP "),/** * Zoom */RESIZE (" RESIZE "); private String value; Private Acttype (String value) {this.value=value; } public String GetValue () {return value; }}
Imagetype.java
package cloud.test;/** * picture format * @author Cloud * @version 1.0 */public enum imagetype { /** * jpg format */ jpg ("image/jpg"), /** * PNG format */ png ("Image/png"), /** * gif format */ gif ("Image/gif"), /** * jpeg format */ jpeg ("Image/jpeg"); private String value; private ImageType (String value) { this.value=value; } public string getvalue () { return value; }}
Source code, jar package, description document Http://pan.baidu.com/s/1ntxMvBb
System.getproperty ("Line.separator") precautions