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