0

I found this example how to read binary file from PostgreSQL.

conn.setAutoCommit(false);

        // Get the Large Object Manager to perform operations with
        LargeObjectManager lobj = ((org.postgresql.PGConnection) conn).getLargeObjectAPI();

        ps = conn.prepareStatement("SELECT FILE FROM KNOWLEDGEBASE_FILES WHERE ID = ?");
        ps.setInt(1, 333);
        ResultSet rs = ps.executeQuery();
        while (rs.next())
        {
            // Open the large object for reading
            long oid = rs.getLong(1);
            LargeObject obj = lobj.open(oid, LargeObjectManager.READ);

            // Read the data
            byte buf[] = new byte[obj.size()];
            obj.read(buf, 0, obj.size());
            // Do something with the data read here


            FileChannel rwChannel = new RandomAccessFile("License_Agreement.pdf", "rw").getChannel();
            ByteBuffer wrBuf = rwChannel.map(FileChannel.MapMode.READ_WRITE, 0, buffer.length * number_of_lines);
            for (int i = 0; i < obj.size(); i++)
            {
                wrBuf.put(obj);
            }

            // Close the object
            obj.close();
        }
        rs.close();
        ps.close();

        // Finally, commit the transaction.
        conn.commit();

How I can write the data into file using NIO?

At this line wrBuf.put(obj); I get error:

no suitable method found for put(LargeObject) method ByteBuffer.put(byte) is not applicable (argument mismatch; LargeObject cannot be converted to byte) method ByteBuffer.put(ByteBuffer) is not applicable (argument mismatch; LargeObject cannot be converted to ByteBuffer) method ByteBuffer.put(byte[]) is not applicable (argument mismatch; LargeObject cannot be converted to byte[])

1 Answer 1

1

You could use LargeObject::getInputStream:

import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
//...
while (rs.next())
{
    long oid = rs.getLong(1);
    LargeObject obj = lobj.open(oid, LargeObjectManager.READ);
    Path targetPath = FileSystems.getDefault().getPath("License_Agreement.pdf");
    Files.copy(obj.getInputStream(), targetPath, StandardCopyOption.REPLACE_EXISTING);
    obj.close();
}
Sign up to request clarification or add additional context in comments.

5 Comments

For some reason the file is not created after I run the code.
the file is not writeable, has the user the necessary rights? did you get a stacktrace?
Yes, there are no write restrictions
I found the problem. How I can configure the Java code to write in JUnit resource directory - src\test\resources
this should do it: FileSystems.getDefault().getPath("src/test/resources", "License_Agreement.pdf"); (see: docs.oracle.com/javase/7/docs/api/java/nio/file/…)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.