之前报struts.multipart.saveDir错误,配置过后不报错了,但也上传不了文件了,现在完全蒙圈了
action代码,action配置是最简单配置,这是别人的代码,我拿过来测试的,自己项目跟这个差不多,情况也一样
public class FileUploadAction extends ActionSupport{
private File image;
//获得文件的文件名,格式为:nameFileName,这里格式中的name为jsp页面中的name属性
private String imageFileName;
public String getImageFileName() {
return imageFileName;
}
public void setImageFileName(String imageFileName) {
this.imageFileName = imageFileName;
}
public File getImage() {
return image;
}
public void setImage(File image) {
this.image = image;
}
public String execute() throws Exception{
//获得要存放图片的绝对路径
String realpath = ServletActionContext.getServletContext().getRealPath("/user/usericon");
System.out.println(realpath);
//在路径下创建上传的文件
File savefile = new File(new File(realpath),imageFileName);
if(image!=null){
if(!savefile.getParentFile().exists()){
//如果路径不存在,则创建一个
savefile.getParentFile().mkdirs();
//把上传的文件保存在路径中
FileUtils.copyFile(image, savefile);
ActionContext.getContext().put("message", "上传成功");
System.out.println("上传成功");
return "seccuss";
}
}else{
System.out.println("文件为空");
return "error";
}
return "error";
}
public String getfile(){
System.out.println(imageFileName);
return "error";
}
}
jsp代码
文件: