Saving Base64 String to file - java

I am getting a JSON response from webservice call as
]1
I have to save this file to my system. I am using the following code:
BASE64Decoder decoder = new BASE64Decoder();
byte[] decodedBytes = decoder.decodeBuffer(response);
ByteArrayInputStream bis = new ByteArrayInputStream(decodedBytes);
bis.close();
File file = new File("C:/image one.jpeg");
FileOutputStream fop = new FileOutputStream(file);
fop.write(decodedBytes);
fop.flush();
fop.close();
The file gets saved but is corrupt and can't be opened. Can anybody shed some light on what I am doing wrong?

Try to use Base64 class from java.util.Base64
byte[] decodedBytes = Base64.getDecoder().decode(base64String);
Or Base64 from org.apache.commons.codec.binary
byte[] decodedBytes = Base64.decodeBase64(base64String);
And you won't need ByteArrayInputStream object
See this link for more detailed answer

Related

Java MimeBodyPart to byte[]

I am using Java 11. I am reading emails and retrieving their attachments using javax.mail and would like to save the attachments byte array.
So I need to convert a javax.mail.internet.MimeBodyPart (the attachment) to a byte[].
code so far:
private void savePDF(MimeBodyPart attachment, String invoiceNumber) {
byte[] data = attachment.get....
}
More info:
When I try open the PDF, I do the following:
InputStream is = response.getStream();
byte[] bytes = IOUtils.toByteArray(is);
OutputStream output = response.getOutputStream();
fos = new BufferedOutputStream(output);
fos.write(bytes);
This works perfectly when I get the bytes from a 'MultipartFile':
byte[] bytes = multipartFile.getBytes();
However, if I try use the byes from a MimeBodyPart (email attachment), it fails:
byte[] data = attachment.getInputStream().readAllBytes();
More info:
I have also tried, but I get the same error:
BASE64DecoderStream content = (BASE64DecoderStream) attachment.getContent();
byte[] bytes = content.readAllBytes();
bytes = Base64.decodeBase64(bytes);

BouncyCastle openPGP encrypt byte[] array as csv file

I have gotten my code to be able to load in a public key, and encrypt a byte[] array with a public key someone else has provided. However, when i decrypt the file, it doesn't have a .csv extension. I can open it in Excel and it looks like a csv once I open it, but the file just doesn't have any extension associated to it, so I have to weirdly open it in Excel.
I've also attached my code below and I just couldn't figure out where to modify it so that when I decrypt it using Kleopatra, it shows with a .csv extension. I tried to change my output stream to a .csv from a .gpg file, but then it throws an error saying that it could not determine whether this is a OpenPGP signature. I think I have to do something with the lData.open() method, but the only options I can put there are PGPLiteralData.TEXT, BINARY, and UTF8. Maybe this has to do with adding a signature to the file?
Btw, for the code below, the public key is already loaded into a variable called pubkey.
byte[] bytesArray = getAccessChannels(customerAlias);
OutputStream out = new FileOutputStream("/path/to/file/eData.gpg");
Security.addProvider(new BouncyCastleProvider());
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
PGPCompressedDataGenerator comData = new PGPCompressedDataGenerator(PGPCompressedDataGenerator.ZIP);
OutputStream cos = comData.open(bOut); // open it with the final destination
PGPLiteralDataGenerator lData = new PGPLiteralDataGenerator();
OutputStream pOut = lData.open(cos, PGPLiteralData.TEXT, "Report.csv", bytesArray.length, new Date());
pOut.write(bytesArray);
lData.close();
comData.close();
PGPEncryptedDataGenerator cPk = new PGPEncryptedDataGenerator(new BcPGPDataEncryptorBuilder(SymmetricKeyAlgorithmTags.AES_256).setSecureRandom(new SecureRandom()));
cPk.addMethod(new BcPublicKeyKeyEncryptionMethodGenerator(pubKey));
byte[] bytes = bOut.toByteArray();
OutputStream cOut = cPk.open(out, bytes.length);
cOut.write(bytes); // obtain the actual bytes from the compressed stream
cOut.close();
Nevermind, I just needed to change the file extension to eData.csv.gpg...this took me a whole 2 days

Convert string from xml response to pdf

