上传文件时出现NullPointerException
上传文件时,出现以下错误:
Struts Problem Report Struts has detected an unhandled exception: Messages: File: java/io/File.java Line number: 317 Stacktraces java.lang.NullPointerException java.io.File.(File.java:317) example.uploadFile.execute(uploadFile.java:36) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) java.lang.reflect.Method.invoke(Method.java:483)
的index.jsp
uploadFile.java
package example; import java.io.File; import org.apache.commons.io.FileUtils; import java.io.IOException; import com.opensymphony.xwork2.ActionSupport; public class uploadFile extends ActionSupport { private File myFile; private String myFileContentType; private String myFileFileName; private String destPath; public String execute() { destPath = "C:\\Program Files\\Apache Software Foundation\\Apache Tomcat 8.0.3\\work"; try{ System.out.println("Src File name: " + myFile); System.out.println("Dst File name: " + myFileFileName); File destFile = new File(destPath, myFileFileName); FileUtils.copyFile(myFile, destFile); }catch(IOException e){ return ERROR; } return SUCCESS; } public File getMyFile() { return myFile;` } public void setMyFile(File myFile) { this.myFile = myFile; this the file which user selects to upload } public String getMyFileContentType() { return myFileContentType; content type of the uploading file } public void setMyFileContentType(String myFileContentType) { this.myFileContentType = myFileContentType;` } public String getMyFileFileName() { return myFileFileName; } public void setMyFileFileName(String myFileFileName) { this.myFileFileName = myFileFileName; } }
在struts.xml
image/jpeg,image/gif /success.jsp /error.jsp
要正常工作, FileUpload
Interceptor必须在basicStack
其他一些拦截器之前运行;
你可以在struts-default.xml
查看它:
例1:
例2:
^action:.*,^method:.* input,back,cancel,browse input,back,cancel,browse
然后在basicStack
声明之前手动放置它,或使用堆栈( defaultStack
或fileUploadStack
)并在参数名称前包含Interceptor的名称,例如:
image/jpeg,image/gif
要么
image/jpeg,image/gif