1、------------------------------------------jsp页面---------------------------------------------------------------
<s:form action="upload" name="upForm" method="post" enctype="multipart/form-data">
<s:textfield name="title" label="文件标题" /><br/>
<s:file name="file" label="选择文件1" />
<s:file name="file" label="选择文件2" />
<s:file name="file" label="选择文件3" />
<s:submit value="上传"/>
</s:form>
2、-----------------------------------------action类---------------------------------------------------------------
public class uplaodAction extends ActionSupport{
private List<File> file;
private List<String> fileFileName;
private List<String> fileContentType;
//这个属性是配置struts.xml文件来注入的
private String savePath;
//这个属性用来存放所有允许上传的文件类型的后缀
private String allowTypes;
//这个属性用来存放限制上传文件的大小的值
private long allowSize;
public String execute() throws Exception {
System.out.println("看看路径"+getSavePath());
try{
//循环写入
for(int i=0;i<file.size();i++){
//以服务器的文件保存地址和原文件名建立上传文件输出流
FileOutputStream fos=new FileOutputStream(getSavePath()+"\\"+this.getFileFileName().get(i));
//建立输入流
FileInputStream fis=new FileInputStream(file.get(i));
//建立缓冲区
byte[] buffer=new byte[1024];
int length=0;
//循环从缓冲区读取内容到输入流中
while((length=fis.read(buffer))>0){
fos.write(buffer, 0, length);
}
}
}catch(Exception e){
e.printStackTrace();
}
return SUCCESS;
}
//实现上传文件过滤
public String filterTypesAndSize(String[] types,Long size){
System.out.println("fileterTypesAndSize");
List<String> fileTypes=this.getFileContentType();
if(fileTypes.size()>0 ){
for(int i=0;i<fileTypes.size();i++){
if(this.getFile().get(i).length()<size){
for(int j=0;j<types.length;j++){
if(fileTypes.get(i).equals(types[j])){
return null;
}
}
}
}
}
return ERROR;
}
public String fileter(String[] types){
List<String> upFileTypes=this.getFileContentType();
for(String type : types){
for(String upfiletype : upFileTypes){
if(type.equals(upfiletype)){
return null;
}
}
}
return ERROR;
}
//用Strust2的输入校验来判断用户上传的文件是否是允许上传的类型
public void validate(){
System.out.println("validate");
if(this.getFile() != null){
for(File uploadFile : this.getFile()){
//将允许上传文件类型的字符串以逗号(,)分解成字符串数组,从而判断当前文件类型是否允许上传
//String filterResult = this.filterTypesAndSize(this.getAllowTypes().split(","),allowSize);
String filterResult=this.fileter(this.getAllowTypes().split(","));
if(filterResult != null){
addFieldError("msg", "你要上传的文件类型不正确或大小超出限制");
}
}
}
}
注:属性的setter和getter方法省略
3、----------------------------------------struts.xml文件------------------------------------------------------------------------------------------------------
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="true" />
<constant name="struts.custom.il8n.resources" value="mess" />
<constant name="struts.il8n.encoding" value="utf-8"></constant>
<package name="default" namespace="/" extends="struts-default">
<action name="upload" class="com.test.action.uplaodAction">
<param name="savePath">/upload</param>
<param name="allowTypes">image/png,image/jpg,image/gif,image/jpeg</param>
<param name="allowSize">10240000</param>
<result name="success">success.jsp</result>
<result name="input" >/WEB-INF/error.jsp</result>
</action>
</package>