As follows:
Code:
Note:Jar package required: commons-fileupload-1.2.1.jar and commons-io-1.4.jar
Index. jsp
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"> <HTML>
upload.jsp
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"> <HTML>
Main Code in upload. Java:
Public class upload extends httpservlet {private string uploadpath = "AA/upload/"; // Private string temppath = "AA/uploadtmp /"; // temporary file directory private string serverpath = NULL; private int sizemax = 3; // maximum size of the image private string [] filetype = new string [] {". jpg ",". GIF ",". BMP ",". PNG ",". JPEG ",". ICO "}; Public void dopost (httpservletrequest request, httpservletresponse response) throws servletexception, Ioexception {response. setcontenttype ("text/html"); response. setcharacterencoding ("UTF-8"); // sets the encoding. The returned Chinese garbled serverpath = getservletcontext (). getrealpath ("/"). replace ("\", "/"); // It is executed during servlet initialization. If the Upload File directory does not exist, if (! New file (serverpath + uploadpath). isdirectory () {New file (serverpath + uploadpath). mkdirs () ;}if (! New file (serverpath + temppath ). isdirectory () {New file (serverpath + temppath ). mkdirs ();} diskfileitemfactory factory = new diskfileitemfactory (); factory. setsizethreshold (5*1024); // Maximum Cache factory. setrepository (new file (serverpath + temppath); // temporary file directory servletfileupload upload = new servletfileupload (factory); upload. setsizemax (sizemax * 1024*1024); // maximum file size: String filepath = NULL; try {list <fileitem> ite MS = upload. parserequest (request); // get all file lists for (fileitem item: Items) {// get the file name, which includes the path if (! Item. isformfield () {// file name string filename = item. getname (). tolowercase (); If (filename. endswith (filetype [0]) | filename. endswith (filetype [1]) | filename. endswith (filetype [2]) | filename. endswith (filetype [3]) | filename. endswith (filetype [4]) | filename. endswith (filetype [5]) {string UUID = UUID. randomuuid (). tostring (); filepath = serverpath + uploadpath + UUID + filename. substring (filename. lastindexof (". "); I TEM. write (new file (filepath); printwriter PW = response. getwriter (); PW. write ("<SCRIPT> alert ('upload successful '); window. returnvalue = '"+ uploadpath + UUID + filename. substring (filename. lastindexof (". ") +" '; window. close (); </SCRIPT> "); PW. flush (); PW. close ();} else {request. setattribute ("errormsg", "Upload Failed. Make sure the uploaded file exists and belongs to an image type! "); Request. getrequestdispatcher ("/upload. JSP "). forward (request, response) ;}}} catch (exception e) {e. printstacktrace (); Request. setattribute ("errormsg", "Upload Failed. Make sure the size of the uploaded file cannot exceed" + sizemax + "M"); Request. getrequestdispatcher ("/upload. JSP "). forward (request, response );}}}
<p style="color: red">${requestScope.errorMsg}</p>
Indicates the scope of the variable. There are four types.
Pagination: indicates that the variable can only be used on this page.
Requestscope: indicates that the variable can be used in this request.
Sessionscope: indicates that the variable can be used in this session.
Applicationscope: indicates that variables can be used throughout the application.
Common-fileupload: upload images and display images