嗨,從這段代碼我得到空指針異常,當我要讀uploadfilename時,我正在嘗試很多來解決這個問題,但這個問題不解決任何人都可以幫我解決在這....我在上傳文件時出現空指針異常
public class ImportAction extends ActionSupport implements ServletRequestAware {
private static final long serialVersionUID = 1L;
private File uploadFile;
private String uploadfileFileName;
private String uploadfileContentType;
private HttpServletRequest servletRequest;
public String getUploadfileFileName() {
return uploadfileFileName;
}
public void setUploadfileFileName(String uploadfileFileName) {
this.uploadfileFileName = uploadfileFileName;
}
public String getUploadfileContentType() {
return uploadfileContentType;
}
public void setUploadfileContentType(String uploadfileContentType) {
this.uploadfileContentType = uploadfileContentType;
}
public File getUploadFile() {
return uploadFile;
}
public void setUploadFile(File uploadFile) {
this.uploadFile = uploadFile;
}
RegisterDAO dao = new RegisterDAO();
/**
*
* @return
* @throws
* Exception
*/
@SuppressWarnings("unchecked")
@Override
public String execute() throws Exception {
String filePath = servletRequest.getSession().getServletContext().getRealPath("/");
System.out.println("filepath" + filePath);
System.out.println("file" + getUploadFile());
System.out.println("filename" + getUploadfileFileName());
File fileToCreate = new File(filePath, this.uploadfileFileName);
try {
FileUtils.copyFile(this.uploadFile, fileToCreate);
} catch (IOException e) {
addActionError(e.getMessage());
}
return SUCCESS;
// Model m = new Model();
// String result = m.fileimport(uploadFile, uploadfileFileName);
// String result = m.fileimport();
// if (result.equals(SUCCESS)) {
// return SUCCESS;
// } else {
// addActionError(getText(result));
// return "failure";
// }
}
@Override
public void validate() {
System.out.println("inside validate of LA");
}
@Override
public void setServletRequest(HttpServletRequest servletRequest) {
this.servletRequest = servletRequest; //To change body of generated methods, choose Tools | Templates.
}
可能重複[文件路徑中上傳附近的文件空指針異常(http://stackoverflow.com/questions/19634677/in-uploading-file-null-pointer-例外 - 接近文件路徑) –
重複,並且已經回答。您尚未更正變量名稱。至少閱讀我們給你的答案....... –