在这里我正在上传文件,我想读取文件类型和文件扩展名,但是当我给文件时,FileUtils.copyFile(this.uploadFile, fileToCreate); 附近出现空指针异常;有人可以解决这个问题吗...
public class ImportAction extends ActionSupport implements ServletRequestAware {
private static final long serialVersionUID = 1L;
private File uploadFile;
private String uploadFileName;
private String uploadContentType;
private HttpServletRequest servletRequest;
public File getUploadFile() {
return uploadFile;
}
public void setUploadFile(File uploadFile) {
this.uploadFile = uploadFile;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
RegisterDAO dao = new RegisterDAO();
/**
*
* @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 hsr) {
this.servletRequest = hsr; //To change body of generated methods, choose Tools | Templates.
}
}
这是我的jsp页面
<s:form method="post" action="file" enctype="multipart/form-data">
<s:file label="choose file" name="uploadFile"></s:file>
<s:submit></s:submit>
</s:form>
我已经打印了我的文件路径,它就像这样 filepathD:\ranjith\ranjithdemo3\build\web\