Upload struts2 files (single files/multiple files)

Source: Internet
Author: User

<1> Brief description:

The struts2 File Upload is actually implemented through the interceptor, but the interceptor is defined as the default interceptor, so you do not need to manually configure it. <interceptor name = "fileupload" class = "org. apache. struts2.interceptor. fileuploadinterceptor "/>

<2> specify the size of the file to be uploaded by the user in two ways:

1) by default, Struts. multipart. maxsize = 2097152 in the default. properties file is specified2097152That is, 2 m, calculated by 2097152/(1024*1024) = 2 m

Then we can change its default value. As long as we create a new struts. properties file under the src directory, specify the upload size as follows:


Only 10 MB can be uploaded at a time. No matter how many files are uploaded at a time, the sum is calculated.


2) specify in the Struts. xml file,


In fact, name corresponds to the key of Struts. properties, and value corresponds to the value.

Note:Struts. PropertiesSet the file upload size.Struts. xmlSet the file upload size.Struts. PropertiesThe priority is higherStruts. xml. Generally, you can specify the upload size at one place. We recommend that youStruts. Properties


<3> single file upload of struts2

1. fileupload. jsp

<% @ Page Language = "Java" Import = "Java. util. * "pageencoding =" UTF-8 "%> <br/> <% <br/> string Path = request. getcontextpath (); <br/> string basepath = request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + path + "/"; <br/>%> </P> <p> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"> <br/> <HTML> <br/> <pead> <br/> <base href = "<% = basepath %>"> </P> <p> <title> my JSP 'fileupload. JSP 'starting page </title> </P> <p> <meta http-equiv = "Pragma" content = "no-Cache"> <br/> <meta http- equiv = "cache-control" content = "no-Cache"> <br/> <meta http-equiv = "expires" content = "0"> <br/> <Meta HTTP-equiv = "keywords" content = "keyword1, keyword2, keywo Rd3 "> <br/> <meta http-equiv =" Description "content =" this is my page "> <br/> <! -- <Br/> <LINK rel = "stylesheet" type = "text/CSS" href = "styles.css"> <br/> --> </P> <p> </ head> </P> <p> <body> <br/> <! -- Enctype is application/X-WWW-form-urlencoded by default --> <br/> <form action = "fileupload2" enctype = "multipart/form-Data" method = "Post"> </P> <p> User Name: <input type = "text" name = "usename"> <br/> upload a file: <input type = "file" name = "file1"> <br/> </P> <p> <input type = "Submit" value = "Submit"/> </ p> <p> </form> </P> <p> </body> <br/> </ptml> <br/>
2. process the uploaded fileupload. Java

