【问题标题】:How to open a File (image,pdf, utc...) without making it如何在不创建文件的情况下打开文件(图像、pdf、utc ...)
【发布时间】:2014-07-25 09:04:52
【问题描述】:

我在 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);
        }
    });

【问题讨论】:

  • 问题是什么?您想读取存储在数据库中的文件内容吗?你想在不创建文件的情况下写入文件(这会很奇怪)吗?使用后要删除文件吗?您的问题和代码似乎不一致。
  • 我想从数据库中读取文件并以正确的方式打开? Buisnes priview 是我得到了带有文件名的表,用户选择文件并打开它。文件可以是图像 pdf utc。
  • 您打算如何使用文件数据?您是否通过 InputStream 访问它?还是直接通过java.io.File?我们需要更多细节。
  • 只打开文件查看。用户可以在本地保存文件,然后根据需要上传...
  • @Speakwithsystem:如果您想打开文件以供查看,那么您为什么还要保存它(顺便说一句,您的问题不是说您不想创建文件)。为什么要上传刚刚从服务器检索到的文件?

标签: java file file-upload file-io


【解决方案1】:

您已将数据读入字节数组 (bytes)。由于我假设您需要InputStream,因此您可以使用ByteArrayInputStream

InputStream inputStream = new ByteArrayInputStream(bytes);

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

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

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

this one 等其他问题中已经讨论了创建虚拟文件/驱动器。然而,真正使用虚拟驱动器似乎有点过头了。

【讨论】:

  • 感谢您的回答以及如何打开输入流?我的意思是像桌面上的打开图像一样打开。
  • 我编辑了第一篇文章,因为我找不到问题。文件现在保存在@用户下载文件夹,但它们没有打开。因为 img 和 pdf 都在说“文件已损坏”:(
猜你喜欢
  • 2016-03-25
  • 2023-03-25
  • 2010-11-04
  • 1970-01-01
  • 1970-01-01
  • 2023-02-07
  • 2020-11-09
  • 1970-01-01
  • 2016-05-17
相关资源
最近更新 更多