To upload files, we must set the form method to POST and set entype to multipart/form-data, the browser sends the binary data of the selected file to the server.
Once the enctype is set to multipart/form-data, the browser uses a binary stream to process form data. However, Struts 2 does not provide its own request parser. That is to say, Struts 2 does not process multipart/form-data requests by itself. It needs to call other upload frameworks to parse binary request data.
In the Struts. properties configuration file of struts 2, we can see the following configuration code, which is mainly used to configure the upload parser when Struts 2 uploads files
# Specify the cos File Upload parser struts. multipart. parser = cos # specify to use the Pell File Upload parser struts. multipart. parser = pell # Struts 2 uses Jakarta's common-FileUpload File Upload parser struts by default. multipart. parser = jakarta
The following uses Struts 2's default file upload support as an example:
To complete file upload, set the enctype attribute of this form field to "multipart/form-data" and "method" to "POST"
As follows:
1 <s: form action = "upload" enctype = "multipart/form-data" method = "post"> 2 <s: textfield name = "title" label = "file title"> </s: textfield> <br/> 3 <s: file name = "upload" label = "SELECT file"> </s: file> 4 <s: submit value = "upload"> </s: submit> 5 </s: form>
When a request is submitted on this page, the request is sent to upload. action. This is an Action of Struts 2.
Action for File Upload
The Action of Struts 2 does not need to process the HttpServletRequest request. The Struts 2 framework is responsible for parsing the parameters in the HttpServletRequest request, including the File domain. Struts 2 uses the File type to encapsulate the File domain.
The Action code used to process the upload request:
1 public class UploadAction extends ActionSupport {2 3 private String title; // encapsulate the File title request parameter 4 private File upload; // upload the File domain 5 private String uploadContentType; // Upload File Type 6 private String uploadFileName; // Upload File Name attributes 7 8 // directly in struts. attribute 9 private String savePath configured in the xml file; 10 11 // accept struts. method 12 13 public void setSavePath (String savePath) {14 this. savePath = savePath; 15} 16 17 // return the storage location of the uploaded file 18 private String getSavePath () throws Exception {19 return ServletActionContext. getServletContext (). getRealPath (savePath); 20} 21 22 // The setter and getter methods of the article title 23 public String getTitle () {24 return title; 25} 26 27 public void setTitle (String title) {28 this. title = title; 29} 30 31 // The setter and getter methods for uploading the File content 32 public File getUpload () {33 return upload; 34} 35 36 public void setUpload (File upload) {37 this. upload = upload; 38} 39 40 // setter and getter methods of the file type to be uploaded 41 public String getUploadContentType () {42 return uploadContentType; 43} 44 45 public void setUploadContentType (String uploadContentType) {46 this. uploadContentType = uploadContentType; 47} 48 49 // setter and getter methods for uploading file names 50 public String getUploadFileName () {51 return uploadFileName; 52} 53 54 public void setUploadFileName (String uploadFileName) {55 this. uploadFileName = uploadFileName; 56} 57 58 @ Override59 public String execute () throws Exception {60 61 // create an upload file output stream 62 FileOutputStream fos = new FileOutputStream (getSavePath () at the server's file storage address and original file name () + "\" + getUploadFileName (); 63 FileInputStream FCM = new FileInputStream (getUpload (); 64 byte [] buffer = new byte [1024]; 65 int len = 0; 66 while (len = Fi. read (buffer)> 0) {67 fos. write (buffer, 0, len); 68 69} 70 return SUCCESS; 71} 72}
The preceding Action provides two attributes: uploadFileName and uploadContentType. These attributes are used to block the file name to be uploaded and the file type to be uploaded. The Action class directly encapsulates the File content of the uploaded File through the File type attribute, but this File attribute cannot obtain the File name and type of the uploaded File, therefore, Struts 2 encapsulates the uploaded file name and file type information in the file domain into the uploadFileName and uploadContentType attributes ,. It can be considered that if the form contains a file field whose name attribute is XXX, the corresponding Action needs to use three attributes to encapsulate the information of the file field.
1. The xxx attribute of the File type encapsulates the File content corresponding to the File domain.
2. The xxxFileName attribute of the String type encapsulates the file name corresponding to the file field.
3. The xxxContentType attribute of the String type encapsulates the file type of the file corresponding to the file field.
The preceding three attributes can be used to implement file attributes more easily. Therefore, in the execute method, you can directly call getXxx () to obtain the file name, file type, and content of the uploaded file.
Configuration File Upload Action
When configuring this Action, you also need to configure a <param.../> element, which is used to dynamically allocate attribute values for the Action attributes:
1 <package name = "uploadaction" extends = "struts-default"> 2 <action name = "upload" class = "com. app. action. UploadAction"> 3 <! -- Dynamically set the Action property value --> 4 <param name = "savePath">/upload </param> 5 <result name = "success">/success. jsp </result> 6 </action> 7 </package>
After the change Action application is configured, we can upload the file. This upload request will be processed by UploadAction. After processing, it will be transferred to the success. jsp page. This page is mainly used to display the uploaded image to verify whether the upload is successful.
Upload successful !! <Br/> file title: <s: property value = "+ title"/> file: "> <Br/>
If the upload is successful, you can see such a page: