File upload instance
1. The content of the form on the front-end page is as follows:
The code is as follows: |
Copy code |
<S: form action = "uploadAction" enctype = "multipart/form-data" theme = "xhtml" method = "post"> <S: file name = "upload" label = "file"/> <S: submit/> </S: form> |
2. struts. xml
Configure an action as follows:
The code is as follows: |
Copy code |
<Action name = "uploadAction" class = "org. Rudiment. action. UploadAction"> <Result>/welcom. jsp </result> <Result name = "input">/input. jsp </result> </Action> |
3. The corresponding actions are as follows:
The code is as follows: |
Copy code |
Package org. Rudiment. action; Public class UploadAction extends ActionSupport { // This is required for the frontend <s: file name = "upload" label = "file"/> Private File upload; // This is also a required naming rule xxxContentType; Private String uploadContentType; // This is also a required naming rule: xxxFileName; Private String uploadFileName;
Public File getUpload (){ Return upload; }
Public void setUpload (File upload ){ This. upload = upload; }
Public String getUploadContentType (){ Return uploadContentType; }
Public void setUploadContentType (String uploadContentType ){ This. uploadContentType = uploadContentType; }
Public String getUploadFileName (){ Return uploadFileName; }
Public void setUploadFileName (String uploadFileName ){ This. uploadFileName = uploadFileName; }
@ Override Public String execute () { // File Upload processing code Try { FileOutputStream fos = new FileOutputStream (ServletActionContext. getServletContext (). getRealPath ("/") + getUploadFileName ()); FileInputStream FCM = new FileInputStream (getUpload ()); Byte [] buffer = new byte [1024]; Int len = 0; While (len = fs. read (buffer)> 0) { Fos. write (buffer, 0, len ); } } Catch (FileNotFoundException e ){ E. printStackTrace (); } Catch (IOException e ){ E. printStackTrace (); } Return SUCCESS; } }
|
There are two methods to control file upload:
The first is to control the judgment in the Action with your own code.
Second, file control is implemented based on the configuration file of the strtus2 framework.
Method 1:
Three variables are required for the Action to implement the upload function. If you do not know the specification requirements, read this article first.
Upload struts2 notes
Based on the Action in the above article, we only need to judge its uploadFileName and uploadFileContentype to obtain the file type uploaded by the current user. In addition, the upload attribute is a File instance, so we can upload. length (); to determine the size of the uploaded File
The following is an example:
The code is as follows: |
Copy code |
@ Override Public String execute () { // The variable content will be printed here System. out. println ("FileSize:" + upload. length ()); System. out. println ("uploadFileName:" + uploadFileName ); System. out. println ("uploadContentType:" + uploadContentType ); // With these two variables, we can use the execute method to determine. If (uploadContentType. equals ("image/png") & upload. length () <5000) { Return INPUT; } Else { Return SUCCESS; } } |
The above is just a custom judgment logic framework. The specific Upload logic is not written in. The specific Upload logic can be found in this article
Upload struts2 notes
The above is the first method. Method 1. High flexibility. One disadvantage of manual check is that when there are more such actions, maintenance may be troublesome, because you need to modify the execute () of multiple actions ().
Method 2:
You only need to write the upload logic in execute, so you don't have to worry about whether the uploaded files comply with the specifications. Because these checks are configured in strtus. xml, the configuration is relatively simple.
The code is as follows: |
Copy code |
<Action name = "uploadAction" class = "org. Rudiment. action. UploadAction"> <Interceptor-ref name = "fileUpload"> <Param name = "allowedTypes"> image/png </param> <Param name = "maximumSize"> 50000 </param> </Interceptor-ref> <Interceptor-ref name = "defaultStack"/> <Param name = "savePath">/upload </param> <Result>/welcom. jsp </result> <Result name = "input">/login. jsp </result> </Action> |
Add a bold interceptor in our Action configuration. Then strtus automatically filters out non-conforming uploaded files.
Note: <interceptor-ref name = "defaultStack"/> because the interceptor is explicitly specified. Therefore, the default interceptor of struts does not take effect. Therefore, we need to explicitly use the ultstack interceptor here, otherwise the application deployment fails.