在 html 页面有 <input type="file" name="certificate" onchange="displayFileValue(this);"  class="huan2"/>
有个按纽能 动态生成 上面的代码,用来实现同时上传多个文件 action 内 接收代码:protected List<File> certificates;
protected List<String> certificateFileNames;
Get/Setpublic List<File> getCertificate() {
return certificates;
}public void setCertificate(List<File> certificates) {
this.certificates = certificates;
}public List<String> getCertificateFileName() {
return certificateFileNames;
}public void setCertificateFileName(List<String> certificateFileNames) {
this.certificateFileNames = certificateFileNames;
}
上面的代码,当我上传一个文件的时候没问题当上传多个文件时,就获取不到 文件` 在线等待`                                   By 巧顾网

解决方案 »

  1.   

    给你看看我的代码:
    private String title;
    private List<File> upload;
    private List<String> uploadContentType;
    private List<String> uploadFileName; // 接受依赖注入的属性
    private String savePath; // 接受依赖注入的方法
    public void setSavePath(String value) {
    this.savePath = value;
    } private String getSavePath() throws Exception {
    return ServletActionContext.getRequest().getRealPath(savePath);
    } public void setTitle(String title) {
    this.title = title;
    } public String getTitle() {
    return (this.title);
    } public List<File> getUpload() {
    return upload;
    } public void setUpload(List<File> upload) {
    this.upload = upload;
    } public List<String> getUploadContentType() {
    return uploadContentType;
    } public void setUploadContentType(List<String> uploadContentType) {
    this.uploadContentType = uploadContentType;
    } public List<String> getUploadFileName() {
    return uploadFileName;
    } public void setUploadFileName(List<String> uploadFileName) {
    this.uploadFileName = uploadFileName;
    } @Override
    public String execute() throws Exception {

    for (int i = 0; i < upload.size(); i++)
    {
    InputStream is = new FileInputStream(upload.get(i)); String root = ServletActionContext.getRequest().getRealPath(
    "/upload"); File destFile = new File(root, this.getUploadFileName().get(i)); OutputStream os = new FileOutputStream(destFile); byte[] buffer = new byte[400]; int length = 0; while ((length = is.read(buffer)) > 0) {
    os.write(buffer, 0, length);
    } is.close(); os.close();

    Dao dao = new Dao();
    dao.getMessage();
    String[] filecount = dao.getL2().getItems();
    int len = filecount.length;
    int no;
    if(len == 0)
    {
    no = 1;
    }
    else no = Integer.parseInt(filecount[len-1])+1;
    dao.insert(no,this.getUploadFileName().get(i));

    }
    return "success";
    }

    public void validate()
    {
    if(upload == null || upload.size() <=0 )
    {
    this.addFieldError("upload", "上传文件不能为空!");
    }
    }
    }