Originally wanted to write a controller, the result of the interceptor stopped the picture, then the direct servlet
public class Uploadeamge extends httpservlet{
/**
*
*/
Private static final long serialversionuid = 1L;
private static final string[] Image_type = new string[] {". bmp", ". jpeg", ". jpg", ". gif", ". png"};
public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
This.dopost (request, response);
}
public void DoPost (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Verify picture format
Diskfileitemfactory factory = new Diskfileitemfactory ();
String uploadtempdir= "e:/123";
Factory.setrepository (New File (Uploadtempdir));
Servletfileupload upload = new Servletfileupload (factory);
Upload.setheaderencoding ("Utf-8");
List<fileitem> fileList;
try {
FileList = upload.parserequest (request);
Response.setcharacterencoding ("Utf-8");
String Uuidname=null;
Iterator<fileitem> it= filelist.iterator ();
String filepath= "e:/123";
File NewFile = new file (FilePath);
if (!newfile.exists ()) {
Newfile.mkdirs ();//E:\all_apaches\apache-tomcat-6.0.32\apache-tomcat-6.0.32\webapps\day09_upload\img
}
while (It.hasnext ()) {
Fileitem item = It.next ();
if (!item.isformfield ()) {
String name = Item.getname ();
Long size = Item.getsize ();
if (name = = NULL | | Name.trim (). Equals ("")) {
Continue
}
try {
if (Size > 716800) {
Response.getwriter ()
. Print ("{\" returnmessage\: \ "Picture size cannot be greater than 700k.\", \ "returncode\": \ "1024\", \ "imgurl\": \ "\"} ");
}
} catch (Exception e) {
E.getstacktrace ();
}
Boolean islegal = false;
for (String Type:image_type) {
if (Stringutils.endswithignorecase (Item.getname (), type)) {
Islegal = true;
System.out.println (Islegal);
Break
}
System.out.println (Islegal);
}
try {
Uuidname=generaterandonfilename (Item.getname ());
Item.write (New File (NewFile, uuidname));
Item.delete ();
} catch (Exception e) {
E.printstacktrace ();
throw new RuntimeException ();
}
}
String url = "http://image.brand.com" + "/" +uuidname;
System.out.println (URL);
Response.getwriter (). Print (
"{\" returnmessage\ ": \" \ ", \" returncode\ ": \" 0000\ ", \" imgurl\ ": \" "
+ URL + "\"} ");
}
}catch (Exception e) {
E.getstacktrace ();
}
}
public static string Generaterandonfilename (String fileName) {
String ext = filename.substring (Filename.lastindexof ("."));
Return Uuid.randomuuid (). toString () + ext;
}
}
An image upload servlet, to the local disk, to upload to the server, please modify