borg48
asked on
How to encode a binary and text file into a string
I'm using the java commons base64 api. Here is what i need to do. My program creates a pdf and html file. I need to base64 encode each file into a string and then store it into a text field in a database. and at a later date retrieve it and decode it and store it back into the file type it should be. But i'm not quite to sure. Here is what i have so far:
File file = new File( "D:/projects/base64code/te st.pdf" );
File fileOut = new File( "D:/projects/base64code/te st_out.pdf " );
byte[] scratch = new byte[8*1024];
FileInputStream in = new FileInputStream(file);
FileOutputStream out = new FileOutputStream(fileOut);
// These next two give you super turbo performance speed
BufferedInputStream bin = new BufferedInputStream(in);
BufferedOutputStream bout = new BufferedOutputStream(out);
// If you don't loop, then you're probably losing data
int consumed = 0;
do {
consumed = bin.read( scratch );
if ( consumed > 0 )
{
bout.write( scratch, 0, consumed );
}
} while ( consumed >= 0 );
byte[] encodedArray = Base64.encodeBase64( scratch );
byte[] decodeArray = new byte[8*1024];
decodeArray = Base64.decodeBase64(encode dArray);
So what i'm trying to do in this example is:
1. Encode the pdf file.
2. Retrieve the string value so i can store it into a column in a database table ( I'm not sure how to do that here)
3. Take the string value (just to test that i can convert back to a byte and decode) and convert into a byte then save to the appropriate file name.
Thanks in advance.
File file = new File( "D:/projects/base64code/te
File fileOut = new File( "D:/projects/base64code/te
byte[] scratch = new byte[8*1024];
FileInputStream in = new FileInputStream(file);
FileOutputStream out = new FileOutputStream(fileOut);
// These next two give you super turbo performance speed
BufferedInputStream bin = new BufferedInputStream(in);
BufferedOutputStream bout = new BufferedOutputStream(out);
// If you don't loop, then you're probably losing data
int consumed = 0;
do {
consumed = bin.read( scratch );
if ( consumed > 0 )
{
bout.write( scratch, 0, consumed );
}
} while ( consumed >= 0 );
byte[] encodedArray = Base64.encodeBase64( scratch );
byte[] decodeArray = new byte[8*1024];
decodeArray = Base64.decodeBase64(encode
So what i'm trying to do in this example is:
1. Encode the pdf file.
2. Retrieve the string value so i can store it into a column in a database table ( I'm not sure how to do that here)
3. Take the string value (just to test that i can convert back to a byte and decode) and convert into a byte then save to the appropriate file name.
Thanks in advance.
You might also want to take a look here: http://javaalmanac.com/egs/java.net/Base64.html
If the only reason for encoding a binary file is that you have to store it into a database, you can use BLOB field
ASKER
How would i then take the byte:
bytes [] decodedPDF = BASE64Decoder().decodeBuff er(endoded String);
and output it back to a file?
bytes [] decodedPDF = BASE64Decoder().decodeBuff
and output it back to a file?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
String encodedString = BASE64Encoder().encode(pdf
To decode it back to a bytes array you use:
bytes [] decodedPDF = BASE64Decoder().decodeBuff