Implement the attachment upload of the page with the package Commons-fileupload-1.2.jar

Source: Internet
Author: User
Tags temporary file storage

Environment: The action for form in the client page is this servlet, introducing the package Commons-fileupload-1.2.jar

The servlet class Fileuploadservlet.java code used is as follows:

Import Java.io.File;
Import java.io.IOException;
Import Java.io.PrintWriter;
Import Java.util.Iterator;
Import java.util.List;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import Org.apache.commons.fileupload.DiskFileUpload;
Import Org.apache.commons.fileupload.FileItem;

public class Fileuploadservlet extends HttpServlet {

/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet Request
* @param response servlet response
* @throws servletexception If a servlet-specific error occurs
* @throws IOException If an I/O error occurs
*/
protected void ProcessRequest (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Response.setcontenttype ("Text/html;charset=utf-8");
PrintWriter out = Response.getwriter ();
String dir = request.getsession (). Getservletcontext (). Getrealpath ("/");
System.out.println ("dir:" + dir);
try {
Diskfileupload fu = new Diskfileupload ();//--key classes used in uploading attachments

Fu.setsizemax (1024 * 1024 * 20);//--upload the maximum value of the attachment
Fu.setsizethreshold (1024 * 1024 * 5);//--Memory Size
Fu.setrepositorypath (System.getproperty ("Java.io.tmpdir"))//--temporary file storage path when the uploaded attachment size is greater than the given memory size
Fu.setheaderencoding ("UTF-8");

try {

List Fileitem = fu.parserequest (request);
Iterator it = Fileitem.iterator ();
while (It.hasnext ()) {
Fileitem item = (Fileitem) it.next ();
if (!item.isformfield () && null!= item.getname () &&
0!= item.getname (). Trim (). Length ()) {//--Judge is not an ordinary form page object, and upload an attachment
String Clientpath = Item.getname ();
String fileName = new File (Clientpath). GetName ();

File Destdir = new file ("d://");//--storage path on server

File File = new file (Destdir, fileName);
Item.write (file);
System.out.println ("======== file uploaded successfully");
}
}
catch (Exception e) {
String str = "File upload exception, error message:" + e.getmessage ();
System.out.println (str);
E.printstacktrace ();
}
catch (Exception e) {
E.printstacktrace ();
}
}

   //<editor-fold defaultstate= "collapsed" desc= "HttpServlet method. Click the + sign on the left to edit the code.
   /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet Request
     * @param response servlet response
     * @throws servletexception If a servlet-specific error occurs
     * @throws IOException If an I/O error occurs
     */
    @Override
 & nbsp;  protected void doget (HttpServletRequest request, httpservletresponse response)
    Throws Servletexception, IOException {
        processrequest (Request, Response);
   }

   /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet Request
     * @param response servlet response
     * @throws servletexception If a servlet-specific error occurs
     * @throws IOException If an I/O error occurs
     */
    @Override
 & nbsp;  protected void DoPost (HttpServletRequest request, httpservletresponse response)
    Throws Servletexception, IOException {
        processrequest (Request, Response);
   }

/**
* Returns A short description of the servlet.
* @return a String containing servlet description
*/
@Override
Public String Getservletinfo () {
Return "Short description";
}

}

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.