如何在 xpages 中从 Java 传递对文件的引用
How to pass a reference to a file from Java in xpages
对不起我的英语。我用翻译器。 Java 有一个代码。从数据库中读取字段Posgresql bytea二进制数据存入文件:
<code>
public static void saveToFile() throws IOException {
try {
Connection conn = JdbcUtil.getConnection(FacesContext.getCurrentInstance(), "derby1");
ResultSet rs = conn.createStatement().executeQuery(
"SELECT files FROM test_goverment where who='d'");
byte[] imgBytes = null;
if (rs != null) {
while (rs.next()) {
imgBytes = rs.getBytes(1);
}
FileOutputStream os = new FileOutputStream("c:\samoutput.txt");
os.write(imgBytes);
os.flush();
os.close();
}
rs.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
enter code here}
}
</code>
如何在 Xpages 中将要下载的文件传输给用户。下载后删除。
您只想将查询结果传输到浏览器客户端下载,不想保存。
而不是先将查询结果保存到文件中,然后再传输以供下载
您可以 stream 内容 direct 到 XPage 的 response 像这样:
public static void downloadFile() throws IOException {
try {
FacesContext facesContext = FacesContext.getCurrentInstance();
Connection conn = JdbcUtil.getConnection(facesContext, "derby1");
ResultSet rs = conn.createStatement().executeQuery(
"SELECT files FROM test_goverment where who='d'");
byte[] imgBytes = null;
if (rs != null) {
while (rs.next()) {
imgBytes = rs.getBytes(1);
}
ExternalContext extCon = facesContext.getExternalContext();
XspHttpServletResponse response = (XspHttpServletResponse)extCon.getResponse();
response.reset();
response.setContentType("text/plain");
response.setHeader("Content-disposition", "attachment; filename=output.txt");
response.setHeader("Cache-Control", "no-cache");
OutputStream os = response.getOutputStream();
os.write(imgBytes);
os.flush();
facesContext.responseComplete();
rs.close();
}
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
您也可以看看这个 XSnippet。它向您展示了如何显示文件以供下载,以防您 want/have 继续使用文件而不是流。
对不起我的英语。我用翻译器。 Java 有一个代码。从数据库中读取字段Posgresql bytea二进制数据存入文件:
<code>
public static void saveToFile() throws IOException {
try {
Connection conn = JdbcUtil.getConnection(FacesContext.getCurrentInstance(), "derby1");
ResultSet rs = conn.createStatement().executeQuery(
"SELECT files FROM test_goverment where who='d'");
byte[] imgBytes = null;
if (rs != null) {
while (rs.next()) {
imgBytes = rs.getBytes(1);
}
FileOutputStream os = new FileOutputStream("c:\samoutput.txt");
os.write(imgBytes);
os.flush();
os.close();
}
rs.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
enter code here}
}
</code>
如何在 Xpages 中将要下载的文件传输给用户。下载后删除。
您只想将查询结果传输到浏览器客户端下载,不想保存。
而不是先将查询结果保存到文件中,然后再传输以供下载
您可以 stream 内容 direct 到 XPage 的 response 像这样:
public static void downloadFile() throws IOException {
try {
FacesContext facesContext = FacesContext.getCurrentInstance();
Connection conn = JdbcUtil.getConnection(facesContext, "derby1");
ResultSet rs = conn.createStatement().executeQuery(
"SELECT files FROM test_goverment where who='d'");
byte[] imgBytes = null;
if (rs != null) {
while (rs.next()) {
imgBytes = rs.getBytes(1);
}
ExternalContext extCon = facesContext.getExternalContext();
XspHttpServletResponse response = (XspHttpServletResponse)extCon.getResponse();
response.reset();
response.setContentType("text/plain");
response.setHeader("Content-disposition", "attachment; filename=output.txt");
response.setHeader("Cache-Control", "no-cache");
OutputStream os = response.getOutputStream();
os.write(imgBytes);
os.flush();
facesContext.responseComplete();
rs.close();
}
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
您也可以看看这个 XSnippet。它向您展示了如何显示文件以供下载,以防您 want/have 继续使用文件而不是流。