Struts2.0实现文件上传

1.struts2.0上传三个关键字段 上传文件名、上传文件类型以及上传文件的File对象,如果jsp前台页面代码如下:

  <s:file name="xxx" label="输入要上传的文件名" />

 则action中三个字段的set,get方法必须分别为 File getxxx,String getxxxContentType,String getxxxFileName,该命名规则可从FileUploadInterceptor拦截器下面部份代码可知:

          parameters.put(inputName, files);
          parameters.put(inputName + "ContentType", contentType);
          parameters.put(inputName + "FileName", fileName);

其中inputName就是<s:file>中的name.

 

2.struts2.0上传多个文件三个字段的set,get方法的配置如下(其中<s:file>中name为upload):

private java.util.List<File> uploads; 
private java.util.List<String> fileNames; 
private java.util.List<String> uploadContentTypes; 
public java.util.List<String> getUploadFileName() 
  {
        return fileNames; 
   } 
  public void setUploadFileName(java.util.List<String> fileNames) 
   {
        this.fileNames = fileNames;
    }
    public java.util.List<File> getUpload()
    {
        return uploads;
    }

    public void setUpload(java.util.List<File> uploads)
    {
        this.uploads = uploads;
    }

    public void setUploadContentType(java.util.List<String> contentTypes)
    {
        this.uploadContentTypes = contentTypes; 
    }

    public java.util.List<String> getUploadContentType()
    {
        return this.uploadContentTypes;
    }
由上面代码可知:三个字段可任意取名称,但set,get方法必须按照文件拦截器中定义的规则.

你可能感兴趣的:(java,jsp)