Convierta PDF a Base64 y almacene datos en BLOB de base de datos

I want to binary data (e.g. a PDF) into a BLOB of my Oracle database. At first I putted the PDF into a FileInputStream and created a byte-array.Here is the code for that:

public static byte[] createByteArray(File pCurrentFolder, String pNameOfBinaryFile)
    {

        String pathToBinaryData = pCurrentFolder.getAbsolutePath()+"/"+pNameOfBinaryFile;

        File file = new File(pathToBinaryData);
        if (!file.exists())
        {
            System.out.println(pNameOfBinaryFile+" could not be found in folder "+pCurrentFolder.getName());
            return null;
        }

        FileInputStream fin = null;
        try {
            fin = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        byte fileContent[] = new byte[(int) file.length()];

        try {
            fin.read(fileContent);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return fileContent;
    }

I sent this (the byte array) via MyBatis to the database and it worked, so that I had the PDF in my BLOB and I also could read the PDF from my database. But now I face the following problem: I have a JDBC Connector for my search engine (FAST ESP...but that dowsnt matter) which connects to a certain database and stores all the content to a xml file. Inside this xml file is an element called "data" which contains the binary data inside its CDATA Field.

When I want to parse this xml, Java tells me:

The content of elements must consist of well-formed character data or markup.

With some PDF's i works but with some not. So I think the problem is, that I have stored them in the database in the wrong way.

For further information I would reverence to another questions I asked before which is similar to that.

Java: omita datos binarios en el archivo xml mientras analiza

Someone there told me that I should encode my PDF (or any binary file) with base64. So that would mean, I do not just put my PDF into a FileInputStream, store the byte[] and put this byte[] to my BLOB of the database. What do I have to do, to store the PDF in correct way inside my database, so that afterwards I can correctly parse my XML file the JDBC connector creates?

preguntado el 31 de julio de 12 a las 15:07

2 Respuestas

You can use the JAXB DatatypeConverter class to easily convert your data to base64 without any external dependencies:

byte[] arr = YOUR_BINARY_ARRAY;
String result = javax.xml.bind.DatatypeConverter.printBase64Binary(arr);

You can simply add this code to the end of your method and change its return type to a String.

Respondido 31 Jul 12, 16:07

nice..very helpful - Mahbubur Rahman Khan

You can try to first convert the bytes to basse64 using Apache Commons as this example:

import org.apache.commons.codec.binary.Base64;

import java.util.Arrays;

public class Base64Encode {
    public static void main(String[] args) {
        String hello = "Hello World";


        byte[] encoded = Base64.encodeBase64(hello.getBytes());


        System.out.println(Arrays.toString(encoded));

        String encodedString = new String(encoded);
        System.out.println(hello + " = " + encodedString);
    }
}

Respondido 31 Jul 12, 16:07

Intenté esto: byte[] base64String = Base64.encodeBase64URLSafe(fileContent); fileContent is my byte[] representing binary content of the PDF file which I stored into the BLOB first. Now I converted it to base64String (which is also a byte[]) and stored this inside my BLOB. This also worked but now I cant open the file inside my database. Is this correct? Ok I think I should have to decode it again but I cant do this, because inside the database there has to be the real file which then is sent to the search engine via the JDBC connector. So I cant use this directly. Is there another way? - Metalhead89

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.