PrimeFaces数据表中的文件下载问题挂起

PrimeFaces数据表中的文件下载问题挂起,primefaces,datatable,javabeans,Primefaces,Datatable,Javabeans,我将文件存储为数据库中的blob。我想将它们与其他信息一起显示在数据表中。我试图在单击超链接时从数据表下载文件。我无法单击超链接,当表格加载时,我在第一页的日志中看到整个页面的下载消息(我将页面大小设置为10),因此我看到10条消息。我还看到来自JDBC的消息10次给了我不同的文件名 这是我的xhtml: </p:column> <p:column sortBy="#{inv.sizeMB}" filterBy="#{inv.sizeMB}"&g

我将文件存储为数据库中的blob。我想将它们与其他信息一起显示在数据表中。我试图在单击超链接时从数据表下载文件。我无法单击超链接,当表格加载时,我在第一页的日志中看到整个页面的下载消息(我将页面大小设置为10),因此我看到10条消息。我还看到来自JDBC的消息10次给了我不同的文件名

这是我的xhtml:

            </p:column>
    <p:column sortBy="#{inv.sizeMB}" filterBy="#{inv.sizeMB}">
        <f:facet name="header">
            <h:outputText value="File Name" />
        </f:facet>
            <p:commandLink id="downloadLink" value="Download" ajax="false">
                <p:fileDownload value="#{inv.downloadFile(inv.id)}" />
            </p:commandLink>
    </p:column>
</p:dataTable>
这是我的JDBC代码:

         public DefaultStreamedContent downloadinv(Integer invID) throws SQLException   {
    DefaultStreamedContent toReturn = null;
    PreparedStatement downloadStatement = dbConn.prepareStatement("SELECT "
            + " FILE_NAME, FILE_DATA FROM inv.INV_META WHERE INV_ID=?");
    java.sql.Blob blob;
    downloadStatement.setInt(1, invID);
    ResultSet downloadResults = null;
    downloadResults = downloadStatement.executeQuery();

    if (downloadResults != null && downloadResults.next()) {
        blob = downloadResults.getBlob("FILE_DATA");
        int iLength = (int) (blob.length());
        if(iLength > 0) {
            String fileName = downloadResults.getString("FILE_NAME");
            String fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
            System.out.println("File extension of " + fileName + ": " + fileType);

            byte barr[] = new byte[(int) blob.length()];
            barr = blob.getBytes(1, (int) blob.length());
            InputStream is = new ByteArrayInputStream(barr);

            String fileExt = "";
            if (fileType.trim().toLowerCase().contains("zip")) {
                fileExt = "application/zip";
            } else if (fileType.trim().toLowerCase().contains("doc")) {
                fileExt = "application/msword";
            } else if (fileType.trim().toLowerCase().contains("pptx")) {
                fileExt = "application/vnd.openxmlformats-officedocument.presentationml.inv";
            } else if (fileType.trim().toLowerCase().contains("ppt")) {
                fileExt = "application/ppt";
            } else if (fileType.trim().toLowerCase().contains("pdf")) {
                fileExt = "application/pdf";
            } else {
                fileExt = "application/octet-stream";
            }
            try {
                is.close();
            } catch (IOException ex) {
                Logger.getLogger(InvJDBC.class.getName()).log(Level.SEVERE, null, ex);
            }
            System.out.println(barr);
            toReturn = new DefaultStreamedContent(is, fileExt, fileName);
        }
    }
    downloadResults.close();
    downloadStatement.close();
    return toReturn;
}

我在日志中看到以下内容:

    Downloading inv 2468
    File extension of inv.txt: txt
    [B@1a3e5ad4
    Downloading inv 2470
    File extension of inv.txt: txt
    [B@17264b15
    Downloading inv 2471
    File extension of inv.txt: txt
    [B@1d95e165
    Downloading inv 2472
    File extension of inv.txt: txt
    [B@7d624027
    Downloading inv 2473
    File extension of inv.txt: txt
    [B@6689be00
    Downloading inv 2474
    File extension of inv.txt: txt
    [B@645bc2c4
    Downloading inv 2466
    File extension of inv.txt: txt
    [B@39300c32
    Downloading inv 2467
    File extension of inv.txt: txt
    [B@7d16339e
    Downloading inv 2475
    File extension of inv.txt: txt
    [B@43379047
    Downloading inv 2476
    File extension of inv.txt: txt
    [B@3cb0d361    

我猜
p:fileDownload
在渲染时正在执行该方法,因为它计算
属性。如果
inv
是迭代变量的名称,则应将组件编写为:

<p:commandLink id="downloadLink" value="Download" ajax="false">
      <p:fileDownload value="#{inv.file}" />
</p:commandLink>

您在日志中看到的“整个页面的下载消息”是什么?我在日志中看到以下内容:只是猜测:尝试通过
p:commandButton
更改
p:commandLink
。如上所述,混合使用
p:commandLink
p:fileDownload
尝试的命令按钮时出现一些问题,无法工作。我的bean具有属性@SessionScoped@ManagedBean(name=“invBean”,eager=true)。我从这个bean调用downloadinv方法来下载文件。这个bean还用于填充数据表。我需要将下载代码移出bean吗?@user7517586很高兴能提供帮助。如果此答案正确,请将其标记为正确,以帮助其他有类似问题的人。
<p:commandLink id="downloadLink" value="Download" ajax="false">
      <p:fileDownload value="#{inv.file}" />
</p:commandLink>
public DefaultStreamedContent getFile() throws SQLException   {
    DefaultStreamedContent toReturn = null;
    PreparedStatement downloadStatement = dbConn.prepareStatement(
                              "SELECT FILE_NAME, FILE_DATA "+
                              "  FROM inv.INV_META "+
                              " WHERE INV_ID=?");
    java.sql.Blob blob;
    downloadStatement.setInt(1, invID);
    ResultSet downloadResults = null;
    downloadResults = downloadStatement.executeQuery();

    .....
}