Upload files with JSP and servlet __JS

Source: Internet
Author: User
Tags mkdir

The most recent project encountered the problem of importing Excel files and saving the data in the file to a database, so I practiced the file upload.

One, page upload.jsp file

<body>
<form name= "Upform" action= "Uploadservlet" method= "POST" enctype= "Multipart/form-data" >
<input type = "File" name= "File1" id= "File1"/><br/>
<input type = "File" name= "File2" id= "File2"/><br/>
<input type = "File" name= "File3" id= "File3"/><br/>
<input type= "Submit" value= "Submit"/><br/>
<input type= "Reset"/>
</form>
</body>

Second, servlet file Uploadservlet.java

Import Java.io.BufferedInputStream;
Import Java.io.BufferedOutputStream;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import Org.apache.commons.fileupload.DefaultFileItemFactory;
Import Org.apache.commons.fileupload.FileItemFactory;
Import Org.apache.commons.fileupload.FileItemIterator;
Import Org.apache.commons.fileupload.FileItemStream;
Import Org.apache.commons.fileupload.disk.DiskFileItemFactory;
Import Org.apache.commons.fileupload.servlet.ServletFileUpload;
Import Org.apache.commons.fileupload.util.Streams;

public class Uploadservlet extends Javax.servlet.http.HttpServlet implements Javax.servlet.Servlet {
File Tmpdir = null;//initialization of the uploaded file temporary storage directory
File Savedir = null;//The saved directory after initializing the uploaded file
Public Uploadservlet () {super (); }


protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
DoPost (Request,response);
}
public void Init () throws Servletexception {
Super.init ();
String Tmppath = "C:\\tmpdir";
String Savepath = "C:\\updir";
Tmpdir = new File (Tmppath);
Savedir = new File (Savepath);
if (!tmpdir.isdirectory ())
Tmpdir.mkdir ();
if (!savedir.isdirectory ())
Savedir.mkdir ();
}

protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
try{
if (servletfileupload.ismultipartcontent (request)) {
Diskfileitemfactory DFF = new Diskfileitemfactory ();//Create the object
Dff.setrepository (tmpdir);//Specify a temporary directory for uploaded files
Dff.setsizethreshold (1024000);//Specify Cache data size in memory, in bytes
Servletfileupload SFU = new Servletfileupload (DFF);//Create the object
Sfu.setfilesizemax (5000000);//Specify the maximum size of a single upload file
Sfu.setsizemax (10000000);//Specify the total size of multiple files uploaded at one time
Fileitemiterator FII = sfu.getitemiterator (request);
while (Fii.hasnext ()) {

Fileitemstream FIS = Fii.next ();//get a file stream from the collection
if (!fis.isformfield () && fis.getname (). Length () >0) {
System.out.println ("fff");
String fileName = Fis.getname (). substring (Fis.getname (). LastIndexOf (\) +1)//Get file name of uploaded files
System.out.println (FileName);
Bufferedinputstream in = new Bufferedinputstream (Fis.openstream ());//get file input stream
Bufferedoutputstream out = new Bufferedoutputstream (New FileOutputStream (savedir+ "\" +filename));//get file output stream
Streams.copy (in, out, true);//start writing files to your designated upload folder
}
}
Response.getwriter (). println ("File upload successfully!!!");
}
}
catch (Exception e) {
E.printstacktrace ();
}
}

}

Iii. Web.xml Documents

<?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>
    <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>uploadservlet</servlet-name>
     <SERVLET-CLASS>UPLOADSERVLET</SERVLET-CLASS>
  </servlet>


<servlet-mapping>
<servlet-name>UploadServlet</servlet-name>
<url-pattern>/UploadServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>UploadServlet</servlet-name>
<url-pattern>/UploadServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>upload.jsp</welcome-file>
</welcome-file-list>
</web-app>

Related Article

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.