【问题标题】:primefaces5 jsf2 multiple upload e scan the elementsprimefaces 5 jsf 2 多次上传扫描元素
【发布时间】:2015-11-13 10:43:39
【问题描述】:

大家好,我在 PrimeFaces/Jsf2 中上传更多文件时遇到问题

我看到http://www.primefaces.org/showcase/ui/file/upload/multiple.xhtml

当方法拦截我设置的关联事件时

上传文件文件上传 = event.getFile();

我想扫描每一个使用 List 实现上传的文件

    InputStream input;
    input = event.getFile().getInputstream();
    pojo.setFileInputStream(input);
    input.close();
    fileTableList.add(pojo);

但是最大的问题是这个列表只包含一个上传的文件。 如何获取从 UploadedFile 事件上传的每个文件?

怎么了? 谢谢你的回答

【问题讨论】:

  • 您将需要存储InputStreambyte[] 甚至UploadedFile,具体取决于从上传到集合或数组(即fileUploadListener 内部)的文件中提取的需要。
  • 我认为是这样,但是字节数组的每个 i 元素都是 UploadedFile?
  • 我的问题是我用前一个文件控制每个 FileUploaded 的名称,所以我想要每个文件的名称列表。
  • 集合的类型取决于需求。可能是InputStreambyte[] 甚至是UploadedFile 或其他。文件名可以通过uploadedFile.getFileName()获取。因此,您需要根据需要决定将哪种类型的信息存储到集合中。您可能只需要byte[] 和文件名,InputStream 和文件名或整个UploadedFile 对象...
  • 是的,文件名可以通过使用uploadedFile.getFileName() 获得,但是我如何捕获每个文件名并将它们存储在列表中?此方法以递归方式同时显示所有名称。

标签: jsf file-upload jsf-2 primefaces multi-upload


【解决方案1】:

但最大的问题是这个列表只包含一个文件 上传。如何获取从UploadedFile 上传的每个文件 活动?

除非您明确说明具有最小可复制示例,否则无法使用具有最少可能依赖项/资源的最小示例来复制。


创建一个如下所示的实用程序类(该类完全取决于需求)。

public class FileUtil implements Serializable {

    private InputStream inputStream; // One can also use byte[] or something else.
    private String fileName;
    private static final long serialVersionUID = 1L;

    public FileUtil() {}

    // Overloaded constructor(s) + getters + setters + hashcode() + equals() + toString().
}

托管 bean 接收多个文件:

@Named
@ViewScoped
public class TestBean implements Serializable {

    private List<FileUtil> fileList;
    private static final long serialVersionUID = 1L;

    public TestBean() {}

    @PostConstruct
    public void init() {
        fileList = new ArrayList<>();
    }

    public void fileUploadListener(FileUploadEvent event) throws IOException {
        UploadedFile file = event.getFile();
        FileUtil fileUtil = new FileUtil();
        fileUtil.setInputStream(file.getInputstream());
        fileUtil.setFileName(file.getFileName());
        fileList.add(fileUtil);
    }


    // Bound to a <p:commandButton>.

    public void action() {
        for (FileUtil fileUtil : fileList) {
            System.out.println(fileUtil.getFileName());
        }

        // Use the list of files here and clear the list afterwards, if needed.
        fileList.clear();
    }
}

XHTML 文件仅包含 &lt;p:fileUpload&gt;&lt;p:commandButton&gt; 只是为了演示。

<h:form id="form">
    <p:fileUpload id="fileUpload"
                  mode="advanced"
                  fileLimit="5"
                  multiple="true"
                  allowTypes="/(\.|\/)(gif|jpe?g|png)$/"
                  sequential="true"
                  process="@this"
                  fileUploadListener="#{testBean.fileUploadListener}">
    </p:fileUpload>

    <p:commandButton value="Submit"
                     process="@this"
                     update="fileUpload"
                     actionListener="#{testBean.action}"/>
</h:form>

如果您需要byte[] 代替InputStream,那么只需将FileUtil 类中的private InputStream inputStream; 更改为byte[],然后使用

byte[] bytes = IOUtils.toByteArray(uploadedFile.getInputstream());

InputStream 中提取一个字节数组(其中IOUtils 来自org.apache.commons.io。您也可以手动编写几行代码)。

您也可以构造一个List&lt;UploadedFile&gt;,而无需像本例中那样创建像FileUtil 这样的附加类,但如果您碰巧使用该层,那么这样做会要求服务层依赖PrimeFaces(这不应该发生)在您的应用程序中,因为 UploadedFile 是 PrimeFaces 工件。毕竟这完全取决于需求。

【讨论】:

  • 感谢 Tiny,问题是我无法对每个文件名进行控制操作,因为只有一个实例包含所有文件名;我也想过字节[],但我的应用程序留在云系统上,我不能在物理内存上写
  • 我没有得到术语,“控制操作”。
  • 文件名称的顺序控制..我想迭代一个名称文件列表。 FileUtil 的列表不会在每个文件名中迭代
  • 我不知道怎么解释。它为每个上传的文件重复指令。如果我在上传 4 个文件后启动 System.out.println(fileUtil.getFileName()),输出为: element1 -> element1 element2 -> element1 element2 element3 -> element1 element2 element3 element4;如果我启动一个超出列表大小的系统,则输出为:1-> 2 -> 3 -> 4。它使用(我认为)递归或类似的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-05
  • 2012-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-05
  • 2013-07-23
相关资源
最近更新 更多