ここで 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;
}
}