Upload iOS files to Springmvc

Source: Internet
Author: User

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

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.