Before the Struts2 framework, we used the FileUpload component of the Commons subproject under Apache to upload files, but the code seemed cumbersome and not flexible. Struts2 provides a better implementation mechanism for fileupload and download. Here I will explain the source code for single fileupload and multiple fileupload. The other is commons-io-2.0.1.jar

Struts2 single file upload:

The first is a JSP file upload page, which is relatively simple, is a form, there is a file upload box

<! -- For file uploads, make sure the form submission is post, because the binary file can be very large, and the encType attribute must be multipart/form-data. <form action=" fileupload. action" method="post" encType ="multipart/form-data"> username: <input type="text" name="username"><br> file: <input type="file" name="file"><br> <input type="submit" value="submit"> </form>Copy the code

FileUploadAction (struts2) : Struts2: FileUploadAction (struts2) : Struts2: FileUploadAction (Struts2) : Struts2: FileUploadAction (struts2)

Public class extends ActionSupport {private String username; // Note that file does not refer to the file itself uploaded by the front-end JSP, but to the file uploaded to the temporary folder under the file. Private String fileFileName; // The MIME type of the submitted file private String fileContentType; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public File getFile() { return file; } public void setFile(File file) { this.file = file; } public String getFileFileName() { return fileFileName; } public void setFileFileName(String fileFileName) { this.fileFileName = fileFileName; } public String getFileContentType() { return fileContentType; } public void setFileContentType(String fileContentType) { this.fileContentType = fileContentType; } @Override public String execute() throws Exception { String root = ServletActionContext.getServletContext().getRealPath("/upload"); InputStream is = new FileInputStream(file); OutputStream os = new FileOutputStream(new File(root, fileFileName)); System. The out. Println (" fileFileName: "+ fileFileName); Println ("file: "+ file.getName()); system.out.println ("file:" + file.getName())); System.out.println("file: " + file.getPath()); byte[] buffer = new byte[500]; int length = 0; while(-1 ! = (length = is.read(buffer, 0, buffer.length))) { os.write(buffer); } os.close(); is.close(); return SUCCESS; }}Copy the code

First of all, let’s be clear that file here does not really refer to the file uploaded by the JSP. When the file is uploaded, Struts2 first looks for struts.multipart.saveDir(found in default.properties), where the name is stored, You can create a struts.properties property file to specify the location of the temporary file. If not, the file will be stored in tomcat’s apache-tomcat-7.0.29\work\Catalina\localhost\ directory. Then we can specify the location of the uploaded file and write it to the stream through the output stream. Then we can see the uploaded file in the folder.

The struts2 file download is simple: define an input stream and write the file to the input stream. The key configuration is still in the struts. XML configuration file:

The FileDownloadAction code looks like this:

public class FileDownloadAction extends ActionSupport { public InputStream getDownloadFile() { return ServletActionContext. GetServletContext (.) getResourceAsStream (" upload/directory on September 4, 2012. XLS "); } @Override public String execute() throws Exception { return SUCCESS; }}Copy the code

This action simply defines an input stream and provides a getter for it. Let’s look at the struts.xml configuration file:

<action name="fileDownload" class="com.xiaoluo.struts2.FileDownloadAction"> <result name="success" type="stream"> <param  name="contentDisposition">attachment; </param> <param name="inputName">downloadFile</param> </result> </action>Copy the code

The struts. XML configuration file has a few things to note. The first is the type of result. Before we defined an action, we basically left out the type attribute of result because it defaults to request dispatcher. Redirect = stream = stream = stream = stream = stream = stream = stream = stream = stream = stream = stream The inputName attribute gets the file input stream in the action with the same name as the input stream attribute in the action, and then the contentDisposition attribute, This property is generally used to specify the way we hope that through how to deal with the file download, if the value is attachment, download box will pop up a, let the user choose whether to download, if don’t set this value, the browser will first check yourself whether to open the download file, if you can, will directly open the downloaded file, The other value is filename. This is the name that the file is prompted to download. After configuring this information, we can implement the file download function.

Struts2 Multi-file upload:

In fact, the principle of multi-file upload and single File upload is the same, single File upload is a single File, multi-file upload is a List collection or an array of File[], first let’s take a look at the front-end JSP part of the code, Here I use jquery to dynamically add file download box and dynamically delete file download box:

<script type="text/javascript" SRC ="script/ jquery-8.8.1.js "></script> <script type="text/javascript"> $(function() { $("#button").click(function() { var html = $("<input type='file' name='file'>"); $("<input type='button' name='button' value=' delete '><br>"); $("#body div").append(html).append(button); button.click(function() { html.remove(); button.remove(); }) }) }) </script> </head> <body id="body"> <form action="fileUpload2.action" method="post" enctype="multipart/form-data"> username: <input type="text" name="username"><br> file: <input type="file" name="file"> <input type="button" value=" add "id="button"><br> <div></div> value="submit"> </form> </body>Copy the code

The action code for multiple file uploads is as follows:

Public class extends ActionSupport {private String username; Private List< file > file; private List< file > file; Private List<String> fileFileName; private List<String> fileFileName; private List<String> fileContentType; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public List<File> getFile() { return file; } public void setFile(List<File> file) { this.file = file; } public List<String> getFileFileName() { return fileFileName; } public void setFileFileName(List<String> fileFileName) { this.fileFileName = fileFileName; } public List<String> getFileContentType() { return fileContentType; } public void setFileContentType(List<String> fileContentType) { this.fileContentType = fileContentType; } @Override public String execute() throws Exception { String root = ServletActionContext.getServletContext().getRealPath("/upload"); for(int i = 0; i < file.size(); i++) { InputStream is = new FileInputStream(file.get(i)); OutputStream os = new FileOutputStream(new File(root, fileFileName.get(i))); byte[] buffer = new byte[500]; @SuppressWarnings("unused") int length = 0; while(-1 ! = (length = is.read(buffer, 0, buffer.length))) { os.write(buffer); } os.close(); is.close(); } return SUCCESS; }}Copy the code

This also writes it to an output stream so that we can see multiple uploaded files in the folder

The following file download is exactly the same as the previous one, as is struts.xml, which I won’t repeat here

Summary: in general, struts2 provides a FileUpload and download mechanism to simplify our code, we can use this mechanism in future projects, we can also use the FileUpload component for FileUpload, this is a personal choice!