I'm getting a response XML from web service and one of the field contains a string that should be convertable to pdf file.
This is example string that I extract from xml response:
JVBERi0xLjQKJeLjz9MKNSAwIG9iago8PC9UeXBlL1hPYmplY3QvUmVzb3VyY2VzPDwvUHJvY1NldFsvUERGL1RleHQvSW1hZ2VCL0ltYWdlQy9JbWFnZUldL0ZvbnQ8PC9GMSA2IDAgUi9GMiA3IDAgUj4+Pj4vU3VidHlwZS9Gb3JtL0JCb3hbMCAwIDg0MS44OSA1OTUuMjhdL01hdHJpeFsxIDAgMCAxIDAgMF0vTGVuZ3RoIDQwNTYvRm9ybVR5cGUgMS9GaWx0ZXIvRmxhdGVEZWNvZGU+PnN0cmVhbQp4nJWbS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SswMScwMCcpL0NyZWF0aW9uRGF0ZShEOjIwMTUwMzA5MTAzNzA5KzAxJzAwJyk+PgplbmRvYmoKeHJlZgowIDE0CjAwMDAwMDAwMDAgNjU1MzUgZiAKMDAwMDAwNDU5OSAwMDAwMCBuIAowMDAwMDA3OTY3IDAwMDAwIG4gCjAwMDAwMDgxMjkgMDAwMDAgbiAKMDAwMDAwNDQ4MiAwMDAwMCBuIAowMDAwMDAwMDE1IDAwMDAwIG4gCjAwMDAwMDQzMDEgMDAwMDAgbiAKMDAwMDAwNDM5NCAwMDAwMCBuIAowMDAwMDA3ODUwIDAwMDAwIG4gCjAwMDAwMDQ3NjEgMDAwMDAgbiAKMDAwMDAwNzY2NyAwMDAwMCBuIAowMDAwMDA3NzYxIDAwMDAwIG4gCjAwMDAwMDgxOTggMDAwMDAgbiAKMDAwMDAwODI0NCAwMDAwMCBuIAp0cmFpbGVyCjw8L1Jvb3QgMTIgMCBSL0lEIFs8NGYyYjMyZmU2NjQ4MzBlZjYxODliMzJhMjNiY2E1MTg+PDIwZGUyZmJkMjc1NzVhMzMyNmNkYTQ0OGNmYmE0OTMyPl0vSW5mbyAxMyAwIFIvU2l6ZSAxND4+CnN0YXJ0eHJlZgo4MzY3CiUlRU9GCg==
now I'm trying to convert it into proper pdf file like this:
byte[] bytes = THAT_LONG_STRING.getBytes();
BufferedOutputStream bos = null;
FileOutputStream fos = new FileOutputStream(new File("a.pdf");
bos = new BufferedOutputStream(fos);
bos.write(bytes);
bos.flush();
bos.close();
Sadly this does not work for me, output file is just file with that long string, pdf viewers cannot open it.
Any advices are welcome :)
Your string is in Base64 format. You should decode it to a byte array first, then write it to a file.
Just replace this line:
byte[] bytes = THAT_LONG_STRING.getBytes();
With this one (Apache Commons Codec required):
byte[] bytes = Base64.decodeBase64(THAT_LONG_STRING);

Issue when converting Base 64 encoded graphic image to PDF file

We are getting Base 64 encoded graphic image as webservice response and we have to convert it to PDF file. We used bellow code snippet to transfrom base 64 encoded graphic image to pdf doc.
// First decode the Base 64 encoded graphic image
BASE64Decoder decoder = new BASE64Decoder();
byte[] decodedBytes = decoder.decodeBuffer(s);
// Create the pdf file
File file = new File("output.png");
FileOutputStream fop = new FileOutputStream(file);
fop.write(decodedBytes);
fop.flush();
fop.close();
But when we open the pdf file we are getting bellow error.
Adobe Reader could not open "output.pdf" because it is either not a supported file type or because the file has been damaged.
We tried the PDF box as bellow,
BASE64Decoder decoder = new BASE64Decoder();
byte[] decodedBytes = decoder.decodeBuffer(s);
ImageToPDF imageToPdf = new ImageToPDF();
imageToPdf.createPDFFromImage("output.pdf", decodedBytes.toString());
This also didnt help us. Please suggest me a way to creat the pdf file from Base 64 encoded graphic image.
I thing you are missing a step here. Please try following
First, create the image from the Base64 data as follows (Taken from here)
String base64String =
"BORw0KGgoAAAANSUhEUgAAAUAAAAHgCAYAAADUjLREAAAgAElEQVR4AexdB4BU1dX+ZmZ7ZWGX3pHeu6goitgQDCZGjdHYu4nGqL81mmaJvdd";
BASE64Decoder decoder = new BASE64Decoder();
byte[] decodedBytes = decoder.decodeBuffer(base64String);
log.debug("Decoded upload data : " + decodedBytes.length);
String uploadFile = "/tmp/test.png";
log.debug("File save path : " + uploadFile);
BufferedImage image = ImageIO.read(new ByteArrayInputStream(decodedBytes));
if (image == null) {
log.error("Buffered Image is null");
}
File f = new File(uploadFile);
// write the image
ImageIO.write(image, "png", f);
Please note that this example uses sun.misc.BASE64Decoder, but I would advice not to use this but use some other open source decoder (e.g apache commend-codec library is good and widely used.).
Once you have the image file, use ImageToPDF to convert the same to PDF file.

Convert android.graphics.Path object to byte[] and back

I'm trying to convert an android.graphics.Path object to byte[] so that I could store it in a blob storage in SQLite, also to convert it back.
So far I don't even know where to begin...
Thanks to anyone willing to help.
As Path extends Object, you can use something like this:
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
objectOutputStream.writeObject(path);
byte[] array = outputStream.toByteArray();
Serialize your object and upload that file .
ByteArrayOutputStream baos = new ByteArrayOutputStream()
ObjectOutput out = new ObjectOutputStream(baos);
out.writeObject(android.graphics.Path);
out.close()
byte[] buf = bos.toByteArray(); //byte array
to recover that object use deserialization
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buf));
class_name recover =(clas_name) in.readObject();
in.close();
return object;

Categories

Resources