iOS code:
-(void) Sendimg:img: (UIImage *) image{
Request Address
nsmutablestring *url = [[Nsmutablestring alloc] init];
[url Appendstring:[utiltool gethosturl]];
[url appendstring:@ "Savepic"];
NSString *twitterfon_form_boundary = @ "aab03x";
Dividing line--aab03x
NSString *mpboundary=[[nsstring alloc]initwithformat:@ "--%@", twitterfon_form_boundary];
Terminator aab03x--
NSString *endmpboundary=[[nsstring alloc]initwithformat:@ "%@--", mpboundary];
nsmutablestring *body = [[Nsmutablestring alloc] init];
[Body appendformat:@ "%@\r\n", mpboundary];
Request parameters
[Body appendformat:@ "content-disposition:form-data;name=\"%@\ "\r\n\r\n", @ "token"];
Parameter values
[Body appendformat:@ "%@\r\n", [Utiltool GetToken]];
NSData *imagedata = uiimagepngrepresentation ([Utiltool changeimg:image max:1136]);
Declare Myrequestdata, which is used to put the HTTP body
Nsmutabledata *myrequestdata;
Converts the body string to a binary in the UTF8 format
Myrequestdata=[nsmutabledata data];
Uploading files
[Body appendformat:@ "%@\r\n", mpboundary];
[Body appendformat:@ "content-disposition:form-data; name=\" uploadfile\ "; filename=\"%@\ "\ r \ n", @ "Temp.png"];
[Body appendformat:@ "content-type:image/png\r\n\r\n"];
[Myrequestdata appenddata:[body datausingencoding:nsutf8stringencoding];
[Myrequestdata Appenddata:imagedata];
Declaration Terminator:--aab03x--
NSString *end=[[nsstring alloc]initwithformat:@ "\r\n%@", endmpboundary];
Join Terminator--aab03x--
[Myrequestdata appenddata:[end datausingencoding:nsutf8stringencoding];
Nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl: [Nsurl Urlwithstring:url]];
[Request Setcachepolicy:nsurlrequestreloadignoringlocalcachedata];
[Request Settimeoutinterval:[datastore Gethttptimeout];
[Request sethttpmethod:@ "POST"];
Set the value of Content-type in Httpheader
NSString *cttype=[[nsstring alloc]initwithformat:@ "multipart/form-data; boundary=%@", TWITTERFON_FORM_BOUNDARY];
Set Httpheader
[Request Setvalue:cttype forhttpheaderfield:@ "Content-type"];
Set Content-length
[Request setvalue:[nsstring stringwithformat:@ "%ld", [myrequestdata length]] forhttpheaderfield:@ "content-length"];
[Request Sethttpbody:myrequestdata];
[Nsurlconnection sendsynchronousrequest:request Returningresponse:nil Error:nil];
}
Background code:
@RequestMapping (value = {"/savepic"}, method = Requestmethod.post, produces= {"Application/json"})
@ResponseBody
Public String savepic (multipartfile uploadfile, httpservletrequest request) throws IOException, jsonexception{
Jsonobject object=new jsonobject ();
if (uploadfile! = null) {//upload file
String path = Request.getsession (). Getservletcontext (). Getrealpath ("FilePath");
String fileName = Uploadfile.getoriginalfilename ();
String FileType = filename.substring (Filename.lastindexof (".")). toLowerCase ();
File tofile = null;
File TargetFile = new file (fileName);
Modify the file name, format the current time
ToFile = new File (Path, Datevaildator.simpledateformat (new Date (), "YYYYMMDDHHMMSS") + FileType);
Targetfile.renameto (ToFile);
if (!tofile.exists ()) {
Tofile.mkdirs ();
}
Save to our server
Uploadfile.transferto (ToFile);
try {
String downloadurl= "filepath/" +tofile.getname ();
Object.put (Constants.result_status, constants.result_status_error);
Object.put (Constants.result_msg, "Save Failed");
} catch (Exception e) {
E.printstacktrace ();
Object.put (Constants.result_status, constants.result_status_success);
Object.put (constants.result_msg, DownloadURL);
}
Tofile.delete ();
}
return object.tostring ();
}
Upload iOS files to Springmvc