这里使用struts2上传文件当我要上传我的文件时,我在String filePath附近得到空指针异常,任何人都可以帮助我解决这个问题.....我希望找到扩展名我上传了什么。
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;
}
}
答案 0 :(得分:0)
很可能你的servletRequest
为空,因此导致空指针异常。您可能必须通过上下文初始化它。
答案 1 :(得分:0)
这是错误的:
private File uploadfile;
private String uploadFileName;
private String fileContentType;
应将其(与Setters一起)更改为:
private File uploadFile; /* Uppercase F */
private String uploadFileName;
private String uploadContentType; /* "upload" prefix */
答案 2 :(得分:0)
字段名称错误。改为
private File uploadfile;
private String uploadfileFileName;
private String uploadfileContentType;
//getters & setters