Package COM. struts2.fileupload; </P> <p> Import Java. io. file; <br/> Import Java. io. fileinputstream; <br/> Import Java. io. fileoutputstream; <br/> Import Java. io. inputstream; <br/> Import Java. io. outputstream; </P> <p> Import Org. apache. struts2.servletactioncontext; </P> <p> Import COM. opensymphony. xwork2.actionsupport; </P> <p>/** <br/> * upload a single file <br/> * @ author administrator <br/> * two files are uploaded, <br/> * beginning First, save the uploaded file to default. struts. multipart. <br/> * empty by default <br/> * saved in Tomcat 6.0 \ work \ Catalina \ localhost \ struts2 directory. TMP extension file <br/> * if you want. multipart. if savedir specifies a directory, you can create a struts file under the SRC folder. properties, <br/> * overwrite default. some key values of properties <br/> * are stored in the specified directory <br/> */<br/> public class fileupload extends actionsupport {</P> <p> private string usename; <Br/> private file file1; // reference of the uploaded file, pointing to the temporary file in the temporary directory <br/> private string file1filename; // name of the uploaded file, fixed filename syntax <br/> private string file1contenttype; // File Upload type, fixed contenttype syntax </P> <p> Public String getusename () {<br/> return usename; <br/>}< br/> Public void setusename (string usename) {<br/> This. usename = usename; <br/>}< br/> Public file getfile1 () {<br/> return file1; <br/>}< br/> Public Vo Id setfile1 (File file1) {<br/> This. file1 = file1; <br/>}< br/> Public String getfile1filename () {<br/> return file1filename; <br/>}< br/> Public void setfile1filename (string file1filename) {<br/> This. file1filename = file1filename; <br/>}< br/> Public String getfile1contenttype () {<br/> return file1contenttype; <br/>}< br/> Public void setfile1contenttype (string file1contenttype) {<br/> This. file1con Tenttype = file1contenttype; <br/>}</P> <p> @ override <br/> Public String execute () throws exception {<br/> // obtain the file storage path <br/> string Path = servletactioncontext. getrequest (). getrealpath ("/upload"); <br/> // output stream <br/> outputstream OS = new fileoutputstream (new file (path, file1filename )); <br/> // input stream <br/> inputstream is = new fileinputstream (file1); </P> <p> byte [] Buf = new byte [1024]; <br/> int length = 0; </P> <P> while (-1! = (Length = is. read (BUF) <br/>{< br/> OS. write (BUF, 0, length); <br/>}< br/> is. close (); <br/> OS. close (); </P> <p> return success; <br/>}</P> <p >}< br/>
3. The final display result page, filedemo. jsp

<% @ Page Language = "Java" Import = "Java. util. * "pageencoding =" UTF-8 "%> <br/> <% @ taglib prefix =" S "uri ="/Struts-tags "%> </P> <p> <% <br/> string Path = request. getcontextpath (); <br/> string basepath = request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + path + "/"; <br/>%> </P> <p> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"> <br/> <HTML> <br/> <pead> <br/> <base href = "<% = basepath %>"> </P> <p> <title> my JSP 'filedemo. JSP 'starting page </title> </P> <p> <meta http-equiv = "Pragma" content = "no-Cache"> <br/> <meta http- equiv = "cache-control" content = "no-Cache"> <br/> <meta http-equiv = "expires" content = "0"> <br/> <Meta HTTP-equiv = "keywords" content = "keyword1, keyword2, keyword 3 "> <br/> <meta http-equiv =" Description "content =" this is my page "> <br/> <! -- <Br/> <LINK rel = "stylesheet" type = "text/CSS" href = "styles.css"> <br/> --> </P> <p> </ head> </P> <p> <body> <br/> upload successful: <br/> usename: <s: property value = "usename"/> <br/> file: <s: property value = "file1filename"/> <br/> contenttype: <s: property value = "file1contenttype"/> </P> <p> </body> <br/> </ptml> <br/>

<4> upload as many files as struts2

1. fileupload. jsp

<% @ Page Language = "Java" Import = "Java. util. * "pageencoding =" UTF-8 "%> <br/> <% <br/> string Path = request. getcontextpath (); <br/> string basepath = request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + path + "/"; <br/>%> </P> <p> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"> <br/> <HTML> <br/> <pead> <br/> <base href = "<% = basepath %>"> </P> <p> <title> my JSP 'fileupload. JSP 'starting page </title> </P> <p> <meta http-equiv = "Pragma" content = "no-Cache"> <br/> <meta http- equiv = "cache-control" content = "no-Cache"> <br/> <meta http-equiv = "expires" content = "0"> <br/> <Meta HTTP-equiv = "keywords" content = "keyword1, keyword2, keywo Rd3 "> <br/> <meta http-equiv =" Description "content =" this is my page "> <br/> <! -- <Br/> <LINK rel = "stylesheet" type = "text/CSS" href = "styles.css"> <br/> --> </P> <p> </ head> </P> <p> <body> <br/> <! -- Enctype is application/X-WWW-form-urlencoded by default --> <br/> <form action = "fileupload2" enctype = "multipart/form-Data" method = "Post"> </P> <p> User Name: <input type = "text" name = "usename"> <br/> upload a file: <input type = "file" name = "file1"> <br/> upload a file: <input type = "file" name = "file1"> <br/> <! -- Both names are file1 --> <br/> <input type = "Submit" value = "Submit"/> </P> <p> </form> </P> <p> </body> <br/> </ptml>
The attribute values of the names of the two uploaded files are the same and can be easily processed in the background.

2. fileupload2.java

Multi-File Upload using collection

Package COM. struts2.fileupload; </P> <p> Import Java. io. file; <br/> Import Java. io. fileinputstream; <br/> Import Java. io. fileoutputstream; <br/> Import Java. io. inputstream; <br/> Import Java. io. outputstream; <br/> Import Java. util. list; </P> <p> Import Org. apache. struts2.servletactioncontext; </P> <p> Import COM. opensymphony. xwork2.actionsupport; </P> <p>/** <br/> * Multifile upload, using a set method <br/> * @ author administrator <br/> * <Br/> */</P> <p> public class fileupload2 extends actionsupport {</P> <p> private string usename; <br/> private list <File> file1; <br/> private list <string> file1filename; <br/> private list <string> file1contenttype; </P> <p> Public String getusename () {<br/> return usename; <br/>}< br/> Public void setusename (string usename) {<br/> This. usename = usename; <br/>}< br/> public list <File> getfile1 () {<br /> Return file1; <br/>}< br/> Public void setfile1 (list <File> file1) {<br/> This. file1 = file1; <br/>}< br/> public list <string> getfile1filename () {<br/> return file1filename; <br/>}< br/> Public void setfile1filename (list <string> file1filename) {<br/> This. file1filename = file1filename; <br/>}< br/> public list <string> getfile1contenttype () {<br/> return file1contenttype; <br/>}< br/> Public void set File1contenttype (list <string> file1contenttype) {<br/> This. file1contenttype = file1contenttype; <br/>}</P> <p> @ override <br/> Public String execute () throws exception {</P> <p> // obtain the file storage path <br/> string Path = servletactioncontext. getrequest (). getrealpath ("/upload"); </P> <p> for (INT I = 0; I <file1.size (); I ++) <br/>{< br/> outputstream OS = new fileoutputstream (new file (path, file1filename. get (I); </P> <P> inputstream is = new fileinputstream (file1.get (I); </P> <p> byte [] Buf = new byte [1024]; <br/> int length = 0; </P> <p> while (-1! = (Length = is. read (BUF) <br/>{< br/> OS. write (BUF, 0, length); <br/>}</P> <p> is. close (); <br/> OS. close (); </P> <p >}</P> <p> return success; <br/>}</P> <p >}< br/>
3. filedemo. jsp

<% @ Page Language = "Java" Import = "Java. util. * "pageencoding =" UTF-8 "%> <br/> <% @ taglib prefix =" S "uri ="/Struts-tags "%> </P> <p> <% <br/> string Path = request. getcontextpath (); <br/> string basepath = request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + path + "/"; <br/>%> </P> <p> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"> <br/> <HTML> <br/> <pead> <br/> <base href = "<% = basepath %>"> </P> <p> <title> my JSP 'filedemo2. JSP 'starting page </title> </P> <p> <meta http-equiv = "Pragma" content = "no-Cache"> <br/> <meta http- equiv = "cache-control" content = "no-Cache"> <br/> <meta http-equiv = "expires" content = "0"> <br/> <Meta HTTP-equiv = "keywords" content = "keyword1, keyword2, keywor D3 "> <br/> <meta http-equiv =" Description "content =" this is my page "> <br/> <! -- <Br/> <LINK rel = "stylesheet" type = "text/CSS" href = "styles.css"> <br/> --> </P> <p> </ head> </P> <p> <body> <br/> upload successful: <br/> usename: <s: property value = "usename"/> <br/> <! -- Traversal value --> <br/> <s: iterator value = "file1filename" id = "F"> <! -- ID is an object. Currently, it is a string set that can be named at will --> <br/> file: <s: property value = "# F"/> <br/> <! -- The method <s: property value = "# F. touppercase () "/> --> <br/> </s: iterator> </P> <p> </body> <br/> </ptml> <br/>
The method of traversing the set can be implemented using the iterator label provided by struts2.

<S: iterator value = "file1filename" id = "F"> <! -- ID is an object. Currently, it is a string set that can be named at will -->
File: <s: property value = "# F"/> <br/>
<! -- You can also call the method <s: property value = "# F. touppercase ()"/> -->

The touppercase () string is converted to uppercase letters.
</S: iterator>


Download link:

1) Servlet File Upload ----
Click Open Link

2) download struts2
Click Open Link




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.