Small Example of struts multi-File Upload-fully implemented

Source: Internet
Author: User
Tags i18n

Most of them refer to the network, but are they completely Complete? So I sorted out a piece of code that can be executed. I haven't used struts for a long time. I hope I don't laugh at me.

 

This example is not perfect yet. It may be that people are too lazy and have not done well in many places. Ah, people, too lazy. I want to do it well, however, we can implement functions and do other things.

 

 

The jar packages here are those. For details, see the following picture. The program is attached here,

 

 

To put it bluntly, the most important thing here is this place.

List <string> uploadfilename;
List <File> upload;

 

 

About java files

Package COM. upload; </P> <p> Import Java. io. file; <br/> Import Java. util. list; </P> <p> Import javax. servlet. HTTP. httpservletrequest; </P> <p> Import Org. apache. commons. io. fileutils; <br/> Import Org. apache. struts2.servletactioncontext; </P> <p> Import COM. opensymphony. xwork2.actioncontext; <br/> Import COM. opensymphony. xwork2.actionsupport; </P> <p> @ suppresswarnings ("serial") <br/> public class uploadaction extends Actionsupport {</P> <p> List <string> uploadfilename; <br/> List <File> upload; </P> <p> Public String upload () {</P> <p> try {</P> <p> // obtain the path, <br/> string filepath = getrequest (). getsession (). getservletcontext () <br/>. getrealpath ("/") <br/> + "uploadpath/"; </P> <p> If (upload = NULL) {</P> <p> return success; <br/>} else {</P> <p> for (INT I = 0; I <upload. size (); I ++) {</P> <p> If (upload. get (I )! = NULL) {</P> <p> string fname = uploadfilename. get (I); <br/> string osname = filepath + fname; </P> <p> file = new file (osname); <br/> fileutils. copyfile (upload. get (I), file); <br/>}</P> <p >}< br/>}</P> <p >} catch (exception E) {<br/> E. printstacktrace (); <br/>}< br/> return success; <br/>}</P> <p> // getter and setter </P> <p> protected httpservletrequest getrequest () {<br/> return (httpservletrequest) actioncontext. getcontext (). get (<br/> servletactioncontext. http_request); <br/>}</P> <p> public list <string> getuploadfilename () {<br/> return uploadfilename; <br/>}</P> <p> Public void setuploadfilename (list <string> uploadfilename) {<br/> This. uploadfilename = uploadfilename; <br/>}</P> <p> public list <File> getupload () {<br/> return upload; <br/>}</P> <p> Public void setupload (list <File> upload) {<br/> This. upload = upload; <br/>}</P> <p >}< br/>

 

About JSP pages

<% @ Page Language = "Java" Import = "Java. util. * "pageencoding =" UTF-8 "%> <br/> <% @ taglib prefix =" S "uri ="/Struts-tags "%> <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> multi-upload home page </title> <br/> <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, keyword3 "> <br/> <meta http-equiv =" descri Ption "content =" this is my page "> <br/> <! -- <Br/> <LINK rel = "stylesheet" type = "text/CSS" href = "styles.css" mce_href = "styles.css"> <br/> --> <br/> <MCE: script Type = "text/JavaScript"> <! -- <Br/> // Add multiple uploads <br/> function addcomponent () {<br/> var uploadhtml = document. createelement ("<input class = 'input _ class' size = '30' type = 'file' name = 'upload'/>"); <br/> document. getelementbyid ("Files "). appendchild (uploadhtml); <br/> uploadhtml = document. createelement ("<p/>"); <br/> document. getelementbyid ("Files "). appendchild (uploadhtml); </P> <p >}</P> <p> // --> </MCE: SCRIPT> <br/> </pead> </P> <p> <body> <br/> <Table Height = "20%" align = "center"> <br/> <tr align = "center"> <br/> <TD colspan = "2" Height = "100%" align = "center"> <br/> <span> multiple struts file Upload example </span> <br/> </TD> <br/> </tr> <br/> </table> <br/> <Table Height =" 60% "align =" center "> <br/> <tr align =" center "> <br/> <TD align =" center "> <br/> <Table align = "center"> <br/> <s: form method = "Post" theme = "simple" name = "Upload" <br/> enctype = "multipart/form-Data" Action = "Upload"> <br/> <tr> <br/> <TD> <br/> picture of device information <br/> </TD> <br/> <TD width = "82%"> <br/> <Table> <br/> <tr> <br/> <TD width = "30%"> <br/> <input type = "button" onclick = "addcomponent (); "value =" add file "/> <br/> you can add up to 10 <br/> </TD> <br/> </tr> <br/> <tr> <br/> <TD> <br/> <span id = "Files"> <br/> <input size = '30' type = 'file' name = 'upload '/> </P> <p/> <br/> </span> <br/> </TD> <br/> </tr> <br /> </table> <br/> </TD> <br/> </tr> <br/> <TD colspan = "2"> <br/> <input type = "Submit" value = "Submit"> <br/> </TD> <br/> </tr> <br/> </ s: form> <br/> </table> <br/> </TD> <br/> </tr> </P> <p> </table> <br/> <Table Height = "20%" align = "center"> <br/> <tr align = "center"> <br/> <TD Height = "100%" align =" center "> <br/> author ---- lzg <br/> </TD> <br/> </tr> <br/> </table> <br/> </ body> <br/> </ptml> <br/>

About web. xml

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <web-app version = "2.5" <br/> xmlns = "http://java.sun.com/xml/ns/javaee" <br/> xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" <br/> xsi: schemalocation = "http://java.sun.com/xml/ns/javaee <br/> http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> </P> <p> <! -- Struts2 --> <br/> <filter-Name> struts2 </filter-Name> <br/> <filter-class> <br/> org. apache. struts2.dispatcher. filterdispatcher <br/> </filter-class> <br/> </filter> <br/> <filter-mapping> <br/> <filter-Name> struts2 </ filter-Name> <br/> <URL-pattern>/* </url-pattern> <br/> </filter-mapping> </P> <p> <welcome -File-List> <br/> <welcome-File> index. JSP </welcome-File> <br/> </welcome-file-List> <br/> </Web-app> <br/>

About struts. xml configuration

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <! Doctype struts Public <br/> "-// Apache Software Foundation/DTD struts configuration 2.0/EN" <br/> "http://struts.apache.org/dtds/struts-2.0.dtd"> </P> <p> <struts> <br/> <constant name = "struts. custom. i18n. resources "value =" message "> </constant> <br/> <constant name =" struts. enable. dynamicmethodinvocation "value =" false "/> <br/> <constant name =" struts. devmode "value =" true "/> <br/> <constant name =" struts. i18n. encoding "value =" UTF-8 "/> <br/> <constant name =" struts. multipart. maxsize "value =" 1073741824 "/> </P> <p> <include file =" struts-default.xml "/> </P> <p> <package name =" Global "extends = "struts-Default"> </P> <p> <action name = "Upload" class = "com. upload. uploadaction "method =" Upload "> <br/> <result> success. JSP </result> <br/> </Action> </P> <p> </package> <br/> </struts>

If there is something unclear, I uploaded it to my file. You can find it in my file.

 

Related Article

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.