Reputation: 1
When I used Struts2 to upload a file ,I clicked submit in jsp, however, tomcat showed me the wrong message,I have searched on the Internet, but I can't find the answer,so I hope someone can help me ,thanks!
The Wrong Message is below:
HTTP Status 500 -
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
java.lang.NullPointerException
java.io.FileInputStream.<init>(FileInputStream.java:134)
lanxiaoxing.upload.upLoadAction.execute(upLoadAction.java:58)
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:601)
com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:441)
com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:280)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:243)
com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:165)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:252)
org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:179)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:306)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:130)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:165)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:179)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:488)
org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.26 logs. Apache Tomcat/7.0.26
the struts.xml is:
<?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>
<package name="lanxiaoxing.user" extends="struts-default">
<action name="loginUser" class="lanxiaoxing.user.userAction">
<result name="success">success.jsp</result>
<result name="error">error.jsp</result>
</action>
</package>
<package name="lanxiaoxing.upload" extends="struts-default">
<action name="upLoad" class="lanxiaoxing.upload.upLoadAction">
<result name="SUCCESS">upload_success.jsp</result>
</action>
</package>
</struts>
the upLoadAction.java is:
package lanxiaoxing.upload;
import java.io.*;
import javax.servlet.http.*;
import org.apache.struts2.*;
import com.opensymphony.xwork2.*;
public class upLoadAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
private File lanxiaoxingfile;
private String lanxiaoxingfileContentType;
private String lanxiaoxingfileName;
private String savePath;
private String getSavePath(){
HttpServletRequest request = ServletActionContext.getRequest();
return request.getSession().getServletContext().getRealPath(savePath);
}
public File getlanxiaoxingfile(){
return lanxiaoxingfile;
}
public void setlanxiaoxingfile(File lanxiaoxingfile){
this.lanxiaoxingfile=lanxiaoxingfile;
}
public String getlanxiaoxingfileContentType(){
return lanxiaoxingfileContentType;
}
public void setlanxiaoxingfileContenType(String lanxiaoxingfileContentType){
this.lanxiaoxingfileContentType=lanxiaoxingfileContentType;
}
public String getlanxiaoxingfileName(){
return lanxiaoxingfileName;
}
public void setlanxiaoxingfileName(String lanxiaoxingfileName){
this.lanxiaoxingfileName=lanxiaoxingfileName;
}
public void setsavePath(String savePath){
this.savePath=savePath;
}
public String execute() throws Exception{
setsavePath("");
FileOutputStream fos = new FileOutputStream(getSavePath()+"\\"+getlanxiaoxingfileName());
FileInputStream fis = new FileInputStream(getlanxiaoxingfile());
byte[]buffer = new byte[1024];
int len=0;
while((len=fis.read(buffer))>0){
fos.write(buffer, 0, len);
}
return SUCCESS;
}
}
Upvotes: 0
Views: 1473
Reputation: 160191
Your getter and setter names are all incorrect; they should follow normal Java naming conventions, and the conventions expected by the file upload interceptor, e.g.:
public void setLanxiaoxingfile(File lanxiaoxingfile) { // Note capital "L"
This assumes a file input field with the name "lanxiaoxinfile".
IMO the "file"
portion of that variable name is redundant.
See the file upload docs for more info.
Also, as tusar says, the result name should be lower-case "success"
, or just left blank, as that is the default. The default stack already contains the "fileUpload"
interceptor, though, so re-configuring it for this action would be redundant and misleading.
Consider doing the file copy via a utility method, or just use one of the Commons IO file copy utilities.
Upvotes: 1
Reputation: 3424
FileUpload using Struts2 and missing the fileUpload
and defaultStack
interceptors seems causing the error. Try changing your action-declaration like :
<action name="upLoad" class="lanxiaoxing.upload.upLoadAction">
<interceptor-ref name="fileUpload">
<param name="maximumSize">999999999999</param>
<param name="allowedTypes">
image/png,image/gif,image/jpeg,image/pjpeg
</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<result name="success">upload_success.jsp</result>
</action>
Also , SUCCESS
is actually a String "success"
thats why I modified the result-name to small letters.
While learning, it is always good to follow a tutorial.
Upvotes: 0