iOS background file upload

Source: Internet
Author: User

Public Modelandview GetImage (HttpServletRequest request,
HttpServletResponse response) throws Exception {
Map<string, object> maps = new hashmap<string, object> ();

try {
Create a temporary file for the file to be uploaded, the first parameter is the upload file size, the second parameter is a temporary directory for the storage
Diskfileitemfactory factory = new Diskfileitemfactory (
1024x768 * 5, new File ("D:\\TEMP1"));
Set buffer size to 5M
Factory.setsizethreshold (1024 * 1024 * 5);
Create a handle to a file upload
Servletfileupload upload = new Servletfileupload (factory);

Set the entire size of the uploaded file and the individual file size of the upload
Upload.setsizemax (1024 * 1024 * 50);
Upload.setfilesizemax (1024 * 1024 * 5);

try {
Parse each form element in the page form into a Fileitem
list<fileitem> items = upload.parserequest (request);
for (Fileitem Fileitem:items) {
If it is a normal form element (type is not a form element of file)
if (Fileitem.isformfield ()) {
System.out.println (Fileitem.getfieldname ()); Get the name of the corresponding form element
System.out.println (Fileitem.getstring ()); Get the value of form element
} else {

Gets the suffix name of the file
String fileName = Fileitem.getname ();//Get the name of the file
String Fileext = filename.substring (
Filename.lastindexof (".") + 1,
Filename.length ());
String a = System.currenttimemillis () + "";
String Path = Request.getrealpath ("/") + "kmtp/"
+ "IOS" + A + ". jpg";
String url = "/http" + request.getservername () + ":"
+ request.getserverport () + "/"
+ Request.getcontextpath () + "kmtp/" + "IOS"
+ A + ". jpg";
String Extension = path.substring (
Path.lastindexof (".") + 1, path.length ());
Pattern pattern = pattern.compile ("Bmp|gif|gepg|png|");
Matcher Matcher = pattern.matcher (extension);
if (Matcher.find ()) {
try {

Fileitem.write (The new File (path));
Maps.put ("message", URL);
} catch (Exception e) {
E.printstacktrace ();
Maps.put ("message", "0");
}
} else {
System.out.println ("The file type cannot be uploaded");
Maps.put ("message", "0");
}
}
}
} catch (Fileuploadbase.sizelimitexceededexception e) {
System.out.println ("The size of the entire request exceeds the specified size ...");
Maps.put ("message", "0");
} catch (Fileuploadbase.filesizelimitexceededexception e) {
System.out.println ("The size of an uploaded file in the request exceeds the specified size ...");
Maps.put ("message", "0");
} catch (Fileuploadexception e) {
E.printstacktrace ();
Maps.put ("message", "0");
}

} catch (Exception e) {
Logger.error (E.getstacktrace ());
E.printstacktrace ();
Maps.put ("message", "0");

}
Return result 0 system error,
Response.getwriter (). Write (
Urlencoder.encode (Jsonobject.fromobject (Maps). ToString (),
"UTF-8"));
return null;
}

iOS background file upload

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.