Manual file filtering

Source: Internet
Author: User

Most of the time, Web applications do not allow viewers to upload files freely, especially executable files-because they may be virus programs. In general, we can allow the viewer to upload images, upload compressed files, and so on. In addition, we must limit the size of the files uploaded by the viewer. Therefore, you must filter files during file upload.

From the preceding Action, we can see that there are two methods in the Action to obtain the file type and size respectively. To filter files, you can filter files by judging the return values of these two methods. In this way, the programmer obtains all the filter control rights.

To manually filter files, follow these steps.

Define a method for file filtering in Action. The method name is arbitrary. The logic of this method is to determine whether the type of the uploaded file is allowed. For example, add the filterTypes () method. The method code is as follows.

Program list: codes \ 04 \ 4.3 \ codeFilter \ WEB-INF \ src \ org \ crazyit \ app \ action \ UploadAction. java

 
 
  1. /**
  2. * Filter file types
  3. * @ Param types all file types that can be uploaded by the System
  4. * @ Return if the file type to be uploaded is allowed,
  5. * Null is returned. Otherwise, an error string is returned.
  6. */
  7. Public String filterTypes (String [] types)
  8. {
  9. // Obtain the object type to be uploaded
  10. String fileType = getUploadContentType ();
  11. For (String type: types)
  12. {
  13. If (type. equals (fileType ))
  14. {
  15. Return null;
  16. }
  17. }
  18. Return ERROR;
  19. }

The above method determines whether the file type to be uploaded is in the list of allowed file types. To enable the application to dynamically configure the list of files that can be uploaded, an allowTypes attribute is added to the Action. The value of this attribute lists all types of files that can be uploaded. To configure the value of the allowTypes attribute in the struts. xml file, the following code must be provided in the Action class.

Program list: codes \ 04 \ 4.3 \ codeFilter \ WEB-INF \ src \ org \ crazyit \ app \ action \ UploadAction. java

 
 
  1. // Define the file type that this Action allows to upload
  2. Private String allowTypes;
  3. // Setter and getter methods of the allowTypes attribute
  4. Public String getAllowTypes ()
  5. {
  6. Return allowTypes;
  7. }
  8. Public void setAllowTypes (String allowTypes)
  9. {
  10. This. allowTypes = allowTypes;
  11. }

3) Use Struts 2 input validation to determine whether the user input file meets the requirements. If not, add the error prompt to FieldError. The added validate () method code in this Action is as follows:

Program list: codes \ 04 \ 4.3 \ codeFilter \ WEB-INF \ src \ org \ crazyit \ app \ action \ UploadAction. java

 
 
  1. // Perform Input Validation
  2. Public void validate ()
  3. {
  4. // You can use commas (,) to upload a string of the file type ,)
  5. // Break it into a string array to determine whether the current file type can be uploaded
  6. String filterResult = filterType (getAllowTypes (). split (","));
  7. // If the current file type cannot be uploaded
  8. If (filterResult! = Null)
  9. {
  10. // Add FieldError
  11. AddFieldError ("upload", "the file type you want to upload is incorrect! ");
  12. }
  13. }

The code of the above validate () method is very simple. It calls filterTypes to determine whether the File Uploaded By the browser meets the requirements. If it is not the file type that can be uploaded, validate () fieldError is added to the method, so that Struts 2 will automatically return the input logic view name. The file upload logic is executed only when the file type is the file type that can be uploaded.

To return the input logic view when the file type check fails, you must add the input logic view configuration for this Action. After the above configuration, when the file type uploaded by the viewer is not allowed, the system will return to the page corresponding to the input logic view.

The modified struts. xml file code is as follows.

Program list: codes \ 04 \ 4.3 \ codeFilter \ WEB-INF \ src \ struts. xml

 
 
  1. <! -- Configure the Action for processing file uploads -->
  2. <Action name = "uploadPro" class = "org. crazyit. app. action. UploadAction">
  3. <! -- Dynamically set the attribute value of an Action -->
  4. <Param name = "savePath">/uploadFiles </param>
  5. <! -- Set the file type that can be uploaded -->
  6. <Param name = "allowTypes"> image/png, image/gif, image/jpeg </param>
  7. <Result name = "input">/WEB-INF/content/upload. jsp </result>
  8. <! -- Configure the default view page of Struts 2 -->
  9. <Result>/WEB-INF/content/succ. jsp </result>
  10. </Action>

To display an error message indicating failed file filtering on the input. jsp page, you can use the following code to output the error message.

 
 
  1. <s:fielderror/> 

Filtering file size is similar to filtering file types. Although there is no way to directly obtain the size of the uploaded File in the preceding Action class, Action contains a File type attribute, which encapsulates the File content corresponding to the File field; the File class has a length () method, which can return the File size. By comparing the File size and the File size that can be uploaded, you can determine whether to allow the File to be uploaded.

Tip: to filter the File size, you can call the length () method of the File class to obtain the size of the uploaded File and compare it with the size of the File that can be uploaded, to determine whether upload is allowed.

 

This article from the "crazy Java Li Gang" blog, please be sure to keep this source http://javaligang.blog.51cto.com/5026500/889839

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.