Reprinted from
Http://www.cnblogs.com/cjt-java/archive/2012/09/01/2666696.html
The project needs to implement a refreshing batch file upload function. After careful research, we found that the Uploadify plug-in provided by JQuery is very good, but the official instance is based on php, below I use jsp + servlet to simply implement this function. Let's talk about it first:
1. Initialization page:
2. After selecting multiple files (multiple files can be selected at a time:
3. Click Start upload (the upload will automatically disappear after the upload)
The result is that the page is not refreshed. The code below:
1, first go to the official website to download the latest zip package http://www.uploadify.com
2. project structure:
3. key code:
Index. jsp
<% @ Page language = "java" contentType = "text/html; charset = UTF-8" %>
<%
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> Upload </title>
<! -- Load file -->
<Link href = "css/default.css" rel = "stylesheet" type = "text/css"/>
<Link href = "css/uploadify.css" rel = "stylesheet" type = "text/css"/>
<Script type = "text/javascript" src = "scripts/jquery-1.4.2.min.js"> </script>
<Script type = "text/javascript" src = "scripts/swfobject. js"> </script>
<Script type = "text/javascript" src = "scripts/jquery. uploadify. v2.1.4.min. js"> </script>
<! -- Ready event -->
<Script type = "text/javascript">
$ (Document). ready (function (){
$ ("# Uploadify"). uploadify ({
'Upload': 'scripts/uploadify.swf ',
'Script': 'servlet/upload', // requests processed by the background
'Cancelimg': 'images/cancel.png ',
'Folder': 'uploads', // path to which you want to save the file
'Queue id': 'filequeue ', // corresponds to the following id
'Queuesizelimit ': 5,
'Filedesc': 'rar file or zip file ',
'Fileext ':' *. rar; *. zip ', // controls the file extension that can be uploaded. When this item is enabled, you must declare fileDesc at the same time.
'Auto': false,
'Multi ': true,
'Simuploadlimit ': 2,
'Buttontext': 'browse'
});
});
</Script>
</Head>
<Body>
<Div id = "fileQueue"> </div>
<Input type = "file" name = "uploadify" id = "uploadify"/>
<P>
<A href = "javascript: jQuery ('# uploadify'). uploadifyUpload ()"> Start upload </a> & nbsp;
<A href = "javascript: jQuery ('# uploadify'). uploadifyClearQueue ()"> cancel all uploads </a>
</P>
</Body>
</Html>
Web. xml
<? Xml version = "1.0" encoding = "UTF-8"?>
<Web-app version = "2.4"
Xmlns = "http://java.sun.com/xml/ns/j2ee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemaLocation = "http://java.sun.com/xml/ns/j2ee
Http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd>
<Servlet>
<Servlet-name> upload </servlet-name>
<Servlet-class> com. xzit. upload. Upload </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-name> upload </servlet-name>
<Url-pattern>/servlet/Upload </url-pattern>
</Servlet-mapping>
<Welcome-file-list>
<Welcome-file> index. jsp </welcome-file>
</Welcome-file-list>
</Web-app>
Upload. java
Package com. xzit. upload;
Import java. io. File;
Import java. io. IOException;
Import java. util. Iterator;
Import java. util. List;
Import java. util. UUID;
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;
@ SuppressWarnings ("serial ")
Public class Upload extends HttpServlet {
@ SuppressWarnings ("unchecked ")
Public void doPost (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
String savePath = this. getServletConfig (). getServletContext ()
. GetRealPath ("");
SavePath = savePath + "/uploads /";
File f1 = new File (savePath );
System. out. println (savePath );
If (! F1.exists ()){
F1.mkdirs ();
}
DiskFileItemFactory fac = new DiskFileItemFactory ();
ServletFileUpload upload = new ServletFileUpload (fac );
Upload. setHeaderEncoding ("UTF-8 ");
List fileList = null;
Try {
FileList = upload. parseRequest (request );
} Catch (FileUploadException ex ){
Return;
}
Iterator <FileItem> it = fileList. iterator ();
String name = "";
String extName = "";
While (it. hasNext ()){
FileItem item = it. next ();
If (! Item. isFormField ()){
Name = item. getName ();
Long size = item. getSize ();
String type = item. getContentType ();
System. out. println (size + "" + type );
If (name = null | name. trim (). equals ("")){
Continue;
}
// Extension format:
If (name. lastIndexOf (".")> = 0 ){
ExtName = name. substring (name. lastIndexOf ("."));
}
File file = null;
Do {
// Generate the file name:
Name = UUID. randomUUID (). toString ();
File = new File (savePath + name + extName );
} While (file. exists ());
File saveFile = new File (savePath + name + extName );
Try {
Item. write (saveFile );
} Catch (Exception e ){
E. printStackTrace ();
}
}
}
Response. getWriter (). print (name + extName );
}
}