Example analysis of file upload and download function implemented by struts 2 in Java Web
When doing the B / s system, it usually involves uploading and downloading files. Before we connect to the struts 2 framework, we use the fileUpload component of the Commons sub project under Apache to upload files. However, in that way, the code looks cumbersome and inflexible. After learning struts 2, Struts 2 provides a better implementation mechanism for file upload and download. Here I will explain the source code of single file upload and multi file upload respectively. Here, we need to import two jar files for file download and upload, one is commons-fileupload-1.2 2. Jar, and the other is commons-io-2.0 1.jar
Struts 2 single file upload:
The first is a JSP file upload page, which is relatively simple. It is a form with a file upload box
Next is the code of fileuploadaction. Because struts 2 provides a good internship mechanism for uploading and downloading, we only need to write a little code in the action section:
First of all, we should be clear that the file here does not really refer to the file uploaded by JSP. When the file is uploaded, struts 2 will first look for struts multipart. Savedir (this is in default. Properties) is the storage location specified by the name. We can create a new struts The properties property file is used to specify the storage location of the temporary file. If it is not specified, the file will be stored in apache-tomcat-7.0.0 of Tomcat 29 \ work \ Catalina \ localhost \ directory, then we can specify the storage location of the uploaded file and write it to the stream through the output stream. At this time, we can see the uploaded file in the folder.
After the file is uploaded, we need to download it. In fact, the file download principle of struts 2 is very simple, that is, define an input stream, and then write the file into the input stream. The key configuration is struts XML configuration file:
The filedownloadaction code is as follows:
Let's see that this action just defines an input stream and provides getter methods for it. Next, let's look at struts XML configuration file:
struts. There are several places we should pay attention to in the XML configuration file. The first is the type of result. Previously, we defined an action. In result, we basically do not write the type attribute, because it defaults to the mode of request dispatcher. In addition to this attribute, we generally have values such as redirect. Here, because we use file download, Therefore, type must be defined as stream type to tell action that this is the result of file download. There are generally param sub elements in the result element, which is used to set parameters during file download. The inputname attribute is to obtain the file input stream in action. The name must be the same as the input stream attribute in action, and then the contentdisposition attribute, This attribute is generally used to specify how we want to handle the downloaded file. If the value is attachment, a download box will pop up to let users choose whether to download. If this value is not set, the browser will first check whether it can open the downloaded file. If it can, it will directly open the downloaded file, (this is certainly not what we need). Another value is filename, which is the file download name prompted when downloading the file. After configuring this information, we can realize the file download function.
Struts 2 multi file upload:
In fact, the principle of multi file upload is the same as that of single file upload. Single file upload is a single file, and multi file upload is a list < File > set or a file [] array. First, let's take a look at the code of the front-end JSP. Here I use jquery to dynamically add and delete file download boxes:
The name of file must be named file, and then the action code for processing multi file upload is as follows:
In this way, it is also written to an output stream, so that we can see multiple uploaded files in the folder
The next file download as like as two peas just now, struts. XML is the same, so it won't be repeated here
Summary: in general, the file upload and download mechanism provided by struts 2 simplifies a lot of our code. We can use this mechanism in future projects. Similarly, we can also use the fileUpload component to upload files, which is determined by our personal preferences!
There is so much about the file upload and download function in Java Web. Thank you for reading.