导入Apache的upload和io包。
一、正确填写JSP表单
<form action="..." [color=red]enctype="multipart/form-data"[/color] method="post">
文件:<input type="file" [color=red]name="image[/color]">
<input type="submit" value="上传"/>
</form>
二、正确写Action类
private File [color=red]image[/color]; //与JSP表单中的file的name一样
private String [color=red]image[/color]FileName; //上传文件的真实名字
//都要提供get和set方法。
public String execute() throws Exception{
String realpath = ServletActionContext.getServletContext().getRealPath("/images");
System.out.println(realpath);
if(image!=null){
File savefile = new File(new File(realpath), imageFileName);
if(!savefile.getParentFile().exists()) savefile.getParentFile().mkdirs();
FileUtils.copyFile(image, savefile);
ActionContext.getContext().put("message", "上传成功");
}
return "success";
}
一、正确填写JSP表单
<form action="..." [color=red]enctype="multipart/form-data"[/color] method="post">
文件:<input type="file" [color=red]name="image[/color]">
<input type="submit" value="上传"/>
</form>
二、正确写Action类
private File [color=red]image[/color]; //与JSP表单中的file的name一样
private String [color=red]image[/color]FileName; //上传文件的真实名字
//都要提供get和set方法。
public String execute() throws Exception{
String realpath = ServletActionContext.getServletContext().getRealPath("/images");
System.out.println(realpath);
if(image!=null){
File savefile = new File(new File(realpath), imageFileName);
if(!savefile.getParentFile().exists()) savefile.getParentFile().mkdirs();
FileUtils.copyFile(image, savefile);
ActionContext.getContext().put("message", "上传成功");
}
return "success";
}