gpt4 book ai didi

java - 如何在不创建文件的情况下打开文件(图像、pdf、utc...)

转载 作者:行者123 更新时间:2023-12-01 12:53:35 25 4
gpt4 key购买 nike

我在 LONG 类型列上存储了 Files @ Oracle DB。现在我需要打开它而不创建相同的文件。怎样才能做到呢?

在 Eclipse 工作场所创建文件的示例代码。如何调整它不在文件系统中创建文件?

谢谢各位的解答!保证正确答案!

session.doWork(new Work() {
public void execute(Connection connection) throws SQLException {
String sql = (" SELECT failas,aprasymas,ispletimas FROM ZALA.claims_additional_doc_v where id = ? ");
PreparedStatement stmt = null;
ResultSet rs = null;
try {
stmt = connection.prepareStatement(sql);
stmt.setLong(1, papId);
rs = stmt.executeQuery();
if (rs.next()) {
byte[] bytes = rs.getBytes(1);
String fileName = rs.getString(2);
// String fileType = rs.getString(3);
FileOutputStream fileO = null;
String userHomeFolder = System.getProperty("user.home") + "\\Downloads";
try {
fileO = new FileOutputStream(userHomeFolder + "\\" + fileName);
fileO.write(bytes);
fileO.close();
File file = new File(userHomeFolder + "\\" + fileName);
if (Desktop.isDesktopSupported()) {
try {
if (file.toString().endsWith(".pdf"))
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + file);
else {
Desktop desktop = Desktop.getDesktop();
desktop.open(file);
}
} catch (IOException ex) {
log.debug("err @ runtime" + ex.getMessage());
}
}
} catch (Exception e) {
String err = e.toString();
log.debug("err @ stream" + err);
} finally {
if (fileO != null) {
fileO.close();
}
}
}
} catch (Exception e) {
e.printStackTrace();
new DisnetErrorHandler().handleError(e);
}
SaveUtility.closeRsAndStmt(rs, stmt);
}
});

最佳答案

您已经将数据读入字节数组 (bytes)。因为我假设您需要一个 InputStream 您可以使用 ByteArrayInputStream .

InputStream inputStream = new ByteArrayInputStream(bytes);

大多数库支持从 InputStream 读取,而不仅仅是从文件读取。

如果您想用其他程序打开文件:

您不知道它们是否支持通过文件系统以外的其他方式接收数据。然而,这意味着您必须创建类似虚拟文件的东西,即使在 java 程序终止后它也可能必须工作。

创建虚拟文件/驱动器已经在其他问题中讨论过,例如 this one 。然而,真正使用虚拟驱动器似乎完全是矫枉过正。

关于java - 如何在不创建文件的情况下打开文件(图像、pdf、utc...),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24042095/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com