Convert PDF to byte array and viceversa android - java

I'm trying to create an Android app able to upload and download PDF files to a server, I've already included in my code a method able to download a PDF from an URL such as "http://www.aaaa.com/myfile.pdf" and display it by using intent.
But I want to know if there is a way to encode a Pdf into a byte array and viceversa decode a byte array to PDF file (I'm using mongoDB)
Thanks to everyone :)

import java.nio.file.*;
Path pdfFilePath = Paths.get("/file/path/your_file.pdf");
// Read file to byte array
byte[] pdfByteArray = Files.readAllBytes(pdfFilePath );
// Write byte array to file
Files.write(pdfFilePath , pdfByteArray);

Related

How download a String with p:fileDownload?

String output = "qwerty123";
Then how to export that string to
StreamedContent in PrimeFaces ?
you can see the example here : https://www.primefaces.org/showcase/ui/file/download.xhtml?jfwid=e2fc5
In that link, the StreamedContent exported from .jpg collected from resources in Web Pages Directory.
But in my case, I just want to convert the string above output to .txt then stream it in StreamedContent without Files.write();.
Thanks in advance.
You can use a ByteArrayInputStream to stream the contents of your string:
DefaultStreamedContent.builder()
.name("your.txt")
.contentType("text/plain")
.stream(() -> new ByteArrayInputStream("qwerty123".getBytes(StandardCharsets.UTF_8)))
.build();
See also:
How to convert Java String into byte[]?
Can we convert a byte array into an InputStream in Java?

Getting a .png image by Apache HttpClient on Android

I'm trying to get a .png file to render in a WebView after getting the file through an apache HttpClient get request. I'm actually reading the HttpResponse contents into a simple byte array -> (evaluating the contents into a String when debugging).
It appears if I convert those bytes into a string to compare with the actual file, they look different. Is there a specific way to retrieve .png file
using the apache HttpClient?
example compare, my test is on the left and actual file on right

Converting base64 encoded pdf to file input stream without writing file to system

I am getting a base64 PDF input as part of a request from my client, I need to convert this file into to File Input Stream to use with the PDF BOX library. I am trying to achieve this without writing the file onto drive and directly reading the base64 pdf into File Input Stream.
What I am able to do:
Convert base64 encoded pdf to File and write on the drive
Read the file into a File Input Stream
What I want to do:
Convert base64 encoded PDF to File input stream to use it with PDFBOX.
I am trying to avoid writing the file onto the disk.
In pdfbox you can load a pdf from an InputStream. it does not need to be FileInputStream.
Using the following code you can get an inputStream of the decoded pdf.
Note: depending on the scheme used to encode pdf you may need to use: .getMimeDecoder() instead of .getDecoder().
InputStream decoded = java.util.Base64.getDecoder().wrap(inputStream);
PDDocument.load(decoded);

write byte array to csv file with java

I have to upload a file attachment and send it via an HTTP GET request to a server location. I am using IBM websphere integration designer to implement this functionality.
I have the attachment as a byte array and it has to be converted to an Excel .csv file.
Please help me understand how to write a .csv file using a byte array.
If you have a byte array. Let's say.
byte[] byteArray = ThisIsWhereMyByteArrayIsComingFrom.getArray();
String convertedString = new String(byteArray);
That gets the string value of whatever your byte array is. Then you can either, use a csv library to output whatever it is to a file.

Convert inputstream to pdf to byte array

i created a mule flow which accepts a file as inputstream and convert into byte array and attach that file to the particular SalesForce case.
Can it possible that i could convert the file(coming as inputstream) to the pdf format and attach to Salesforce case.
PDF is a special format so directly it will not create PDF. You can use libraries like Apache FOP or iText to create PDF output.

Categories