Struts 2 in Java realizes simple file upload and download

1、 Struts 2 file upload the implementation of struts 2 file upload is very simple, which can be completed in a few simple steps; Note: (1) the Struts2 tag of file upload is: < s: file name = "" label = "Upload" / > (2) the premise of file upload is the form attribute method = "post" enctype = "multipart / form data"; (3) The web application must contain common fileUpload Jar and common io Jar, because the default upload parser of struts 2 uses Jakarta; (4) Can be in struts Configure the maximum file size allowed to upload in XML: < constant name = "struts. Multipart. Maxsize" value = "..." / >, The default is 2m;

1. Implementation rules for ordinary file upload: (1) set the form control < s: file name = "Upload" label = "Upload" / > (2) define the attribute: private file upload in action// Private string uploadfilename// The name of the uploaded file; private String uploadContentType; // MIME type of uploaded file; These attributes will be assigned automatically as the file is uploaded; (3) Complete the write to disk function in execute();

Code example: upload01action java

struts. xml

1.jsp

2. Filter by interceptor. The manual filtering method is very simple, that is, filter by input verification, that is, filter in validate(); The interceptor method to be discussed here is a good method. It only needs to be configured in the configuration file. It has good flexibility and can limit the file type and file size; If the uploaded file does not meet the requirements, return to the input logic view;

To configure the Interceptor:

(1) The interceptor for file upload is fileUpload; (2) The parameters allowedtypes and maximumsize need to be given; (3) After the fileUpload interceptor, you need to add < interceptor ref name = "defaultstack" / >

Code example: because filtering is performed through interceptors, you only need to configure struts XML, so only struts. XML is given here XML configuration

We also need the error message after the failed upload of the configuration file. We need to configure the following in the global internationalization resource file:

2、 Struts 2 file download we can download files when learning servlet and HTTP protocol, that is, we can write the content disposition header. This is also the implementation principle of struts 2, but it provides better encapsulation; The stream result type of struts 2 is specially used for file download; (1)struts. Configure the stream result type in XML, and configure the contenttype, contentdisposition and buffersize parameters. Template:

(2) Create public InputStream getinputstream() throws exception in action; Method, which is used to obtain the input stream of the downloaded file;

DownloadAction. java

struts. xml

You can complete the download.

The implementation of struts 2 file upload and download in Java is introduced here. Thank you for reading.

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
分享
二维码
< <上一篇
下一篇>>