Single-File and multi-file implementation differences are mainly modified two, one is the plug-in ajaxfileupload.js to receive file ID, the second is the background action is an array of receive. This article mainly introduces the AJAXFILEUPLOAD+STRUTS2 implementation of multi-file upload function, the need for friends can refer to, hope to help everyone.
1. ajaxfileupload File Download Address http://www.phpletter.com/Demo/AjaxFileUpload-Demo/
2, the introduction of Jquery-1.8.0.min.js, Ajaxfileupload.js file
3, File upload page core code
<body> <form action= "" enctype= "Multipart/form-data" >
The files parameters received by the Fileelementid attribute above are [' file1 ', ' file2 ', ' file3 ']
Since it is 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);
Modified 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); Determine if the folder exists and if it does not exist create the 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 ();//the party using time +uuid String name = FileName + filefilename[i].substring (Filefilename[i].lastindexof (".")); File name saved on 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 (); Full path to file save//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; HttpServletResponse response = Servletactioncontext.getresponse (); The try {//path refers to the path of the file to be downloaded. File File = new file (path); Gets the file name. String filename = File.getname (); Download the file in the form of a stream. InputStream fis = new Bufferedinputstream (new FileInputStream (path)); byte[] buffer = new byte[fis.available ()]; Fis.read (buffer); Fis.close(); Empty response 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; }/** * Omit set Get method */}
5, Struts configuration
<! DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.0//en" "Http://struts.apache.org/dtd S/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" n Ame= "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" >inputstrea m</param> <param name= "Contentdisposition" >attachment;filename=${fileName}</param> <param Name= "BufferSize" >4096</param> </result> </action> </package> </struts>
Browser input: http://localhost:8080/struts_ajaxfileupload/index.jsp for file Upload