This example for you to share the JSP using Commons-fileupload to implement file upload code for your reference, the specific contents are as follows
1, Prepare:
Copy Commons-fileupload-1.1.zip and Commons-io-1.1.zip to the "\web-inf\lib" directory
2, First is Servlet:FileUpload.java
Package servlet;
Import Java.io.File;
Import java.io.IOException;
Import Java.io.PrintWriter;
Import java.util.*;
Import Java.util.regex.Pattern;
Import Java.util.regex.Matcher;
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;
Import org.apache.commons.fileupload.FileUploadException;
Import Org.apache.commons.fileupload.disk.DiskFileItemFactory;
Import Org.apache.commons.fileupload.servlet.ServletFileUpload; public class FileUpload extends HttpServlet {private String uploadpath= "e:\\addnetfile\\";//directories to upload files private file tem Ppath=new file ("e:\\tempfile\\");//directory of uploaded files public void doget (HttpServletRequest request, HttpServletResponse Response) throws Servletexception, IOException {} public void DoPost (HttpServletRequest request, HttpServletResponse Response) throws Servletexception, IOException {RESponse.setcontenttype ("text/html;charset=gb2312");
Response.setcharacterencoding ("gb2312");
PrintWriter Out=response.getwriter ();
Out.println ("The length of the request content is:" +request.getcontentlength ());
Out.println ("The type of request content is:" +request.getcontenttype ());
Diskfileitemfactory factory=new diskfileitemfactory ();
Factory.setrepository (TempPath);
Factory.setsizethreshold (4096);
Servletfileupload upload=new servletfileupload (Factory);
Upload.setsizemax (1000000);
List<?> Fileitems=null;
try{fileitems=upload.parserequest (Request);
Iterator<?> Iterator=fileitems.iterator ();
String regex= ". +\\\\ (. +) $";
String[] errortype={". exe", ". com", ". CGI", ". asp"};
Pattern P=pattern.compile (regex);
while (Iterator.hasnext ()) {Fileitem item= (Fileitem) iterator.next ();
if (!item.isformfield ()) {String name=item.getname ();
Long Size=item.getsize (); if (name==null| |
Name.equals ("") &&size==0) continue;
Matcher M=p.matcher (name); if (M.find ()) {for (int temp=0;temp<errortype.length;temp++) {if (M.group (1). EndsWith (errortype[temp)) throw new IOException (name+ ": Wrong type");
} try{Item.write (New File (Temppath,m.group (1)));
Out.println (name+ "" +size+ "<br/>");
Out.println ("Upload success");
}catch (Exception e) {out.println ("333" +e);
} else{throw new IOException ("fail to upload");
}}}catch (IOException e) {out.println ("222" +e);
catch (Fileuploadexception E1) {e1.printstacktrace ();
Out.println ("+e1"); } public void Init () throws Servletexception {if (!new file (Uploadpath). Isdirectory ()) New File (Uploadpath). mkdir (
);
if (!temppath.isdirectory ()) Temppath.mkdir ();
public void Destroy () {Super.destroy ();
}
}
3, followed by html:Uploadfile.html
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
4, finally is the configuration Web.xml
<servlet> <description>this is the description's my
java ee component</description>
< Display-name>this is, the display name of my Java EE component</display-name>
<servlet-name>fileupload </servlet-name>
<servlet-class>servlet. fileupload</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name> Fileupload</servlet-name>
<url-pattern>/servlet/FileUpload</url-pattern>
First run Html,servlet process upload request
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.