This is a servlet application.
First, Web.xml .
[XHTML] View Plain copy print? <?xml version= "1.0" encoding= "UTF-8?> <web-app version=" 2.5 " xmlns=" Http://java.sun.com/xml/ns/javaee " xmlns:xsi=" http://www.w3.org/2001/ Xmlschema-instance " xsi:schemalocation=" Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "> <servlet> < servlet-name>uploadservelt</servlet-name> <servlet-class>com.jadyer.servlet.UploadServelt</servlet-class> <!-- Specify the uploaded files, and save them in the hard drive after the upload is successful --> <init-param> <param-name>savePath</param-name> <param-value>D://mydata//upload</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>UploadServelt</servlet-name> <url-pattern>/servlet/UploadServelt</url-pattern> </servlet-mapping> < welcome-file-list> <welcome-file> upload.jsp</welcome-file> </welcome-file-list> </ web-app> <!-- had a net friend took the Cos with commons-fileupload and jspsmartupload test comparison, the test process is as follows upload 2MB, 20MB, 45MB, 200MB of the file respectively, and test three components of the time, the test results are as follows file less than 2MB, cos and fileupload upload speed is similar, but Jspsmartupload has shown weakness with the increase of the file, FileUpload and smartupload upload speed is significantly inferior to cos when file 200MB, Smartupload has been overwhelmed and crashed And now Cos takes less than fileupload more than 20 seconds no doubt, the test results, COS performance, well-deserved ranked first --> <?xml Version= "1.0" encoding= "UTF-8"?> <web-app version= "2.5" xmlns= "Http://java.sun.com/xml/ns/javaee" Http://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" Http://java.sun.com/xml/ns/javaee http:// Java.sun.com/xml/ns/javaee/web-app_2_5.xsd "> <servlet> <servlet-name>uploadservelt</ Servlet-name> <servlet-class>com.jadyer.servlet.UploadServelt</servlet-class> <!--Specify the uploaded files, The location saved on the hard drive after the upload is successful--> <init-param> <param-name>savePath</param-name> <param-value>d:// Mydata//upload</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>UploadServelt</servlet-name> < Url-pattern>/servlet/uploadservelt</url-pattern> </servlet-mapping> <welcome-file-list> < Welcome-file>upload.jsp</welcome-file> </welcome-file-list> </web-app> <!-- Have a net friend take Cos with Commons-fileupload and jspsmartupload to carry on the experiment comparison, the test process follows respectively uploads 2MB, 20MB, 45MB, 200MB the file, and tests three kinds of components the time, the test result following file is less than 2MB, Cos and fileupload upload speed is similar, but Jspsmartupload has shown weakness with the file increase, fileupload and smartupload upload speed is obviously inferior to cos when file 200MB, Smartupload already overwhelmed and collapsed and now Cos takes less than fileupload more than 20 seconds. No doubt, the test results, COS performance, well-deserved ranked first-->
The upload.jsp page is then used to collect the uploaded file information.
[XHTML] View Plain copy print? <%@ page language= "java" pageencoding= "UTF-8"%> <form Action= "<%=request.getcontextpath ()%>/servlet/uploadservelt" enctype= "Multipart/form-data" Method= "POST" > User: <input type= "text" name= "username" > <br/> Password: <input type= "password" name= "password" >< br/> Sex: <input type= "Radio" name= "Sex" value= "woman" checked> Women <input type= "Radio" name= "Sex" value= "man" > <br/> Preferences:< input type= "checkbox" name= "like" value= "sleeping" > Sleep <input type= "checkbox" name= "like" value= "coding" &NBsp;checked> Write code <input type= "checkbox" name= "like" value= "eat" > Dinner <br/> Attachment: <input type= "file" name= "File11" ><br/> accessories:< Input type= "File" name= "File22" ><br/> attachment:<input Type= "File" name= "file33" ><br/> <input type= "Submit" value= "I want to upload" > </form> <%@ page Language= "java" pageencoding= "UTF-8"%> <form action= "<%=request.getcontextpath ()%>/servlet/ Uploadservelt "enctype=" Multipart/form-data "method=" POST "> User: <input type=" text "name=" username "><br/ > Password: <input type= "password" name= "password" ><br/> sex: <input type= "Radio" name= "Sex" value= "woman" checked> female &NBSP;&NBSp <input type= "Radio" name= "Sex" value= "man" > <br/> preferences: <input type= "checkbox" Name= "like" value= "sleep" > Sleeping <input type= "checkbox" Name= "like" value= "coding" checked> write code <input type= " CheckBox "Name=" "like" value= "eat" > Dinner <br/> Accessories: <input type= "file" Name= "File11" ><br/> attachment:< Input type= "file" Name= "File22" ><br/> accessories: <input type= "file" Name= "file33" ><br/> <input type= " Submit "value=" I want to upload > </form>
Next is a custom named policy class for resolving file name conflicts for uploads
[Java] View Plain copy print? package com.jadyer.util; import java.io.file; import java.util.date; import com.oreilly.servlet.multipart.filerenamepolicy; /** * Custom named policy file * @see To resolve conflicting issues with the same name as uploaded files * @ see workaround: FileName + timestamp + file suffix */ public class randomfilenamepolicy implements filerenamepolicy { public file rename (file file) { /* * assume that we uploaded a file with the same name named "ABCD.CCC" */ Int index = file.getname (). LastIndexOf ("."); //gets the subscript &n of "." In the filename.Bsp; string body = file.getname (). substring (0, index); / /represents the subject of the filename, that is, "ABCD" String postfix = " //the suffix of the filename, that is, ". CCC" string timer = ""; //a number representing the current system time //If there is no "." In the name of the file, then LastIndexOf (".") will return-1 if (index != -1) { timer = new date ( ). GetTime () + ""; //in front of the file name, add a number that represents the current system time postfix = file.getname (). substring (index); //get to the file name of ". CCC " } else { timer = new date () getTime () + ""; postfix = " //if LastIndexOf (". ") Returns-1, stating that there is no "." In the filename (no suffix } String newname = body + timer + postfix; //constructs a new filename return new file (File.getparent (), newname); //returns the renamed file } } Package com.jadyer.util; Import Java.io.File; Import Java.util.Date; Import Com.oreilly.servlet.multipart.FileRenamePolicy; /** * Custom named Policy file * @see To resolve conflicting issues with the same name for uploaded files * @see Workaround: FileName + timestamp + file suffix/public class Randomfilenamepolicy implements Filerenamepolicy {Public file rename (file file) {/* * assumption, We uploaded a file with the same name named "ABCD.CCC" */INT index = File.getname (). LastIndexOf ("."); Gets the subscript String BODY = file.getname () of "." in the filename. substring (0, index); Represents the subject of a filename, that is, "ABCD" String postfix = ""; Represents the suffix of a filename, that is, ". CCC" String timer = ""; Number that represents the current system time//If the name of the file is not ".", then LastIndexOf (".") Returns-1 if (index!=-1) {timer = new Date (). GetTime () + ""; Before the name of the file, add the number postfix = File.getname (), which represents the current system time. substring (index); Gets the ". CCC"} else {timer = new Date () in the filename. GetTime () + ""; Postfix = ""; If LastIndexOf (".") Returns-1, stating that there is no "." In the filename, that is, no suffix} String newName = body + timer + postfix; Constructs a new filename return (File.getparent (), newName); Returns the renamed file}}
Finally, the servlet that handles the core operations of file uploads
[Java] View Plain copy print? package com.jadyer.servlet; import java.io.file; import java.io.ioexception; import java.util.enumeration; import javax.servlet.servletconfig; import javax.servlet.servletexception; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import com.jadyer.util.randomfilenamepolicy; import com.oreilly.servlet.multipartrequest; /** * You will need to introduce cos.jar * via http://www.servlets.com/cos/in this web project. Cos-26dec2008.zip Downloads */ public class uploadservelt extends httpservlet { private static final long serialversionuid = -7946856825034537432l; private string savePath; public void init (servletconfig Config) { //Gets the value of the Savepath parameter configured in Web.xml savepath = config.getinitparameter ("SavePath") ; } public void DoPost (Httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { // Specify uploaded files, upload success, save location on hard drive string savedirectory = this.savePath; //if the folder you specified does not exist on your hard disk, create the folder file filedirectory = new