1.jsp页面
<form action="Message_file" method="post" enctype="multipart/form-data">
file upload:<input type="file" name="photo"/><br>
file upload:<input type="file" name="photo"/><br>
<input type="submit" value="上传"/>
</form>
注意:(1).enctype="multipart/form-data"
(2).type="file"
2.action类
public class MessageAction implements ModelDriven<Message>{
private File[] photo;
private String[] photoFileName;
private String[] photoContentType;
public File[] getPhoto() {
return photo;
}
public void setPhoto(File[] photo) {
this.photo = photo;
}
public String[] getPhotoFileName() {
return photoFileName;
}
public void setPhotoFileName(String[] photoFileName) {
this.photoFileName = photoFileName;
}
public String[] getPhotoContentType() {
return photoContentType;
}
public void setPhotoContentType(String[] photoContentType) {
this.photoContentType = photoContentType;
}
public String fileInput() {
return "success";
}
public String file() {
System.out.println(msg.getTitle());
try {
for(int i=0;i<photo.length;i++) {
File p=photo[i];
String pfn=photoFileName[i];
FileUtils.copyFile(p, new File("d:/test/"+pfn));
}
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return "success";
}
}
注意:(1):action有3个属性,第一个是文件数组,第二个是文件名数组(格式:xxFileName),第三个是文件类型数组(格式:xxContentType)
(2):3个属性都必须设置getter,setter!
3.struts.xml配置可上传的文件大小
<constant name="struts.multipart.maxSize" value="10485760"/>
1MB=1024KB,1KB=1024B(字节),一个英文字母占用1个字节,一个汉字占用2个字节