InputStream xls file can't be decoded - java

I use an endpoint in order to upload a .xlsx or .xls file. After that I am storing the encoded excel file (base64) to a String. In order to parse and handle the values of excel I need to decode that file.
Upload service class:
public BulkUploadResponse validateFile(BulkUploadRequest request) {
final String pfx = String.format("validateFile: ");
BulkUploadResponse response = new BulkUploadResponse();
String delimiters = "\\s+|,";
String[] tokensVal = request.getContent().split(delimiters);
String fileContentEncoded = tokensVal[tokensVal.length-1];
InputStream fileContent = new ByteArrayInputStream(new String(Base64.getDecoder().decode(fileContentEncoded), UTF_8).getBytes(UTF_8));
Then I call a class where I convert the data included in excel file parsing as a parameter the fileContent which is supposed to be decoded.
BulkVignetteCustomer customerVignettes = bulkVignetteCustomerConverter.convert(fileContent);
Finally I use the Workbook package in order to parse that file
Example:
#Override
public BulkVignetteCustomer convert(InputStream fileContent) {
try {
Workbook wb = WorkbookFactory.create(fileContent);
FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
Sheet sheet = wb.getSheetAt(0);
Iterator rows = sheet.iterator();
(...)
Error that I get:
java.lang.IllegalArgumentException: Your InputStream was neither an OLE2 stream, nor an OOXML stream at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:89)
Any thoughts on how to decode the file without any errors?
Thank you!

This line seems very strange to me:
InputStream fileContent = new ByteArrayInputStream(new String(Base64.getDecoder().decode(fileContentEncoded), UTF_8).getBytes(UTF_8));
You have an Excel file which isn't plain text but a big binary blob at this stage. Why do you use a String here?
So the binary blob is encoded as Base64. You can directly get the bytes out of the decoder and put them into the ByteArrayInputStream:
InputStream fileContent = new ByteArrayInputStream(Base64.getDecoder().decode(fileContentEncoded));
To verify this part of your application you can store the bytes to a file and try to open it with Excel.

Related

HTML to PDF conversion using OpenhtmlTopdf with ByteArrayOutputStream

I am trying to convert HTML to PDF as encoded string. I am using openhtmltopdf library. I don't want to create a new file in users environment, so I am using ByteArrayOutputStream.
Following is my code:
Document document = Jsoup.parse(html, "UTF-8");
document.outputSettings().syntax(Document.OutputSettings.Syntax.xml);
document.outputSettings().prettyPrint(false);
// File outputpdf = new File("output.pdf");
try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
PdfRendererBuilder pdfRendererBuilder = new PdfRendererBuilder();
pdfRendererBuilder.toStream(os);
pdfRendererBuilder.withW3cDocument(new W3CDom().fromJsoup(document), "/");
pdfRendererBuilder.run();
// os.writeTo(new FileOutputStream(outputpdf));
byte[] encoded = java.util.Base64.getEncoder().encode(os.toString().getBytes());
String encodedString = new String(encoded);
I used an online base64 string to PDF decoder and generated PDF while testing. My PDF is coming as empty. When I replaced the ByteArrayOutputStream with FileOutputStream(<fileName>). It is creating a proper PDF file and also when I decode the string it is coming correct.
What am I missing in ByteArrayOutputStream?

java apache IOUtils breaks file content

I need to encode/decode pdf file into Base64 format.
So I read file from disk into String(because I will receive file in String Base64 format in future);
String pdfString = IOUtils.toString(new FileInputStream(new
File("D:\\vrpStamped.pdf")));
byte[] encoded = Base64.encodeBase64(pdfString.getBytes());
byte[] newPdfArray = Base64.decodeBase64(encoded);
FileOutputStream imageOutFile = new FileOutputStream(
"D:\\1.pdf");
imageOutFile.write(newPdfArray);
imageOutFile.close();
imageOutFile.flush();
So my D:\\1.pdf doesnt opens in AdobeReader, but if I read file straight to byte array, using IOUtils.toByteArray(..) instead ,all works fine and my D:\\1.pdf file sucessfuly opens in Adobe Reader:
byte[] encoded = Base64.encodeBase64(IOUtils.toByteArray(new FileInputStream(new File("D:\\vrpStamped.pdf"))););
It seems to me thath IOUtils.toString(..) change something inside file content. So how can I convert file to String with not content breaking?
How to encode a pdf...
byte[] bytes = IOUtils.toByteArray(new FileInputStream(new File("/home/fschaetz/test.pdf")));
byte[] encoded = Base64.encode(bytes);
String str = new String(encoded);
...now do something with this encoded String, for example, send it via a Rest service.
And now, if you receive an encoded String, you can decode and save it like this...
byte[] decoded = Base64.decode(str.getBytes());
FileOutputStream output = new FileOutputStream(new File("/home/fschaetz/result.pdf"));
output.write(decoded);
output.close();
Works perfectly fine with all files, not limited to images or pdfs.
What your example is doing is...
Read the pdf into a String (which pretty much destroys the data, since you are reading binary data into a String)
Encode this spring (which is in all likelyhood not a valid representation of the original pdf anymore)
Decode it and save it to disk

How to convert ByteBuffer to FileInputStream in Java?

I am trying to import xls sheet to DB, for that when i am uploading the file the uploaded file is in ByteBuffer Format, i have written a logic which reads as FileInputStream.
Now how do i convert this ByteBuffer to FileInputStream
Here is my Code
ByteBuffer fileBytes = (ByteBuffer) context.get("uploadedFile");
String encoding = System.getProperty("file.encoding");
String filename = Charset.forName(encoding).decode(fileBytes).toString();
filename = filename.replaceAll("\\r", "");
I tried Casting it using ByteArrayInputStream() but looks like it is not working!
Change your logic to use InputStream rather than FileInputStream. You don't care where the input comes from.
Use the following code:
ByteArrayInputStream bais = new ByteArrayInputStream(buffer.array(), buffer.position(), buffer.limit());
and pass bais to your existing method.

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+PnN0cmVhbQp4nJWbS5MdRxGF9/dX3KUNoXbXu9o7CQtCRg4LzUSMMWahkCXPmDsy2IpwwK8nH9WVJ1tjsGGBDpPn63pXVnXff53Wc9nKEvv5nv65ni+nnsPSN/znCLicbk83p3encP75FM+fU/j3p7Cevzj97e/r+dvTv8S/nn/87vTk+vTJH8M5pCXm8/VbcvAfwnnrgkt12dr5+v70aF1iaiv9+/Xpoy+efvbsD4+fn59+9eLl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BuIAowMDAwMDAwMDE1IDAwMDAwIG4gCjAwMDAwMDQzMDEgMDAwMDAgbiAKMDAwMDAwNDM5NCAwMDAwMCBuIAowMDAwMDA3ODUwIDAwMDAwIG4gCjAwMDAwMDQ3NjEgMDAwMDAgbiAKMDAwMDAwNzY2NyAwMDAwMCBuIAowMDAwMDA3NzYxIDAwMDAwIG4gCjAwMDAwMDgxOTggMDAwMDAgbiAKMDAwMDAwODI0NCAwMDAwMCBuIAp0cmFpbGVyCjw8L1Jvb3QgMTIgMCBSL0lEIFs8NGYyYjMyZmU2NjQ4MzBlZjYxODliMzJhMjNiY2E1MTg+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);

Export string with bytes to file without encoding

I stored bytes within a string in Java
String header ="00110011000000011001000000000001001011000000000100000010000000000000000000000000000000000000000000000000000000000000000000000000";
Now i want to write that String to a file, but export that as a series of bits and not encoded as a text.
Writing to the file looks like this:
BufferedWriter writer = new BufferedWriter (new FileWriter("test.epd"));
writer.write(header);
How can I do this(The string in this prog will be longer --> around 8kB)
I would use BinaryCodec from commons apache commons-codec.
String headerb = "00110011000000011001000000000001001011000000000100000010000000000000000000000000000000000000000000000000000000000000000000000000";
BinaryCodec codec = new BinaryCodec();
//I have no idea why this method is not static.
//you may use BinaryCodec.fromAscii(ascii.toCharArray()) instead
byte[] bval = codec.toByteArray(headerb);
File file = new File("test.epd");
Files.write(bval, file );
//Test that when the file is read, we retrieve the same string
byte[] byteArray = Files.toByteArray(file);
String asciiString = BinaryCodec.toAsciiString(byteArray);
System.out.println(asciiString);

Categories

Resources