The control layer for uploading files
Content is for reference, the following code is the result of successful operation. Mo copy, refuse to do only the theory emperor, and strive to become a manual king, master Please leave a message, let everyone learn progress, expand my greater China programmer ...
Package com. Yy. Upload.servlet;
Import Java.io.File;
Import java.io.IOException;
Import java.util.*;
Import Javax.servlet.ServletConfig;
Import Javax.servlet.ServletContext;
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;
Import Com.actionsoft.Util.CreateFileUtil;
public class Fileuploadservlet extends HttpServlet {
Private static final long serialversionuid = -7744625344830285257l;
Private ServletContext SC;
Private String Savepath;
public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
DoPost (request, response);
}
public void init (ServletConfig config) {
An initialization parameter set in the Web.xml
Savepath = Config.getinitparameter ("Savepath");
sc = Config.getservletcontext ();
}
public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
Request.setcharacterencoding ("UTF-8");
Diskfileitemfactory factory = new Diskfileitemfactory ();
Servletfileupload upload = new Servletfileupload (factory);
try {
List items = upload.parserequest (request);
Iterator ITR = Items.iterator ();
while (Itr.hasnext ()) {
Fileitem item = (Fileitem) itr.next ();
if (Item.isformfield ()) {
System.out.println ("Form parameter name:" + item.getfieldname () + ", form parameter value:" + item.getstring ("UTF-8"));
} else {
if (item.getname ()!= null &&!item.getname (). Equals ("")) {
int kb= (int) (Item.getsize ()/1024);
System.out.println ("Upload file Size:" + kb+ "KB");
System.out.println ("Type of File uploaded:" + Item.getcontenttype ());
Item.getname () returns the full path name of the uploaded file on the client
System.out.println ("Upload file name:" + item.getname ());
File Tempfile = new file (Item.getname ());
Save path to upload file
String dirname = "E:/temp/sqlfile";
File dir = new file (Sc.getrealpath ("/") + Savepath);
if (dir.exists ()) {
File File = new file (Sc.getrealpath ("/") + Savepath, Tempfile.getname ());
Item.write (file);
}else{
Createfileutil.createdir (Sc.getrealpath ("/") + Savepath);
File File = new file (Sc.getrealpath ("/") + Savepath, Tempfile.getname ());
Item.write (file);
}
Request.setattribute ("Upload.message", Tempfile.getname () + "uploaded successfully." "+kb+" KB ");
}else{
Request.setattribute ("Upload.message", "no upload file selected.") ");
}
}
}
}catch (Fileuploadexception e) {
E.printstacktrace ();
catch (Exception e) {
E.printstacktrace ();
Request.setattribute ("Upload.message", "Upload file failed.") ");
}
Request.getrequestdispatcher ("/uploadresult.jsp"). Forward (request, response);
}
}