Struts2 Study Notes (11)-file upload and struts2 Study Notes

Source: Internet
Author: User

Struts2 Study Notes (11)-file upload and struts2 Study Notes

1. Upload a single file

Steps for uploading a single file:

1) Create an upload jsp page

The file upload form must be submitted in POST mode. The encoding type is enctype = "multipart/form-data". The default format is application/x-www-form-urlencoded. For example:

1 <form action="${pageContext.servletContext.contextPath}/uploadAction.action" enctype="multipart/form-data" method="post"> 

2) create an Action class for processing file uploads

  • Declare related attributes in the Action class:
    • To declare an attribute with the same name as the form name attribute on the page, the type of the attribute with the same name is File type, which is used to save temporary files;
    • To declare the [attributes with the same name] ContentType attribute, the type is String type, which indicates the type of the uploaded file;
    • To declare the [attributes with the same name] FileName attribute, the type is String type, which indicates the file to be uploaded;
    • Provides get and set methods for all attributes.
  • You can use the following methods to process uploads in the Action class:
    • Obtain the location of the file to be saved;
    • In the target folder, create a file with the same name as the uploaded file;
    • The FileUtils tool class provides the copyFile () method to copy the temporary file content to the file with the same name in the target folder;
    • Call the delete () method for saving the temporary File attribute to delete the temporary File (that is, the delete method for the File type attribute ).

3) configure the struts. xml file

  • Set the size of the uploaded file. By default, Struts2 sets the total size of the uploaded file to 2 MB. When the total size of the file is exceeded, jump to the input view and display the error message in struts through <s: actionError/>. xml sets the total upload size:
    1 <constant name="struts.multipart.maxSize" value="20000000"></constant>

    Sets the total size of the uploaded file, which is valid for all uploaded forms. You only want to set the current form. You can set the fileUpload interceptor attribute.

  • The fileUpload interceptor has three attributes:
    • Protected Long maximumSize: You can set the size of each uploaded file.
    • Protected Set <String> allowedTypesSet: specifies the type of file to be uploaded.
    • Protected Set <String> allowedExtensionsSet: sets the suffix of the file to be uploaded.

You can configure these attributes in the struts. xml file:

1 <interceptor-ref name = "defaultStack"> 2 <! -- Configure the size of the uploaded file. Here, the size of the uploaded file is configured. --> 3 <param name = "fileUpload. maximumSize"> 20971520 </param> 4 <! -- Configure the types allowed for the upload file --> 5 <param name = "fileUpload. allowedTypes"> text/plain, application/msword </param> 6 <! -- Configure the File Upload extension --> 7 <param name = "fileUpload. allowedExtensions"> .txt,.doc </param> 8 </interceptor-ref>

The allowed types (allowedTypes) and allowed extensions (allowedExtensions) must be consistent.

Example of uploading a single file:

1) create a jsp page

1 <body> 2 <form action = "$ {pageContext. servletContext. contextPath}/uploadAction. action "enctype =" multipart/form-data "method =" post "> 3. upload a file: <input type = "file" name = "upload"> 4 <input type = "submit" value = "upload"> 5 </form> 6 </body>

2) create an Action class

1 public class UploadAction extends ActionSupport {2 private File upload; 3 private String uploadContentType; 4 private String uploadFileName; 5 6 public File getUpload () {7 return upload; 8} 9 10 public void setUpload (File upload) {11 this. upload = upload; 12} 13 14 public String getUploadContentType () {15 return uploadContentType; 16} 17 18 public void setUploadContentType (String uploadContentType) {19 this. uploadContentType = uploadContentType; 20} 21 22 public String getUploadFileName () {23 return uploadFileName; 24} 25 26 public void setUploadFileName (String uploadFileName) {27 this. uploadFileName = uploadFileName; 28} 29 30 @ Override31 public String execute () throws Exception {32 ServletContext SC = ServletActionContext. getServletContext (); 33 // get the location of the file to be saved 34 String path = SC. getRealPath ("/upload"); 35 // create a File 36 file File = new File (path, uploadFileName) with the same name as the uploaded file ); 37 // copy the temporary file content to the file named 38 FileUtils in the target folder. copyFile (upload, file); 39 // Delete the temporary file 40 upload. delete (); 41 return SUCCESS; 42} 43}

3) configure the struts. xml file

 1 <struts> 2     <constant name="struts.devMode" value="true" /> 3     <constant name="struts.multipart.maxSize" value="200000000"/>  4     <package name="default" namespace="/" extends="struts-default"> 5         <action name="uploadAction" class="com.sunny.action.UploadAction"> 6             <result>/success.jsp</result> 7             <result name="input">/error.jsp</result> 8             <interceptor-ref name="defaultStack"> 9                 <param name="fileUpload.maximumSize">20971520</param>10                 <param name="fileUpload.allowedTypes">text/plain,application/msword</param>11                 <param name="fileUpload.allowedExtensions">.txt,.doc</param>12             </interceptor-ref>13         </action>14     </package>15 </struts>

4) file upload interface

