JAVA Servlet batch upload of large files

Source: Internet
Author: User

Create a class UploadServlet

[Java]
Package org. action;
 
Import java. io. File;
Import java. io. IOException;
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. FileItem;
Import org. apache. commons. fileupload. disk. DiskFileItemFactory;
Import org. apache. commons. fileupload. servlet. ServletFileUpload;
 
@ SuppressWarnings ("unchecked ")
Public class UploadServlet extends HttpServlet {
 
Private static final long serialVersionUID = 1L;
 
@ Override
Protected void doGet (HttpServletRequest req, HttpServletResponse resp)
Throws ServletException, IOException {
DoPost (req, resp );
}
 
@ Override
Protected void doPost (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
DiskFileItemFactory factory = new DiskFileItemFactory ();
Factory. setSizeThreshold (8192 );
String tempPath = request. getSession (). getServletContext (). getRealPath ("/")
+ "/Upload ";
File file = new File (tempPath );
If (! File. isDirectory ()){
File. mkdirs ();
}
Factory. setRepository (file );
ServletFileUpload upload = new ServletFileUpload (factory );

List <FileItem> items;
Try {
Items = upload. parseRequest (request );
Iterator <FileItem> itr = items. iterator ();
While (itr. hasNext () {// process each form field in sequence
FileItem item = (FileItem) itr. next ();
If (! Item. isFormField () {/* determine whether it is a Form Control (not a File control). If it is not a Form Control, upload the File */
File savedFile = new File (tempPath, item. getName ());
Item. write (savedFile );
} Else {/* if it is a Form Control, save its value */
System. out. println (item. getFieldName () + "-->" + item. getString ());
}
}
} Catch (Exception e ){
E. printStackTrace ();
}
}

}
Configure in web. xml:

[Html]
<Servlet>
<Servlet-name> upload </servlet-name>
<Servlet-class> org. action. UploadServlet </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-name> upload </servlet-name>
<Url-pattern>/upload </url-pattern>
</Servlet-mapping>

Page code:
[Java]
<% @ Page language = "java" import = "java. util. *" pageEncoding = "ISO-8859-1" %>
<%
String path = request. getContextPath ();
String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";
%>
 
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Base href = "<% = basePath %>">

<Title> My JSP 'index. jsp 'starting page </title>
<Meta http-equiv = "pragma" content = "no-cache">
<Meta http-equiv = "cache-control" content = "no-cache">
<Meta http-equiv = "expires" content = "0">
<Meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3">
<Meta http-equiv = "description" content = "This is my page">
</Head> www.2cto.com

<Body>
<Form action = "<% = basePath %> upload" method = "post" enctype = "multipart/form-data">
<Input type = "file" name = "file"/>
<Input value = "name" name = "name" type = "text"/>
<Input type = "submit"/>
</Form>
</Body>
</Html>

 

Author: hfmbook

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.