Struts. xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"><struts><constant name="struts.ui.theme" value="simple"></constant><package name="rx" extends="struts-default" namespace="/*"><action name="*_*" class="{1}Action" method="{2}"><result name="success">${successResultValue}</result><result name="redirect" type="redirectAction" >${redirectResultValue}</result></action></package></struts>
Baseaction. Java
Package COM. YL. action; import javax. servlet. HTTP. httpservletrequest; import javax. servlet. HTTP. httpservletresponse; import javax. servlet. HTTP. httpsession; import Org. apache. struts2.servletactioncontext; import COM. opensymphony. xwork2.actionsupport; public class baseaction extends actionsupport {/*** serialization ID */Private Static final long serialversionuid = 1l; protected string successresultvalue; Public String getsuccessresultvalue () {return successresultvalue ;} public void setsuccessresultvalue (string successresultvalue) {This. successresultvalue = successresultvalue;} protected string redirectresultvalue; Public String getredirectresultvalue () {return redirectresultvalue;} public void setredirectresultvalue (string redirectresultvalue) {This. redirectresultvalue = redirectresultvalue;} protected string chainresultvalue; Public String getchainresultvalue () {return chainresultvalue;} public void setchainresultvalue (string chainresultvalue) {This. chainresultvalue = chainresultvalue;}/*** return request object ** @ return */protected httpservletrequest getrequest () {return servletactioncontext. getrequest ();}/*** returns the response object ** @ return */protected httpservletresponse getresponse () {return servletactioncontext. getresponse ();}/*** return Session Object ** @ return */protected httpsession getsession () {return getrequest (). getsession ();}}
Example action:
Package COM. YL. action; import Java. io. file; import Java. io. fileinputstream; import Java. io. filenotfoundexception; import Java. io. fileoutputstream; import Java. io. ioexception; import Java. text. simpledateformat; import Java. util. date; import javax. annotation. resource; import Org. springframework. context. annotation. scope; import Org. springframework. stereotype. component; import COM. YL. biz. imgbiz; import COM. YL. conf IG. sysconfig; import COM. YL. cons. uploadresult; import COM. YL. entity. IMG; import COM. YL. util. cipherutil; @ component ("imgaction") @ scope ("prototype ") /*** image action */public class imgaction extends baseaction {/*** serialization ID */Private Static final long serialversionuid = 1l; /*** path */private file upload; Public file getupload () {return upload;} public void setupload (File Upload) {This. upload = upload;}/*** figure Slice entity */private IMG; Public IMG getimg () {return IMG;} public void setimg (IMG) {This. IMG = IMG;}/*** injection */@ suppresswarnings ("UNUSED") @ Resource (name = "imgbiz") Private imgbiz; /*** upload an image ** @ return */@ suppresswarnings ("deprecation") Public String uploadfile () {@ suppresswarnings ("UNUSED") string paths = getrequest (). getrealpath ("/"); // determines whether the path is null if (this. upload = NULL |! This. Upload. isfile () |! This. upload. canread () {This. addactionerror (uploadresult. nullfile. gettitle (); Return success;} Try {fileinputstream FCM = new fileinputstream (this. upload); // get the image size int filesize = fiis. available (); // deduce the image size if (filesize> sysconfig. max_file_size) {This. addactionerror (uploadresult. toolargefile. gettitle ();} else {// save path string Path = sysconfig. public_path; // obtain the image name string filename = This. upload. getname (); // Infer whether it is a bit if (! Filename. contains (". ") {This. addactionerror (uploadresult. notimg. gettitle ();} // specifies the filename = filename. substring (filename. lastindexof (". "); simpledateformat SDF = new simpledateformat (" yyyy-mm-dd hh: mm: ss aa "); // name the image filename = cipherutil. md5encoding (SDF. format (new date () + filename; // Save the image path + image name filename = path + "\" + filename; // a new address. file file = new file (filename); // output the image to the new address fileo Utputstream Fos = new fileoutputstream (File); int C; byte B [] = new byte [4*1024]; while (C = FCM. Read (B ))! =-1) {FOS. write (B, 0, c);} // close related operation FOS. flush (); FCM. close (); // Save the successful message this. addactionerror (uploadresult. success. gettitle () ;}} catch (filenotfoundexception e) {// Save the failed information this. addactionerror (uploadresult. nullfile. gettitle (); E. printstacktrace ();} catch (ioexception e) {// Save the failure information this. addactionerror (uploadresult. uploadfail. gettitle (); E. printstacktrace ();} // you can specify setsuccessresultvalue ("/index. JSP "); Return success ;}}
Constant:
Package COM. YL. config; import Org. apache. struts2.servletactioncontext; import Org. springframework. beans. factory. initializingbean;/*** image system class **/public class sysconfig implements initializingbean {public static Boolean is_debug = true; /*** upload image size control */public static int max_file_size = 1024*1024*100;/*** upload image path control */@ suppresswarnings ("deprecation ") public static string public_path = servletactionco Ntext. getrequest (). getrealpath ("/") + "Upload"; private sysconfig () {} public static Boolean isdebug () {return is_debug! = NULL & is_debug;} public void setis_debug (Boolean is_debug) {is_debug = is_debug;} public void afterpropertiesset () throws exception {}}
An error returned Enum is encapsulated. For details, see action:
Package COM. YL. cons;/*** upload return flag **/Public Enum uploadresult {success (short) 0, "successful"), nullfile (short) 1, "file not found"), notimg (short) 2, "non-image file"), uploadfail (short) 3, "Upload Failed"), toolargefile (short) 4, "the file is too large");/***** Error Type */private short type;/***** error name */private String title; private uploadresult (short type, string title) {This. type = type; this. title = title;} public short GetType () {return type;} Public String gettitle () {return title ;}}
Struts2 is a concise struts. xml