In depth analysis of java web file upload and download function (2)

Follow the previous narrative:

2、 File upload and download

Three board axe developed by struts 2, page JSP - configuration file Struts2 XML -- and action class action

Prerequisite for file upload: the method of the form must be post, and the enctype of the form must be the upload input field of type = "file" provided by multipart / form data

Some rules of struts support for file upload

1. Single file upload

Development steps:

1) . add commons-fileupload-1.2 under WEB-INF / lib 1.jar、commons-io-1.3. 2.jar。 These two files can be downloaded from http://commons.apache.org/ download

2) Step 2: write upfile JSP, set the enctype of the form table to "multipart / form data", as follows:

Write the error page error jsp

success. jsp

3) Write uploadaction1 class: add an attribute in the action class, which corresponds to the name of the file field in the form:

In struts Add the following configuration to the XML file

Principle analysis:

a. The file upload interceptor is responsible for file upload. It is a member of the default stack interceptor stack The interceptor has 3 properties that can be set. • Maximumsize: the maximum length (in bytes) of uploaded files. The default value is 2 Mb • allowedtypes: types of files allowed to be uploaded, separated by commas • allowedextensions: file extensions allowed to be uploaded, separated by commas, which can be in struts These three attributes are overwritten in the XML file

b. An error will be reported if the file exceeds the size or is uploaded illegally (turn to an input view)

Display the prompt of error message through: < s: actionerror / > < s: feilderror / >

c. The error message prompt is changed to Chinese version: with the help of international message resource files

If the error is caused by configuring the global default parameters, it is best to use the global message resource file. The default prompt resource file of struts 2: struts 2 core - * * Jar. Org apache. Struts message of struts 2 Properties file. Just overwrite the corresponding value with the key value.

The configuration is as follows:

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

{0}: < input type = "file" name = "uploadimage" > value of name attribute {1}: real name of uploaded file {2}: name of uploaded file saved to temporary directory {3}: type of uploaded file (struts.messages.error.file.too.large is the size of uploaded file)

Source code:

Modify the information of the resource file that displays errors

Step 1: create a new resource file, such as fileuploadmessage Properties, put it under SRC, and add the following information struts in the resource file messages. error. Uploading = uploading error: {0} struts messages. error. file. too. Large = the uploaded file is too large: {0} "{1}" "{2}" {3} struts messages. error. content. type. not. Allowed = the type of uploaded file is not allowed: {0} '{1}' {2} '{3} struts messages. error. file. extension. not. Allowed = suffix of uploaded file is not allowed: {0} '{1}' {2} '{3}

Step 2: in struts XML file to load the resource file

2. Multi file upload

To upload multiple files, you can use array or list. Other files are similar to single file upload.

3. File download

Principle: Struts2 provides a stream result type, which is a specified stream result type specifically used to support file download. An inputname parameter needs to be specified, which specifies an input stream and provides an entry to the downloaded file

Coding steps: 1) action class downloadaction:

struts. XML configuration file: it is mainly used to configure the results of stream type

Interceptors and file uploads are written here. I'm tired, but I have a full sense of achievement.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>