我将文件存储为数据库中的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>
这是我的bean代码:
public DefaultStreamedContent downloadFile(Integer invID) {
System.out.println("Downloading inv " + invID);
DefaultStreamedContent toReturn = null;
try {
toReturn = dbConnector.downloadinv(invID);
} catch (SQLException ex) {
Logger.getLogger(invBean.class.getName()).log(Level.SEVERE, null, ex);
}
return toReturn;
}
这是我的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
答案 0 :(得分:0)
我猜p:fileDownload
正在渲染时执行该方法,因为它会评估value
属性。如果inv
是迭代变量的名称,则应将组件编写为:
<p:commandLink id="downloadLink" value="Download" ajax="false">
<p:fileDownload value="#{inv.file}" />
</p:commandLink>
在inv
的课程中,添加getFile
方法:
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();
.....
}