The Code comes from the network. Make some changes...
Imported jar package
Commons-io-1.2.jar
Commons-fileupload-1.1.1.jar
Servlet type
Import Java. io. file; import Java. io. ioexception; import Java. io. printwriter; import Java. util. iterator; import Java. util. list; import Java. util. regEx. matcher; import Java. util. regEx. pattern; import javax. servlet. servletexception; import javax. servlet. HTTP. httpservlet; import javax. servlet. HTTP. httpservletrequest; import javax. servlet. HTTP. httpservletresponse; import Org. apache. commons. fileupload. fileitem; impo RT Org. apache. commons. fileupload. fileuploadexception; import Org. apache. commons. fileupload. disk. diskfileitemfactory; import Org. apache. commons. fileupload. servlet. servletfileupload;/***** @ author administrator **/public class fileuploadservlet extends httpservlet {private string uploadpath = "d :\\ addnetfile \\"; // Private file temppath = new file ("d :\addnetfile \ TMP \") used to store uploaded files; // directory P used to store temporary files Ublic void dopost (httpservletrequest req, httpservletresponse res) throws servletexception, ioexception {res. setcontenttype ("text/html; charset = gb2312"); printwriter out = res. getwriter (); system. out. println (req. getcontentlength (); system. out. println (req. getcontenttype (); diskfileitemfactory factory = new diskfileitemfactory (); factory. setsizethreshold (4096); factory. setrepository (new file ("D: \ file \ Ddnetfile \ Temp "); servletfileupload upload = new servletfileupload (factory); upload. setsizemax (1000000); try {list fileitems = upload. parserequest (req); iterator iter = fileitems. iterator (); // regular match, filtering path to get file name string Regexp = ". + \\\\(. +) {1} quot; // The filtered file type string [] errortype = {". EXE ",". com ",". cgi ",". ASP "}; pattern P = pattern. compile (Regexp); While (ITER. hasnext () {fileitem item = (fileitem) ITER. n EXT (); // ignore all other form information not in the file field if (! Item. isformfield () {string name = item. getname (); long size = item. getsize (); If (name = NULL | Name. equals ("") & size = 0) continue; matcher M = P. matcher (name); boolean result = m. find (); If (result) {for (INT temp = 0; temp <errortype. length; temp ++) {If (M. group (1 ). endswith (errortype [temp]) {Throw new ioexception (name + ": Wrong type") ;}try {// Save the uploaded file to the specified directory item. write (new file ("D: \" + M. group (1); out. print (name + "" + size + "<br>");} catch (exception e) {out. println (e) ;}} else {Throw new ioexception ("fail to upload") ;}}} catch (ioexception e) {out. println (E);} catch (fileuploadexception e) {out. println (e) ;}} public void Init () throws servletexception {}}
Web Front-end HTML page
<HTML>
You can configure web. xml by yourself. You can also select a wizard, which is very simple...