Use WebUploader to implement the multipart breakpoint file upload function (2 ),
Preface:
In the past few days, I have studied how to upload files through WebUploader. The previous blog has recorded examples of using WebUploader to upload files. Today I will record the multipart resumable upload examples, in the blog, I also checked some materials. Basically, Servlet or SpringMVC is used for data processing in the background, because the latest projects have been Struts2, so here we use the action in Struts2 to process the data, to achieve the effect of multipart upload files.
1. What is multipart upload?
As the name implies, a file is divided into several small files and then uploaded. The advantage of this is that it is easy to upload large files.
2. General idea of multipart upload:
1. On the foreground page, select a file and Click Upload.
2. WebUploader splits the uploaded files into a specified number and sends them to the server background one by one.
3. The server receives split small files and stores them in a temporary folder.
4. After the server receives the split small file, the frontend page executes the upload success function.
5. In the upload success function, send a request to the server and merge the small file into a whole file.
6. Merge files in the background of the server. After the merge, delete the temporary files that store small files.
After learning about the process of multipart upload, let's go to the demo.
Front-end page:
<% @ Page contentType = "text/html; charset = UTF-8" language = "java" %> <% String scheme = request. getScheme (); String serverName = request. getServerName (); String contextPath = request. getContextPath (); int port = request. getServerPort (); // The URL String baseURL = scheme + ": //" + serverName + ":" + port + contextPath; request. setAttribute ("baseURL", baseURL); %>
Background action:
/*** Description: com. ims. action * Author: Eleven * Date: */@ Controller ("FileAction ") public class FileAction {/* parameters related to each small file used to receive the split Request * // remember to provide the corresponding get set Method // upload the file object (and the form type = file the name value is consistent) private File upload; // File name: private String uploadFileName; // upload type: private String uploadContentType;/*** the following variables are public and there are too many parameters. If you do not want to set them to private, you can write get again, when the set method is complete, * the system uses public to steal the slice. * // The object part number is public String chunk; Public String guid; // used for merging and splitting // The public String fileName parameter used to receive the merge request. // the file name is public String chunks; // Number of splits // this method is called every time a small file is uploaded during multipart upload. This method is no different from the public void uploadFile2 () throws Exception {String str = "D:/upload44/divide /"; // File storage path // Save the path of each small File String realPath = str + guid + "/" + chunk; File tmp = new File (realPath); FileUtils. copyFile (upload, tmp); System. out. println ("Upload File" + uploadFi LeName + ", slice:" + chunk + ", size:" + (upload. length ()/1024/1024) + "M");} // file merging public void mergeFile () throws Exception {String path = "D:/upload44/merge /"; // create the merged folder new File (path ). mkdir (); // create the merged File newFile = new File (path + fileName); if (! NewFile. exists () {newFile. createNewFile ();} FileOutputStream outputStream = new FileOutputStream (newFile, true); // append a file to byte [] byt = new byte [10*1024*1024]; int len; fileInputStream temp = null; // fragment file for (int I = 0; I <Integer. parseInt (chunks); I ++) {// "D: /upload44/divide/"+ guid +"/"+ I is the path for saving the split small File temp = new FileInputStream (new File (" D: /upload44/divide/"+ guid +"/"+ I); while ((Len = temp. read (byt ))! =-1) {System. out. println (len); outputStream. write (byt, 0, len);} temp. close ();} // you can close the stream outputStream only when all append writes are completed. close (); // Delete the multipart file String path2 = "D:/upload44/divide/" + guid; FileUtils. deleteDirectory (new File (path2); // delete all contents in the directory System. out. println ("success! Guid = "+ guid +"; chunks = "+ chunks +"; fileName = "+ fileName);} public File getUpload () {return upload ;} public void setUpload (File upload) {this. upload = upload;} public String getUploadFileName () {return uploadFileName;} public void setUploadFileName (String uploadFileName) {this. uploadFileName = uploadFileName;} public String getUploadContentType () {return uploadContentType;} public void setUploadContentType (String uploadContentType) {this. uploadContentType = uploadContentType ;}}
Struts. xml configuration:
<action name="uploadFile2" class="FileAction" method="uploadFile2"></action><action name="mergeFile" class="FileAction" method="mergeFile"></action>
Now, a simple multipart upload task is complete.
By the way, the backend only receives some simple parameters, and the parameters passed from WebUploader at the front end are of course not limited to the preceding ones. Therefore, you can learn to use the F12 debugging mode, view the sent request and related request parameters.
Run:
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.