An image upload servlet, to the local disk, to upload to the server, please modify

Source: Internet
Author: User

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

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.