Package Com.newegg.shopping.util.servlet;import Java.io.ioexception;import Java.io.printwriter;import Javax.servlet.servletconfig;import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.HttpServletResponse; Import Org.apache.commons.fileupload.*;import java.util.*;import Java.util.regex.*;import Java.io.*;import Org.apache.commons.fileupload.servlet.*;import Org.apache.commons.fileupload.disk.diskfileitemfactory;public Class FileUpload extends HttpServlet {//blob@overridepublic void init (servletconfig config) throws servletexception {thi s.config = config;} Private ServletConfig config = null;//private String uploadpath= "d:\\";//private file TempPath = new file ("D:\\upload\\te Mp\\ "); The directory used to hold temporary files private file TempPath = new file ("d:\\temp\\");p ublic Void Destroy () {config = Null;super.destroy ();} public void DoPost (HttpServletRequest req, httpservletresponse Res) throws Servletexception, Ioexception {int id =-1; String Uploadpath = Config.getinitparameter ("Uploadpath"); The directory used to store the uploaded files Res.setcontenttype ("text/html; Charset=utf-8 "); PrintWriter out = Res.getwriter (); System.out.println (Req.getcontentlength ()); System.out.println (Req.getcontenttype ());D iskfileitemfactory factory = new Diskfileitemfactory ();//maximum size that Would be stored in memoryfactory.setsizethreshold (4096);//The location for saving data, is larger than Getsizethreshol D ()//factory.setrepository (TempPath); factory.setrepository (TempPath);//factory.setrepository ("D:\\temp \\")); Servletfileupload upload = new Servletfileupload (factory);//maximum size before a fileuploadexception would be Thrownuploa D.setsizemax (1000000); try {List fileitems = upload.parserequest (req);//Assume we know there are and files. The first file is a small//text file, the second are unknown and is written to a file on//the serveriterator iter = Filei Tems.iterator ();//Regular match, filter path take file name string regExp = ". +\\\\ (. +) $";//filter out the file type StrinG[] ErrorType = {". exe", ". com", ". CGI", ". JSP"}; Pattern p = pattern.compile (REGEXP), while (Iter.hasnext ()) {Fileitem item = (Fileitem) iter.next (); if (Item.isformfield ( ) {if (Item.getfieldname (). Equals ("id")) {id = integer.parseint (item.getstring ());}} Ignores all other form information that is not a 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//when uploading the file to the database below, the Item.write (new file (Uploadpath + ID + ". jpg") will be overwritten here,//item.write ("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);}}
Windows Live Writer test