Implement file upload in struts 2 (2)

Source: Internet
Author: User
Tags tmp folder
Implement file upload in struts 2 (2)Source: chinaitlab [] Author: Admin Editor: Admin

Next we will look at the successful upload page:
<% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8" pageencoding = "UTF-8" %>
<% @ Taglib prefix = "S" uri = "/Struts-tags" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> struts 2 File Upload </title>
</Head>
<Body>
<Div style = "padding: 3px; Border: solid 1px # cccccc; text-align: Center">
'/>
<Br/>
<S: property value = "caption"/>
</Div>
</Body>
</Html> listing 4 showupload. jsp
Showupload. jsp obtains the imagefilename and combines its uploadimages into a URLFigureImage.
Then there is the action configuration file:
<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype struts public
"-// Apache Software Foundation // DTD struts configuration 2.0 // en"
Http://struts.apache.org/dtds/struts-2.0.dtd>
<Struts>
<Package name = "fileuploaddemo" extends = "struts-Default">
<Action name = "fileupload" class = "tutorial. fileuploadaction">
<Interceptor-ref name = "fileuploadstack"/>
<Result name = "success">/showupload. jsp </result>
</Action>
</Package>
</Struts>
The fileupload Action explicitly applies the fileuploadstack interceptor.
Finally, the Web. xml configuration file:
<? XML version = "1.0" encoding = "UTF-8"?>
<Web-app id = "webapp_9" version = "2.4"
Xmlns = "http://java.sun.com/xml/ns/j2ee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemalocation = "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<Display-Name> struts 2 fileupload </display-Name>
<Filter>
<Filter-Name> Struts-cleanup </filter-Name>
<Filter-class>
Org. Apache. struts2.dispatcher. actioncontextcleanup
</Filter-class>
</Filter>

<Filter>
<Filter-Name> struts2 </filter-Name>
<Filter-class>
Org. Apache. struts2.dispatcher. filterdispatcher
</Filter-class>
</Filter>

<Filter-mapping>
<Filter-Name> Struts-cleanup </filter-Name>
<URL-pattern>/* </url-pattern>
</Filter-mapping>
<Filter-mapping>
<Filter-Name> struts2 </filter-Name>
<URL-pattern>/* </url-pattern>
</Filter-mapping>
<Welcome-file-List>
<Welcome-File> index.html </welcome-File>
</Welcome-file-List>
</Web-app>
More configurations
InRun the preceding example. If you pay attention to it, you will find that the server console has the following output:
Mar 20,200 7 4: 08: 43 PM org. Apache. struts2.dispatcher. Dispatcher getsavedir
Info: unable to find'Struts. multipart. savedir'Property setting. Defaulting to javax. servlet. Context. tempdir
Mar 20,200 7 4: 08: 43 PM org. Apache. struts2.interceptor. fileuploadinterceptor intercept
Info: removing file myfile C:/program files/tomcat 5.5/work/Catalina/localhost/struts2_fileupload/upload_251447c2_1116e355841 _ 7ff7_00000006.tmp List 9 server console output
The above information tells us,Struts. multipart. savedirNo configuration.Struts. multipart. savedirUsed to specify the folder for storing temporary files. This configuration is written in the Struts. properties file. For example, if you add the following code to the Struts. properties file:
Struts. multipart. savedir=/Tmp list 10 struts Configuration
In this way, the uploaded file will be temporarily saved to the TMP folder under your root directory (generally C:/tmp). If this folder does not exist, Struts 2 will automatically create one.

Baishitong

Error Handling
The image upload function is implemented in the preceding example. Therefore, users should be prevented from uploading non-image files. How can we achieve this in struts 2? In fact, this is also very simple. Just modify the above example as follows.
Modify fileupload. jsp and add "<s: fielderror/>" between <body> and <s: Form> to output error messages on the page.
Modify the Struts. xml file and change the definition of Action fileupload to the following:
<Action name = "fileupload" class = "tutorial. fileuploadaction">
<Interceptor-ref name = "fileupload">
<Param name = "allowedtypes">
Image/BMP, image/PNG, image/GIF, image/JPEG
</Param>
</Interceptor-ref>
<Interceptor-ref name = "defaultstack"/>
<Result name = "input">/fileupload. jsp </result>
<Result name = "success">/showupload. jsp </result>
</Action> Configuration File Modified in listing 11
Obviously, the function is the allowtypes parameter of the fileupload interceptor. In addition, defaultstack is also introduced in the configuration, which will help us add verification and other functions. Therefore, after an error occurs, the system will jump to the result named "input", that is, fileupload. jsp.
Multi-File Upload
Similar to single-file upload, Struts 2 implements Multifile upload. You can bind multiple <s: File/> to an array or list of actions. As shown in the following example.
<S: Form Action = "domultipleuploadusinglist" method = "Post" enctype = "multipart/form-Data">
<S: file label = "file (1)" name = "Upload"/>
<S: file label = "file (2)" name = "Upload"/>
<S: file label = "file (3)" name = "Upload"/>
<S: Submit/>
</S: Form> list 14 JSP code snippets uploaded to multiple files
If you want to bind to an array, the Action Code should be similar:
Private file [] Uploads;
Private string [] uploadfilenames;
Private string [] uploadcontenttypes;
Public file [] getupload () {return this. Uploads ;}
Public void setupload (file [] upload) {This. uploads = upload ;}
 
Public String [] getuploadfilename () {return this. uploadfilenames ;}
Public void setuploadfilename (string [] uploadfilename) {This. uploadfilenames = uploadfilename ;}
 
Public String [] getuploadcontenttype () {return this. uploadcontenttypes ;}
Public void setuploadcontenttype (string [] uploadcontenttype) {This. uploadcontenttypes = uploadcontenttype;} list more than 15 files upload arrays bind action code snippets
If you want to bind to the list, it should be similar:
Private list <File> uploads = new arraylist <File> ();
Private list <string> uploadfilenames = new arraylist <string> ();
Private list <string> uploadcontenttypes = new arraylist <string> ();
Public list <File> getupload (){
Return this. Uploads;
}
Public void setupload (list <File> uploads ){
This. uploads = Uploads;
}
 
Public list <string> getuploadfilename (){
Return this. uploadfilenames;
}
Public void setuploadfilename (list <string> uploadfilenames ){
This. uploadfilenames = uploadfilenames;
}
 
Public list <string> getuploadcontenttype (){
Return this. uploadcontenttypes;
}
Public void setuploadcontenttype (list <string> contenttypes ){
This. uploadcontenttypes = contenttypes;
}

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.