【问题标题】:how to keep the uploaded files in the form until submit it using jsf-2 and primefaces-3.4如何将上传的文件保留在表单中,直到使用 jsf-2 和 primefaces-3.4 提交
【发布时间】:2012-10-06 11:19:20
【问题描述】:

当我提交表单时,我的表单包含许多输入字段和 primefaces 组件来上传多个文件“p:fileUpload”。我无法获取上传的文件.. manged bean 是“RequestScoped”。那么如何在不做manged bean View范围的情况下获取上传的文件呢?

上传方式

    public void upload(FileUploadEvent event) {
    try {
        FacesMessage msg = new FacesMessage("Success! ", event.getFile().getFileName() + " is uploaded.");
        FacesContext.getCurrentInstance().addMessage(null, msg);
        // Do what you want with the file
        String thumbnail = getDestination() + event.getFile().getFileName();
        int index = thumbnail.lastIndexOf('.');
        SystemFile systemFile = new SystemFile();
        systemFile.setAccount(getActor().getAccount());
        systemFile.setName(event.getFile().getFileName());
        systemFile.setPath(getTalentPath());

        systemFile.setFileType(FileUtil.checkFileType(thumbnail.substring(index + 1)));
        if (systemFiles == null) {
            systemFiles = new ArrayList<>();
        }
        systemFiles.add(systemFile);
        copyFile(event.getFile().getFileName(), event.getFile().getInputstream());
    } catch (IOException ex) {
        SystemLogger.getLogger(getClass().getSimpleName()).error(null, ex);
    }
}

primefaces 组件

    <p:fileUpload label="#{TalentMessages.lbl_Select_File}" fileUploadListener="#{talentPropertyAction.upload}"
                                  mode="advanced"
                                  multiple="true"
                                  uploadLabel="#{TalentMessages.lbl_upload_File}"
                                  cancelLabel="#{TalentMessages.lbl_cancel_File}"
                                  sizeLimit="2000000"
                                  oncomplete="completeUploadFile(#{talentPropertyAction.talentId});"
                                  />

然后是保存功能

    @Setter
@Getter
private List<SystemFile> systemFiles;
try {
 // save something else then save the files
        if (systemFiles != null) {
            System.out.println("Not Null" + systemFiles);
            for (SystemFile systemFile : systemFiles) {
                TalentPropertyFile talentPropertyFile = new TalentPropertyFile();
                talentPropertyFile.setTalentProperty(talentProperty);
                talentPropertyFile.setFile(systemFile);
                getTalentService().save(getActor().getAccount(), talentPropertyFile);
            }
        } else {
            System.out.println("Null");
        }
    } catch (InvalidParameter ex) {
        SystemLogger.getLogger(getClass().getName()).error(null, ex);
    }

【问题讨论】:

  • 请发布相关的 JSF 和 Java Bean 代码,您也可以使用 java 和 Spring/JEE 对其进行标记
  • 在这里获取 jsf 页面和 javaBean 代码需要做很多工作,对不起,问题是当我上传文件时,它会向 bean 发送请求并成功上传,但是当我完成表单和提交表单的方法看不到上传的文件
  • 不是全部 - 只是相关部分,如果我们不知道您的代码是什么样的,我们将如何为您提供帮助!
  • 为什么不给用户一个input字段,只给用户一个upload按钮呢?我的意思是,用户先选择要上传的文件,然后再一起上传。
  • @dngfng :我更新了代码。我希望它可以帮助你帮助我:)

标签: java spring jsf primefaces multifile-uploader


【解决方案1】:

那么如何在不做manged bean View范围的情况下获取上传的文件呢?

只需将上传信息立即存储在更永久的位置,而不是作为请求范围 bean 的属性,无论如何都会在请求响应结束时被丢弃(注意:每次上传都算作一个单独的 HTTP 请求)。

public void upload(FileUploadEvent event) {
    // Now, store on disk or in DB immediately. Do not assign to a property.
}

public void save() {
    // Later, during submitting the form, just access them from there.
}

如果您需要一些密钥来访问它们,请考虑将密钥存储在会话范围内。

【讨论】:

  • 现在我有两个选择:1- 将文件存储在磁盘上,但我怎样才能将它们取回 2- 将文件存储在 DB 上,但我的 DB 表需要我上传文件的人才 ID 和这个选项让我请求数据库的数量与文件数量一样多(性能不佳)
  • 我不建议将文件存储在数据库中,仅将文件的路径存储在磁盘中,在数据库中需要的空间要少得多。
  • 如前所述,“如果您需要一些密钥来访问它们,请考虑将密钥存储在会话范围内”。如有必要,您可以只使用当前登录的用户作为密钥。至于性能,测量就是知道。是否将文件存储在数据库中仅仅是可移植性/可重用性/可维护性/语义问题,而不是性能问题。无论如何,它最终只是相同的磁盘文件系统硬件。
猜你喜欢
  • 2013-07-23
  • 2012-03-26
  • 2012-03-19
  • 2012-04-04
  • 1970-01-01
  • 2013-09-07
  • 2015-07-01
  • 2017-09-26
  • 1970-01-01
相关资源
最近更新 更多