Example of struts2 upload

Source: Internet
Author: User

I wrote a struts2 upload example by referring to some friends on the Internet:

The project uses the integration of struts2 and spring3, and the configuration of the integration part will not be written. The packages I imported include:

In struts2, there are seven in total: spring3 is omitted.

Commons-fileupload-1.1.1.jar

Commons-io-1.1.jar

Commons-logging-1.0.4.jar

Freemarker-2.3.8.jar

Ognl-2.6.11.jar

Xwork-2.0.7.jar

Struts2-core-2.0.14.jar

<Body> <! -- In JSP, two forms are written in the body. One is fileupload. Action, and the other is struts2 upload. fileuploadother. Action is the write Io operation. --> <! -- <Form action = "/struts2upload/system/upload/fileupload. action "enctype =" multipart/form-Data "method =" Post "> select a file: <input type = "file" id = "fileop" name = "fileop"/> <input type = "Submit" value = "Upload"/> </form> --> <br/> <form action = "/struts2upload/system/upload/fileuploadother. action "enctype =" multipart/form-Data "method =" Post "> select a file: <input type = "file" id = "fileop" name = "fileop"/> <input type = "Submit" value = "Upload"/> </form> </body>
<! -- Spring configuration, inject the path dependency into the configuration file, and use savepath for Io operations --> <bean id = "myfileuploadaction" class = "com. zyujie. action. uploadaction "Scope =" prototype "> <property name =" savepath "value =" D: \ webapp \ apache-Tomcat-6.0.10 \ webapps \ struts2upload \ upfiles "/> </bean>
<Struts> <! -- This attribute specifies the request suffix to be processed by struts2. The default value of this attribute is action, that is, all requests matching *. Action are processed by struts2. If you need to specify multiple request suffixes, multiple suffixes are separated by commas. --> <Constant name = "struts. Action. Extension" value = "action"/> <! -- Sets whether the browser caches static content. The default value is true (used in the production environment). It is best to disable it during development --> <constant name = "struts. serve. static. browsercache "value =" false "/> <! -- When the struts configuration file is modified, whether the system automatically reloads the file. The default value is false (used in the production environment). It is best to open the file during development --> <constant name = "struts. configuration. XML. reload "value =" true "/> <! -- Used in development mode to print more detailed error information --> <constant name = "struts. devmode" value = "true"/> <! -- Default view topic --> <constant name = "struts. UI. Theme" value = "simple"/> <! -- Integrate Spring and manage struts to the spring container --> <constant name = "struts. objectfactory" value = "Spring"/> <! -- Solve garbled --> <constant name = "struts. i18n. encoding" value = "UTF-8"/> <! -- Specifies the maximum number of bytes of files that can be uploaded. The default value is 2097152 (2 m) --> <constant name = "struts. multipart. maxsize" value = "2097152"/> <! -- Sets the Temporary Folder for uploading files. javax is used by default. servlet. context. tempdir --> <constant name = "struts. multipart. savedir "value =" E: /test "/> <package name =" syserror "extends =" struts-Default "> <global-Results> <result name =" SQL ">/system_error.jsp </result> <result name = "invalidinput">/invalid_input.jsp </result> <result name = "Naming">/system_error.jsp </result> <result name = "Io">/system_error.jsp </result> <result name = "nullpointe R ">/system_error.jsp </result> </Global-Results> <global-exception-mappings> <exception-mapping result =" SQL "exception =" Java. SQL. sqlexception "> </exception-mapping> <exception-mapping result =" invalidinput "exception =" CN. codeplus. exception. invalidinputexception "> </exception-mapping> <exception-mapping result =" Naming "exception =" javax. naming. namingexception "> </exception-mapping> </Global-exception-Map Pings> </package> <! -- Configuration for the default package. --> <package name = "Upload" namespace = "/system/upload" extends = "struts-Default"> <action name = "fileupload" class = "myfileuploadaction" method =" fileupload "> <! -- <Param name = "fileopfilename"> 12345.gif</param> --> <result name = "success" type = "Redirect">/OK. JSP </result> <result name = "Io" type = "Redirect">/system_error.jsp </result> <result name = "nullpointer" type = "Redirect">/system_error.jsp </result> <exception-mapping result = "Io" exception = "Java. io. ioexception "> </exception-mapping> <exception-mapping result =" nullpointer "exception =" Java. lang. nullpointereffectio N "> </exception-mapping> </Action> <action name =" fileuploadother "class =" myfileuploadaction "method =" fileuploadother "> <! -- Dynamically set the savepath attribute value to the path on the server --> <! -- <Param name = "savepath"> D: \ webapp \ apache-Tomcat-6.0.10 \ webapps \ struts2upload \ upfiles </param> --> <Param name = "fileopfilename"> abcde.xls </param> <result name = "success" type = "Redirect">/OK. JSP </result> <result name = "input" type = "Redirect">/index. JSP </result> <! -- Implement the default interceptor function of Struts. Therefore, a defaultstack should be added to the backend to interceptor. --> <Interceptor-ref name = "fileupload"> <! -- File filtering <Param name = "allowedtypes"> image/BMP, image/PNG, image/GIF, image/JPEG, image/jpg </param> --> <! -- File size, in bytes --> <Param name = "maximumsize"> 2097152 </param> </Interceptor-ref> <! -- The default interceptor must be placed after fileupload; otherwise, it is invalid --> <Interceptor-ref name = "defaultstack"/> </Action> </package> </struts>
Package COM. zyujie. action; import Java. io. file; import Java. io. fileinputstream; import Java. io. fileoutputstream; import Java. io. ioexception; import Org. apache. commons. io. fileutils; import Org. apache. struts2.servletactioncontext; import COM. opensymphony. xwork2.actionsupport; @ suppresswarnings ("serial") public class uploadaction extends actionsupport {// Private file fileop of the uploaded file; // file name private string fileopfi Lename; // file type private string fileopcontenttype; // accept the dependency injection attribute private string savepath;/** upload the file, throw the exception to the outside, and handle the declarative exception, submit the exception to struts XML for configuration processing. We can see that strut2 configuration XML */Public String fileupload () throws ioexception {// string realpath = servletactioncontext on the server. getservletcontext (). getrealpath ("/upfiles"); system. out. println ("realpath:" + realpath); If (fileop! = NULL) {file SaveFile = new file (realpath), fileopfilename); If (! SaveFile. getparentfile (). exists () {/** if the folder does not exist, create a folder **/SaveFile. getparentfile (). mkdirs ();} fileutils. copyfile (fileop, SaveFile); // call the Upload Component of struts2 to write the file. // if no exception is thrown, the file is uploaded successfully. out. println ("File Uploaded successfully! ");} Return" success ";}/** use the IO stream to upload files and handle exceptions manually. This is not concise enough */Public String fileuploadother () {fileoutputstream OS = NULL; fileinputstream is = NULL; try {// create the file output stream system. out. println (getsavepath (); OS = new fileoutputstream (getsavepath () + "\\" + getfileopfilename ()); // create a file upload stream is = new fileinputstream (getfileop (); byte [] buffer = new byte [1024]; int Len = 0; while (LEN = is. read (buffer)> 0) {OS. write (buff Er, 0, Len) ;}} catch (exception e) {system. out. println ("File Upload Failed"); E. printstacktrace ();} finally {close (OS, is);} return "success";}/** close Io stream */private void close (fileoutputstream OS, fileinputstream is) {If (is! = NULL) {try {is. close ();} catch (ioexception e) {system. out. println ("failed to disable fileinputstream"); E. printstacktrace () ;}} if (OS! = NULL) {try {OS. close ();} catch (ioexception e) {system. out. println ("failed to disable fileoutputstream"); E. printstacktrace () ;}} public file getfileop () {return fileop;} public void setfileop (File fileop) {This. fileop = fileop;} Public String getfileopfilename () {return fileopfilename;} public void setfileopfilename (string fileopfilename) {This. fileopfilename = fileopfilename;} Public String getfileopcontenttype () {return fileopcontenttype;} public void setfileopcontenttype (string fileopcontenttype) {This. fileopcontenttype = fileopcontenttype;} Public String getsavepath () {return savepath;} public void setsavepath (string savepath) {This. savepath = savepath ;}}

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.