0

I have a stored Files @ Oracle DB at LONG type column. Now i need to open it without making the same file. How it can be done ?

Sample code who makes the file at Eclipse workplace. How to adjust it do not making File at file system ?

Thanks for answers! The right answers guaranteed!

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);
        }
    });
  • 1
    What is the question? Do you want to read the file's contents stored in the DB? Do you want to write to a file without creating it (which would be weird)? Do you want to delete the file after using it? Your question and the code do not seem to align. – SirRichie Jun 04 '14 at 15:56
  • I want to read file from DB and Open in what will be the corect way ? Buisnes priview is that i got Table with file names, users choose the file and open it. File can be image pdf utc. – Speak with system Jun 04 '14 at 15:58
  • And how do you plan on using the file data? Do you access it via an InputStream? Or directly through java.io.File? We need more details. – SirRichie Jun 04 '14 at 16:01
  • Only open file for view. User can save the File localy and then upload if they need... – Speak with system Jun 04 '14 at 16:03
  • @Speakwithsystem: If you **only** want to open the file for view, then why do you want to save it too (and btw, doesn't your question say that you **don't** want to create a file). And why should a file be uploaded, that's just been retrieved from the server? – fabian Jun 04 '14 at 16:19
  • I tested the code what i give as sample, it's maked a File @Windows file system 'leslie2.gif' @ eclipse work direction.Of course then i can select the newly created 'leslie2.gif' and open it. But why i need to create file @ file system if i need only open it from stored DB LONG column ? Are some others ways how to do it in Java ? – Speak with system Jun 04 '14 at 19:27

1 Answers1

1

You already read the data into a byte array (bytes). Since I assume you need a InputStream you can use ByteArrayInputStream.

InputStream inputStream = new ByteArrayInputStream(bytes);

Most libraries support reading from InputStreams, not just from files.

If you want to open a file with other programs:

You don't know, if they support receiving data through other means than the file sytem. That however would mean you have to create something like a virtual file, that may have to work even after the java program terminates.

Creating a virtual files / drives are already discussed in other questions like this one. However really using a virtual drive seems to be a total overkill.

Community
  • 1
  • 1
fabian
  • 80,457
  • 12
  • 86
  • 114