gpt4 book ai didi

java - 上传文件时出现 NullPointerException

转载 作者:行者123 更新时间:2023-11-30 08:14:57 24 4
gpt4 key购买 nike

上传文件时,出现以下错误:

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.jav‌​a:43)
java.lang.reflect.Method.invoke(Method.java:483)

索引.jsp

<form action="upload" method="post" enctype="multipart/form-data">
<label for="myFile">Upload your file</label>
<input type="file" name="myFile" />
<input type="submit" value="Upload"/>
</form>

上传文件.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

<struts>
<include file="example.xml"/>
<constant name="struts.devMode" value="true" />
<constant name="struts.multipart.maxSize" value="1000000" />

<!-- Configuration for the default package. -->
<package name="default" extends="struts-default">
<action name="upload" class="example.uploadFile">
<interceptor-ref name="basicStack"/>
<interceptor-ref name="fileUpload">
<param name="allowedTypes">image/jpeg,image/gif</param>
</interceptor-ref>
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>

最佳答案

要正常工作,FileUpload 拦截器必须在 basicStack 中的其他一些拦截器之前运行;

您可以在 struts-default.xml 中查看:

示例 1:

<!-- Sample file upload stack -->
<interceptor-stack name="fileUploadStack">
<interceptor-ref name="fileUpload"/>
<interceptor-ref name="basicStack"/>
</interceptor-stack>

示例 2:

<interceptor-stack name="defaultStack">
<interceptor-ref name="exception"/>
<interceptor-ref name="alias"/>
<interceptor-ref name="servletConfig"/>
<interceptor-ref name="i18n"/>
<interceptor-ref name="prepare"/>
<interceptor-ref name="chain"/>
<interceptor-ref name="scopedModelDriven"/>
<interceptor-ref name="modelDriven"/>
<interceptor-ref name="fileUpload"/>
<interceptor-ref name="checkbox"/>
<interceptor-ref name="datetime"/>
<interceptor-ref name="multiselect"/>
<interceptor-ref name="staticParams"/>
<interceptor-ref name="actionMappingParams"/>
<interceptor-ref name="params">
<param name="excludeParams">^action:.*,^method:.*</param>
</interceptor-ref>
<interceptor-ref name="conversionError"/>
<interceptor-ref name="validation">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
<interceptor-ref name="workflow">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
<interceptor-ref name="debugging"/>
<interceptor-ref name="deprecation"/>
</interceptor-stack>

然后将其手动放置在 basicStack 声明之前,或使用堆栈(defaultStackfileUploadStack)并包含拦截器的名称在参数名称之前,例如:

<interceptor-ref name="defaultStack">
<param name="fileUpload.allowedTypes">image/jpeg,image/gif</param>
</interceptor-ref>

<interceptor-ref name="fileUploadStack">
<param name="fileUpload.allowedTypes">image/jpeg,image/gif</param>
</interceptor-ref>

关于java - 上传文件时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29210254/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com