<form action="/struts3/demo/file" method="post" enctype="multipart/form">
<input type="file" name="some" />
<input type="submit" value="submit" />
</form>
action:
public class FileAction {
private File some;//頁面name屬性名,用於接受上傳文件
private String someFileName;//固定格式頁面name屬性名+FileName,用於保存文件名
//說明:struts負責幫妳接受文件,但是不保存,需要程序員自己保存到指定地點
public String execute(){
if(some==null){
return "error";
}
String path = "WEN-INF/upload"+someFileName;
path = ServletActionContext.getServletContext().getRealPath(path);
CopyFile.copy(some, new File("path"));
//自己定義的文件復制方法,將struts攔截的文件copy到指定地點保存
return "success";
}
public File getSome() {
return some;
}
public void setSome(File some) {
this.some = some;
}
public String getSomeFileName() {
return someFileName;
}
public void setSomeFileName(String someFileName) {
this.someFileName = someFileName;
}
}