10

Possible Duplicate:
Decode Base64 data in Java

I have a method that takes in a String, which is a Base64 encoding of a PDF document. How can I decode the binary back into a PDF file and then save the PDF file (or just pass back the file as a File object so the next program can use it).

I need to do this using Java.

Community
  • 1
  • 1
user1769045
  • 141
  • 1
  • 3
  • 12

1 Answers1

21

Try this code:

//Required imports
import java.io.*;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

// Code
BASE64Decoder decoder = new BASE64Decoder();
byte[] decodedBytes = decoder.decodeBuffer(encodedBytes);

File file = new File("c:/newfile.pdf");;
FileOutputStream fop = new FileOutputStream(file);

fop.write(decodedBytes);
fop.flush();
fop.close();
Wyetro
  • 8,439
  • 9
  • 46
  • 64
Frank Rem
  • 3,632
  • 2
  • 25
  • 37