java - When I try to upload and save an image file using Struts in eclipse it shows the file name as null -
this index page:
<%@ page language="java" contenttype="text/html; charset=iso-8859-1" pageencoding="iso-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <meta http-equiv="scontent-type" content="text/html; charset=iso-8859-1"> <title>insert title here</title> </head> <body> <s:form action="clickupload" method="post" enctype="multipart/form-data" > <s:file name="myfile"></s:file> <s:submit value="upload"/> </s:form> </body> </html> struts.xml
<?xml version="1.0" encoding="utf-8"?> <!doctype struts public "-//apache software foundation//dtd struts configuration 2.3//en" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <include file="struts-default.xml"/> <constant name="struts.enable.dynamicmethodinvocation" value="false" /> <constant name="struts.devmode" value="true" /> <package name="uploadsnew" namespace="/" extends="struts-default"> <action name="clickupload" class="tryupload.uploadnewfile" method="execute"> <interceptor-ref name="basicstack"/> <interceptor-ref name="fileupload"> <param name="maximumsize">2048000</param> <param name="allowedtypes">image/jpeg,image/gif</param > </interceptor-ref> <result name="success" >/success.jsp</result> <result name="error">/error.jsp</result> <result name="input">/index.jsp</result> </action> </package> </struts> when upload file , submit shows:
struts problem report struts has detected unhandled exception: messages: file: java/io/file.java line number: 291 -------------------------------------------------------------------------------- stacktraces
java.lang.nullpointerexception java.io.file.<init>(file.java:291) tryupload.uploadnewfile.execute(uploadnewfile.java:32) sun.reflect.nativemethodaccessorimpl.invoke0(native method) sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57) sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) java.lang.reflect.method.invoke(method.java:606) ognl.ognlruntime.invokemethod(ognlruntime.java:870) ognl.ognlruntime.callappropriatemethod(ognlruntime.java:1293) ognl.objectmethodaccessor.callmethod(objectmethodaccessor.java:68) com.opensymphony.xwork2.ognl.accessor.xworkmethodaccessor.callmethodwithdebuginfo(xworkmethodaccessor.java:117) com.opensymphony.xwork2.ognl.accessor.xworkmethodaccessor.callmethod(xworkmethodaccessor.java:108) ognl.ognlruntime.callmethod(ognlruntime.java:1369) ognl.astmethod.getvaluebody(astmethod.java:90) ognl.simplenode.evaluategetvaluebody(simplenode.java:212) ognl.simplenode.getvalue(simplenode.java:258) ognl.ognl.getvalue(ognl.java:494) ognl.ognl.getvalue(ognl.java:458) com.opensymphony.xwork2.ognl.ognlutil$2.execute(ognlutil.java:309) com.opensymphony.xwork2.ognl.ognlutil.compileandexecute(ognlutil.java:340) com.opensymphony.xwork2.ognl.ognlutil.getvalue(ognlutil.java:307) com.opensymphony.xwork2.defaultactioninvocation.invokeaction(defaultactioninvocation.java:423) com.opensymphony.xwork2.defaultactioninvocation.invokeactiononly(defaultactioninvocation.java:287) com.opensymphony.xwork2.defaultactioninvocation.invoke(defaultactioninvocation.java:250) org.apache.struts2.interceptor.fileuploadinterceptor.intercept(fileuploadinterceptor.java:326) com.opensymphony.xwork2.defaultactioninvocation.invoke(defaultactioninvocation.java:244) org.apache.struts2.interceptor.deprecationinterceptor.intercept(deprecationinterceptor.java:41) com.opensymphony.xwork2.defaultactioninvocation.invoke(defaultactioninvocation.java:244) com.opensymphony.xwork2.interceptor.conversionerrorinterceptor.intercept(conversionerrorinterceptor.java:138) com.opensymphony.xwork2.defaultactioninvocation.invoke(defaultactioninvocation.java:244) com.opensymphony.xwork2.interceptor.parametersinterceptor.dointercept(parametersinterceptor.java:229) com.opensymphony.xwork2.interceptor.methodfilterinterceptor.intercept(methodfilterinterceptor.java:98) com.opensymphony.xwork2.defaultactioninvocation.invoke(defaultactioninvocation.java:244) com.opensymphony.xwork2.interceptor.parametersinterceptor.dointercept(parametersinterceptor.java:229) com.opensymphony.xwork2.interceptor.methodfilterinterceptor.intercept(methodfilterinterceptor.java:98) com.opensymphony.xwork2.defaultactioninvocation.invoke(defaultactioninvocation.java:244) org.apache.struts2.interceptor.multiselectinterceptor.intercept(multiselectinterceptor.java:73) com.opensymphony.xwork2.defaultactioninvocation.invoke(defaultactioninvocation.java:244) org.apache.struts2.interceptor.datetextfieldinterceptor.intercept(datetextfieldinterceptor.java:125) com.opensymphony.xwork2.defaultactioninvocation.invoke(defaultactioninvocation.java:244) org.apache.struts2.interceptor.checkboxinterceptor.intercept(checkboxinterceptor.java:91) com.opensymphony.xwork2.defaultactioninvocation.invoke(defaultactioninvocation.java:244) com.opensymphony.xwork2.interceptor.prepareinterceptor.dointercept(prepareinterceptor.java:171) com.opensymphony.xwork2.interceptor.methodfilterinterceptor.intercept(methodfilterinterceptor.java:98) com.opensymphony.xwork2.defaultactioninvocation.invoke(defaultactioninvocation.java:244) org.apache.struts2.interceptor.servletconfiginterceptor.intercept(servletconfiginterceptor.java:164) com.opensymphony.xwork2.defaultactioninvocation.invoke(defaultactioninvocation.java:244) com.opensymphony.xwork2.interceptor.exceptionmappinginterceptor.intercept(exceptionmappinginterceptor.java:189) com.opensymphony.xwork2.defaultactioninvocation.invoke(defaultactioninvocation.java:244) org.apache.struts2.impl.strutsactionproxy.execute(strutsactionproxy.java:54) org.apache.struts2.dispatcher.dispatcher.serviceaction(dispatcher.java:564) org.apache.struts2.dispatcher.filterdispatcher.dofilter(filterdispatcher.java:434) org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:239) org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:206) org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve.java:219) org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve.java:106) org.apache.catalina.authenticator.authenticatorbase.invoke(authenticatorbase.java:502) org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:142) org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:79) org.apache.catalina.valves.abstractaccesslogvalve.invoke(abstractaccesslogvalve.java:617) org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:88) org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:518) org.apache.coyote.http11.abstracthttp11processor.process(abstracthttp11processor.java:1091) org.apache.coyote.abstractprotocol$abstractconnectionhandler.process(abstractprotocol.java:668) org.apache.tomcat.util.net.nioendpoint$socketprocessor.dorun(nioendpoint.java:1527) org.apache.tomcat.util.net.nioendpoint$socketprocessor.run(nioendpoint.java:1484) java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1145) java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:615) org.apache.tomcat.util.threads.taskthread$wrappingrunnable.run(taskthread.java:61) java.lang.thread.run(thread.java:724) seeing page because development mode enabled. development mode, or devmode, enables debugging behaviors , reports assist developers. disable mode, set: struts.devmode=false in web-inf/classes/struts.properties file. console: sep 30, 2015 10:16:29 pm org.apache.struts2.interceptor.fileuploadinterceptor warn warning: content-type not allowed: myfile "fivefingerdeathpunch.jpg" "upload_1a24317f_cf06_45b2_be8d_1b56d56eab1e_00000000.tmp" image/pjpeg server path:e:\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\tryupload\ filename:null uploadnewfile - action class:
package tryupload; import java.io.file; import java.io.ioexception; import javax.servlet.http.httpservletrequest; import org.apache.commons.io.fileutils; import com.opensymphony.xwork2.actionsupport; import org.apache.struts2.interceptor.servletrequestaware; public class uploadnewfile extends actionsupport implements servletrequestaware { /** * */ private static final long serialversionuid = 1l; private httpservletrequest servletrequest; private file myfile; private string myfilecontenttype; private string myfilefilename; public file getmyfile() { return myfile; } public void setmyfile(file myfile) { this.myfile = myfile; } public string getmyfilefilename() { return myfilefilename; } public void setmyfilefilename(string myfilefilename) { this.myfilefilename = myfilefilename; } public string getmyfilecontenttype() { return myfilecontenttype; } public void setmyfilecontenttype(string myfilecontenttype) { this.myfilecontenttype = myfilecontenttype; } public string execute() throws exception { /*file savefilepath = new file("webcontent/images" +myfilefilename); system.out.println("filename:"+myfilefilename); */ try { string filepath = servletrequest.getsession().getservletcontext().getrealpath("/"); system.out.println("server path:" + filepath); system.out.println("filename:"+myfilefilename); file filetocreate = new file(filepath, this.myfilefilename); fileutils.copyfile(this.myfile, filetocreate); } catch (ioexception ex) { system.out.println("couldn't save file: " + ex.getmessage()); ex.getstacktrace(); addactionerror(ex.getmessage()); return error; } return success; } @override public void setservletrequest(httpservletrequest servletrequest) { // todo auto-generated method stub this.servletrequest = servletrequest; } }
to handle multipart request correctly, fileupload interceptor must run before other interceptors otherwise mess things up. changing order
<interceptor-ref name="basicstack"/> <interceptor-ref name="fileupload"> <param name="maximumsize">2048000</param> <param name="allowedtypes">image/jpeg,image/gif</param > </interceptor-ref> to
<interceptor-ref name="fileupload"> <param name="maximumsize">2048000</param> <param name="allowedtypes">image/jpeg,image/gif</param > </interceptor-ref> <interceptor-ref name="basicstack"/> is enough make work. read more.
Comments
Post a Comment