Sample Code for Multifile upload using html5

Source: Internet
Author: User

Comments: Multi-File Upload may be complicated in the past, but since the emergence of html5, it has become very easy. Below is a good example, you can refer to the multiple attribute of <input>.

The Code is as follows:
<Input type = "file" name = "multipleFileUpload" multiple/>

The detailed code of the page is as follows:

The Code is as follows:
<! DOCTYPE html>
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> Solution 4-5: Sending multiple files </title>
</Head>
<Body>
<Form id = "myForm"
Action = "http: // 10.10.25.31: 8080/myupload/UploadPhotoServlet" ENCTYPE = "multipart/form-data" METHOD = "POST">
<Input type = "file" name = "multipleFileUpload" multiple/> <input
Type = "submit" value = "submit"> <input type = "reset" value = "reset">
</Form>
</Body>
</Html>

Detailed code of the java Background:

The Code is as follows:
Import java. io. File;
Import java. io. IOException;
Import java. text. SimpleDateFormat;
Import java. util. Calendar;
Import java. util. Date;
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. FileUploadException;
Import org. apache. commons. fileupload. disk. DiskFileItemFactory;
Import org. apache. commons. fileupload. servlet. ServletFileUpload;
/**
* Servlet implementation class UploadPhotoServlet
*/
Public class UploadPhotoServlet extends HttpServlet {
Private static final long serialVersionUID = 1L;
/**
* @ See HttpServlet # HttpServlet ()
*/
Public UploadPhotoServlet (){
Super ();
// TODO Auto-generated constructor stub
}
/**
* @ See HttpServlet # doGet (HttpServletRequest request, HttpServletResponse response)
*/
Protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
DoPost (request, response );
}
/**
* @ See HttpServlet # doPost (HttpServletRequest request, HttpServletResponse response)
*/
@ SuppressWarnings ("unchecked ")
Protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String imagePath = "c: \ uploadFile \ Image \" + getEachDate () + "\"; // generate a folder by date
File uploadPath = new File (imagePath );
If (! UploadPath. exists ()){
UploadPath. mkdirs ();
}
File tmp = new File ("c: \ tmp \\");
If (! Tmp. exists ()){
Tmp. mkdirs ();
}
DiskFileItemFactory factory = new DiskFileItemFactory (); // create a disk factory
Factory. setRepository (tmp); // sets the File Cache path.
Factory. setSizeThreshold (10*1096); // The default critical value for saving files in the memory or on-disk Temporary Folder. The value is 10240, that is, 10 KB.
ServletFileUpload sfu = new ServletFileUpload (factory); // create a processing tool
Sfu. setSizeMax (10*1024*1024); // The maximum file size that the server can receive.-1 indicates no upper limit.
String fileName = null;
Try {
List <FileItem> list = sfu. parseRequest (request); // parse
If (list. size () <1 ){
Return;
}
For (int j = 0; j <list. size (); j ++ ){
FileItem item = list. get (j );
FileName = item. getName ();
If (fileName. equals ("")){
Request. getRequestDispatcher ("/com/visualizerPhoto. jsp"). forward (request, response );
Return;
}
Int pos = fileName. lastIndexOf ("."); // retrieves the image file format
If (pos> 0 ){
Date date = new Date ();
FileName = imagePath + date. getTime () + fileName. substring (pos );
}
System. out. println ("item:" + item );
Item. write (new File (fileName); // write to disk
}
} Catch (FileUploadException e ){
E. printStackTrace ();
} Catch (Exception e ){
E. printStackTrace ();
}
}
// 13-11-15
Public static String getEachDate (){
Calendar cal = Calendar. getInstance ();
Cal. add (Calendar. DATE, 0 );
String yesterday = new SimpleDateFormat ("yyyy-MM-dd"). format (cal
. GetTime ());
String [] dates = yesterday. split ("-");
String realDate = dates [0]. substring (2, 4) + "-" + dates [1] + "-"
+ Dates [2];
Return realDate. trim ();
}
}

Below is:

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.