Dopost of uploaded files ()

Source: Internet
Author: User

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);
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.