struts2 上传文件问题 就大神解答
上传文件的action..
我如果设置了这些字段的get set方法。就不执行 这个文件上传的方法。也没什么异常,直接跳到错误页面怎么回事
。我把字段的方法删除后。就执行了这个方法。但是却是null指针异常
private File uploadImage;
//上传文件的类型:text/plain
private String uploadImageContentType;
//上传文件的真是名称
private String uploadImageFileName;
public File getUploadImage() {
return uploadImage;
}
public void setUploadImage(File uploadImage) {
this.uploadImage = uploadImage;
}
public String getUploadImageContentType() {
return uploadImageContentType;
}
public void setUploadImageContentType(String uploadImageContentType) {
this.uploadImageContentType = uploadImageContentType;
}
public String getUploadImageFileName() {
return uploadImageFileName;
}
public void setUploadImageFileName(String uploadImageFileName) {
this.uploadImageFileName = uploadImageFileName;
}
public String saveFile(){
System.out.println("UploadAction *********** saveFile()");
ServletContext sc = ServletActionContext.getServletContext();
String path = sc.getRealPath("/fileupload");
File file = new File(path, uploadImageFileName);
try {
FileUtils.copyFile(uploadImage, file);
} catch (
IOException e) {
e.printStackTrace();
}
uploadImage.delete();
return "success";
}
}
------解决方案--------------------form 表单要写一个enctype="multipart/form-data"