5) after the upload is successful, the success. jsp page is displayed.

6) There will be uploaded files in the project's upload folder.

Upload question information Internationalization

Struts2 uploads question information the default file is under the struts-message.properties:

1 struts.messages.error.uploading=Error uploading: {0}2 struts.messages.error.file.too.large=File too large: {0} "{1}" "{2}" {3}3 struts.messages.error.content.type.not.allowed=Content-Type not allowed: {0} "{1}" "{2}" {3}4 struts.messages.error.file.extension.not.allowed=File extension not allowed: {0} "{1}" "{2}" {3}

To display Chinese characters, you must create the ActionClassName. properties file under the same file of the Action class. The ActionClassName file is the class name of the upload Action class, and change the file content:

1 struts. messages. error. uploading = upload error: {0} 2 struts. messages. error. file. too. large = the file to be uploaded is too large: {0} "{1}" "{2}" {3} 3 struts. messages. error. content. type. not. allowed = the File Upload type is not allowed: {0} "{1}" "{2}" {3} 4 struts. messages. error. file. extension. not. allowed = the extension of the uploaded file is not allowed: {0} "{1}" "{2}" {3}

The content before the equal sign is fixed, and the content after it can be written by yourself.

2. Multifile upload

The process for uploading multiple files is the same as that for uploading a single file, but note that:

  • On the page, although multiple files are uploaded, the values of the name attribute of the form must be consistent;
  • Attribute declared in the Action class. The type is changed to an array or set;
  • In the business method, the related processing process is changed to a single file upload cycle.

Example of uploading a single file:

1) create a jsp page

1 <body> 2 <form action = "$ {pageContext. servletContext. contextPath}/uploadAction. action "enctype =" multipart/form-data "method =" post "> 3. upload a file: <input type = "file" name = "upload"> <br> 4. upload a file: <input type = "file" name = "upload"> <br> 5. upload a file: <input type = "file" name = "upload"> <br> 6. upload a file: <input type = "file" name = "upload"> <br> 7 <input type = "submit" value = "upload"> 8 </form> 9 </body>

2) create an Action class

1 public class UploadAction extends ActionSupport {2 private File [] upload; 3 private String [] uploadContentType; 4 private String [] uploadFileName; 5 6 public File [] getUpload () {7 return upload; 8} 9 10 public void setUpload (File [] upload) {11 this. upload = upload; 12} 13 14 public String [] getUploadContentType () {15 return uploadContentType; 16} 17 18 public void setUploadContentType (String [] uploadContentType) {19 this. uploadContentType = uploadContentType; 20} 21 22 public String [] getUploadFileName () {23 return uploadFileName; 24} 25 26 public void setUploadFileName (String [] uploadFileName) {27 this. uploadFileName = uploadFileName; 28} 29 30 @ Override31 public String execute () throws Exception {32 ServletContext SC = ServletActionContext. getServletContext (); 33 // get the location of the file to be saved 34 String path = SC. getRealPath ("/upload"); 35 // create a file 36 for (int I = 0; I <upload. length; I ++) {37 File file = new File (path, uploadFileName [I]); 38 // copy the temporary file content to the 39 FileUtils file with the same name in the target folder. copyFile (upload [I], file); 40 // Delete the temporary file 41 upload [I]. delete (); 42} 43 return SUCCESS; 44} 45}

3) configure the struts. xml file, which is the same as the configuration for uploading a single file.

 1 <struts> 2     <constant name="struts.devMode" value="true" /> 3     <constant name="struts.multipart.maxSize" value="200000000"/>  4     <package name="default" namespace="/" extends="struts-default"> 5         <action name="uploadAction" class="com.sunny.action.UploadAction"> 6             <result>/success.jsp</result> 7             <result name="input">/error.jsp</result> 8             <interceptor-ref name="defaultStack"> 9                 <param name="fileUpload.maximumSize">20971520</param>10                 <param name="fileUpload.allowedTypes">text/plain,application/msword</param>11                 <param name="fileUpload.allowedExtensions">.txt,.doc</param>12             </interceptor-ref>13         </action>14     </package>15 </struts>

4) file upload interface

5) after the upload is successful, the success. jsp page is displayed.

6) There will be uploaded files in the project's upload folder.

3. File Download

 

 

 





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.