AjaxFileUpload + Struts2 implement Multifile upload,
This article focuses on introducing the multi-file upload function of AjaxFileUpload + Struts2. For detailed implementation code, refer to the following article.
The differences between single-file and multi-file implementations are mainly modified at two points,
First, the ajaxfileupload. js plug‑in receives the file ID.
Second, the background action is received in array format
1. ajaxFileUpload file http://www.phpletter.com/Demo/AjaxFileUpload-Demo/
2. Introduce the jquery-1.8.0.min.js, ajaxFileUpload. js File
3. Core code of the file upload page
<Body> <form action = "" enctype = "multipart/form-data">
The files received by the preceding fileElementId attribute is['file1','file2','file3']
Because it is a multi-file, we need to modify ajaxfileupload. js to find the following code:
var oldElement = jQuery('#' + fileElementId); var newElement = jQuery(oldElement).clone(); jQuery(oldElement).attr('id', fileId); jQuery(oldElement).before(newElement); jQuery(oldElement).appendTo(form);
To:
for(var i in fileElementId){ var oldElement = jQuery('#' + fileElementId[i]); var newElement = jQuery(oldElement).clone(); jQuery(oldElement).attr('id', fileId); jQuery(oldElement).before(newElement); jQuery(oldElement).appendTo(form); }
4. File Upload Action
Public class FileAction {private File [] file; // File private String [] fileFileName; // file name private String [] filePath; // File path private String downloadFilePath; // File Download path private InputStream inputStream;/*** File Upload * @ return */public String fileUpload () {String path = ServletActionContext. getServletContext (). getRealPath ("/upload"); File file = new File (path); // determines whether a folder exists. if not, create a folder if (! File. exists () {file. mkdir ();} try {if (this. file! = Null) {File f [] = this. getFile (); filePath = new String [f. length]; for (int I = 0; I <f. length; I ++) {String fileName = java. util. UUID. randomUUID (). toString (); // use the time + UUID method to name String name = fileName + fileFileName [I]. substring (fileFileName [I]. lastIndexOf (". "); // file name saved in the hard disk: FileInputStream inputStream = new FileInputStream (f [I]); FileOutputStream outputStream = new FileOutputStream (path +" \ "+ Name); byte [] buf = new byte [1024]; int length = 0; while (length = inputStream. read (buf ))! =-1) {outputStream. write (buf, 0, length);} inputStream. close (); outputStream. flush (); // The complete path for saving the file // such as: D: \ tomcat6 \ webapps \ struts_ajaxfileupload \ upload \ a0be14a1-f99e-4239-b54c-b37c3083134a.png filePath [I] = path + "\" + name ;}} catch (Exception e) {e. printStackTrace ();} return "success";}/*** File Download * @ return */public String downloadFile () {String path = downloadFilePath; HttpServletRe Response se response = ServletActionContext. getResponse (); try {// path indicates the path of the object to be downloaded. File file = new File (path); // get the File name. String filename = file. getName (); // download the object as a stream. InputStream FCM = new BufferedInputStream (new FileInputStream (path); byte [] buffer = new byte [FCM. available ()]; FCM. read (buffer); FCM. close (); // clear response. reset (); // set response Header String filenameString = new String (filename. getBytes ("gbk"), "iso-8859-1"); response. addHeader ("Content-Disposition", "attachment; filename =" + filenameString); response. addHeader ("Content-Length", "" + file. length (); OutputStream toClient = new BufferedOutputStream (response. getOutputStream (); response. setContentType ("application/octet-stream"); toClient. write (buffer); toClient. flush (); toClient. close ();} catch (IOException ex) {ex. printStackTrace ();} return null;}/*** omitting the set get Method */}
5. struts Configuration
<! DOCTYPE struts PUBLIC "-// Apache Software Foundation // DTD Struts Configuration 2.0 // EN "" http://struts.apache.org/dtds/struts-2.0.dtd "> <Struts> <package name =" ajax_code "extends =" json-default "> <! -- File Upload --> <action name = "fileUploadAction" class = "com. itmyhome. fileAction "method =" fileUpload "> <result type =" json "name =" success "> <param name =" contentType "> text/html </param> </result> </action> </package> <package name = "jsp_code" extends = "struts-default"> <! -- File download --> <action name = "downloadFile" class = "com. itmyhome. fileAction "method =" downloadFile "> <result type =" stream "> <param name =" contentType "> application/octet-stream </param> <param name =" inputName"> inputStream </param> <param name = "contentDisposition"> attachment; filename =$ {fileName} </param> <param name = "bufferSize"> 4096 </param> </result> </action> </package> </struts>
Enter http: // localhost: 8080/struts_ajaxfileupload/index. jsp in the browser to upload files.
Project source code download: http://demo.jb51.net/js/2017/struts_ajaxfileupload.rar
Summary
The above is the multi-file upload function of AjaxFileUpload + Struts2 introduced by xiaobian. I hope it will help you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!