在這裏我使用struts2來上傳文件我得到空指針異常附近字符串filePath當我要上傳我的文件,可以anybbody PLZ幫我在這個問題.....我想找到我上傳的內容的擴展名。在上傳文件空指針異常附近文件路徑
public class ImportAction extends ActionSupport implements ServletRequestAware {
public File getUploadfile() {
return uploadfile;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
public void setUploadfile(File uploadfile) {
this.uploadfile = uploadfile;
}
private static final long serialVersionUID = 1L;
private LoginBean bean;
private File uploadfile;
public String getFileContentType() {
return fileContentType;
}
public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
}
private String uploadFileName;
private String fileContentType;
private HttpServletRequest servletRequest;
RegisterDAO dao = new RegisterDAO();
public LoginBean getBean() {
return bean;
}
public void setBean(LoginBean bean) {
this.bean = bean;
}
/**
*
* @return
* @throws
* Exception
*/
@SuppressWarnings("unchecked")
@Override
public String execute() throws Exception {
try {
String filePath = servletRequest.getSession().getServletContext().getRealPath("/");
System.out.println("Server path:" + filePath);
File fileToCreate = new File(filePath, this.uploadFileName);
FileUtils.copyFile(this.uploadfile, fileToCreate);
} catch (Exception e) {
e.printStackTrace();
addActionError(e.getMessage());
return INPUT;
}
Model m = new Model();
String result = m.fileimport(uploadfile, uploadFileName);
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;
}
}
是否掌握了有效/非空的servletRequest對象? –
對不起,我沒有得到這個評論謝謝 – user2838630
只是檢查你的servletRequest對象是否爲非null。嘗試把一個空,如果檢查。 –