简体   繁体   中英

Base64 encode and decode not give same result

I have been banging my head against a wall on this all day. I have a PDF file that we generate. The PDF file looks fine in Acrobat.

I need to encode the file in base64. Using Apache codec library I do this:

String base64buf = Base64.encodeBase64String( m_reportText.getBytes( "UTF-8" ) );

As a test I write base64buf out to a file:

Files.write( new File( "report.b64" ).toPath(), base64buf.getBytes( "UTF-8") );

Then I convert it back, just to see if it is working:

String encodedName = "report.b64";
String decodedName = "report.pdf";

// Read original file.
byte[] encodedBuffer = Files.readAllBytes( new File( encodedName ).toPath() );

// Decode
byte[] decodedBuffer = Base64.decodeBase64( encodedBuffer );

// Write out decodedBuffer.
FileOutputStream outputStream = new FileOutputStream( decodedName );
outputStream.write( decodedBuffer );
outputStream.close();

I open report.pdf in Acrobat and it is a blank document. It has the correct number of pages (all are blank).

What am I missing here?

m_reportText is a String, hence contains Unicode text. However a PDF is in general binary data. That should really be avoided, as the superfluous conversion in both directions is lossy and error prone. For a hack you could try storing and retrieving the PDF bytes as ISO-8859-1.

Use a byte[] m_reportText .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM