implementation of the File upload project Bad environment is built as follows:
STRUTS2 related jar packages:
1. Configure core filters in Web. XML
<!--Configure core filters for Struts2--
<filter>
<filter-name>struts2</filter-name>
< Filter-class>org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
< url-pattern>/*</url-pattern><!--Mapping path-
</filter-mapping>
2. The new Struts.xml file in the SRC directory reads as follows:
<span style= "FONT-SIZE:12PX;" ><! DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.3//en" "http://struts.apache.org/dtds/ Struts-2.3.dtd "> <struts> <package name=" action "namespace="/"extends=" Struts-default "> <action na Me= "Upload" class= "cn.lsh.web.struts2.upload.UploadAction" method= "execute" > <result name= "Success" Type= " Dispatcher ">/WEB-INF/jsp/message.jsp</result> <!--<interceptor-ref name=" FileUpload "> to fi
Elupload Interceptor Injection parameters allow upload of file size, here is set to 5M (if more than 5M will be packet exception) <param name= "MaximumSize" >1024*1024*5</param> Set file extension <param name= "allowedextensions" >.jpg/.txt</param> set File type <param name= "Allowedtypes" &G t;image/jpeg/jpg/txt</param> </interceptor-ref> </action> </package> <!--configuration File temporary save directory, if there is no configuration will be reported: Prompt that the Struts.multipart.saveDir property is not set exception </span><span style= "Font-family:arial, Helvetica, Sans-serif; " >--></span><span style= "FONT-SIZE:12PX;" > <constant name= "struts.multipart.saveDir" value= "/upload"/> </struts></span>
3, New uploadatcion Inherits Actionsupport class (default overload validate () and execute () method)
public class Uploadaction extends Actionsupport {private String username; The following three properties, FileUpload interceptor automatically break into private file upload;//upload file private string uploadcontenttype;//upload file type private string UPLOADFI
lename;//upload file name public void Setusername (String username) {this.username = username;
} public void Setupload (File upload) {this.upload = upload;
} public void Setuploadcontenttype (String uploadcontenttype) {this.uploadcontenttype = Uploadcontenttype;
} public void Setuploadfilename (String uploadfilename) {this.uploadfilename = Uploadfilename;
}//default validation succeeded @Override public void Validate () {super.validate (); }//Upload the Business control method public String execute () throws Exception {System.out.println ("Upload file ....
");
Specifies the temporary save directory location of the uploaded file, String path = Servletactioncontext.getservletcontext (). Getrealpath ("/web-inf/upload");
InputStream in = new FileInputStream (this.upload);
OutputStream out = new FileOutputStream (path+ "\ \" +this.uploadfilename);
int len = 0; Byte[] By = new byte[1024];
while ((Len=in.read (by)) >0) {out.write (By,0,len);
} in.close ();
Out.close ();
Return "Success"; }
}
4, create a new upload.jsp upload page:
<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
Test: Enter http://localhost:8080/struts3/upload.jsp in the browser
Visit the following page:
Upload file Error exception, the test in the bad environment, the following exception occurred:
2016-7-2 12:15:39 Com.opensymphony.xwork2.util.logging.jdk.JdkLogger Info
Info: Unable to find ' struts.multipart.saveDir ' property setting. Defaulting to Javax.servlet.context.tempdir
Upload file ....
Error Analysis: Indicates that the Struts.multipart.saveDir property is not set.
The above information tells us that Struts.multipart.saveDir is not configured.
The Struts.multipart.saveDir is used to specify the folder where the temporary files are stored, which is written in the Struts.properties file or the Struts.xml configuration file. For example, if it is a struts.properties file, add the following code: Struts.multipart.saveDir =/tmp/, or struts.xml configuration file, add the following code: <constant name= " Struts.multipart.saveDir "value="/tmp "/> set temporary file upload path. To resolve this problem.
Workaround One (you can configure J in the Struts.xml file to include the following properties):
<constant name= "Struts.multipart.saveDir" value= "/upload"/>
Workaround two (the following properties can be configured in the Struts.proerties file):
Struts.multipart.savedir=/upload
Solution three (you can configure <url-pattern>/*</url-pattern> in the Web. xml file to <url-pattern>*.action</url-pattern>
Test three configuration methods: View upload file Temporary save directory, E:\apache-tomcat-6.0.39\webapps\struts3\WEB-INF\upload, file upload successful (perfect solution exception).