Decoding excel file gone wrong - java

Hello I have a REST API in which I am uploading .xls files. Using the debugger I can see that the excel file is getting encoded correctly.
Code:
String[] tokensVal = request.getContent().split(delimiters);
String fileContentEncoded = tokensVal[tokensVal.length-1];
Result:
UEsDBBQABgAIAAAAIQAbOCctygEAAIsHAAATAM4BW0NvbnRlbnRfVHlwZXNdLnhtbCCiygEooAACAAAA...KxVy27bMBC8F+g/CLwWFp0eiiCwnEPSHNMAST6AIdcSYb7ApR3777uUIqM2FDWGfNF7Z3ZmH1rc7qwpthBRe1exq3LOCnDSK+3qir2+PMyuWYFJ...
When I am trying to decode the file with UTF-8 charset I can see that something is going wrong.
Code:
InputStream fileContent = new ByteArrayInputStream(new String(Base64.getDecoder().decode(fileContentEncoded), UTF_8).getBytes(UTF_8));
Result:
PK!8'-[Content_Types].xml (Un0? CI>!aﻔ"65|{gvfZ)Q{Wr
p+ꊽ<̮YI8%wP= ]~xVI)p+YhEX Z翸.K1rq+1]&oڱ.SUL`[NHf~K%Ba)C!%rWpjs)I(,w'$'݂(bTu=#A"<OZJ^Rd[5ltq=yqҮ)Ю{ 鞢ȩ֓<
Is anyone who faced this problem before?

Related

org.apache.poi.poifs.filesystem.NotOLE2FileException

I am creating an UI where I ask the user to upload an existing excel file and I am getting this error. I have done some searching, and tried to use a POIFileSystem object before passing in the FileInputStream but that didn't. I am getting this error of the line that creates the workbook.
This my code:
public static Cell readExcelFile(byte[] byteFile){
if (file == null){
System.out.println("file is empty");
} else {
InputStream input = new ByteArrayInputStream(byteFile);
POIFileSytem fsPOI = new POIFileSytem(input);
HSSFWorkbook wb = new HSSFWorkbook(fsPOI);
//continues to read the file
}
}
I found an answer! So I as you see from the code above I passed my file as a byte array. Prior to that in my POST request I upload the file as a string and decode it from base 64 and into an byte array, that were the error occured. It wasn't decode correctly. I printed out what the String version of the file from the POST endpoint and that consist of "data:application/vmd.ms-excel;base64,OMBR4K(this the file data encoded in base 64 but Im not going to type all of it). Basically it wasn't decoding correctly because of everything before the comma was a regular text header. I spilt the string and saved everything after the comma in another string, and converted that into a byte array and decoded. Then my file was acceptable and I was able the create the workbook, and I also had to use .getSheetAt(int) instead of .getSheet("string")

Not able to read tiff file after saving it from base64 string in java

I'm not able to open tiff image file from local machine after saving it from base64 string(from eclipse)
Below is the code for the same.
Byte[] decod = Base64.decodeBase64(base64encodedstring);
FileOutputStream str = new FileOutputStream(tifffilepathtosave);
BufferedOutputStream bstr = new BufferedOutputStream(str);
bstr.write(decod);
bstr.close();
When I tried to open the file
Alert saying this is not a valid bitmap file.or its format is not currently supported
But im able to open original tiff files.
Is there any other way to save tiff images properly, or am I missing anything in above code.
PF below link to access base64 string which I'm decoding and saving as a tiff file.
https://github.com/rajinikanthd08/ProjectA/blob/main/Tiffbase64%20(1).txt
This is the link of code which I'm getting response from web service and getting base64 encoded string and trying to saving in tiff.
https://github.com/rajinikanthd08/ProjectA/blob/main/SignatureDisplay.txt

Is possible to read a tif file like a txt, delete some header rows, and save it back to a tif file?

i'm trying to delete the first 3 rows of a tif file content generated by a scanner, because i cant open correctly.
example of rows to delete:
------=_Part_23XX49_-1XXXX3073.1XXXXX20715
ID: documento<br>
MimeType: image/tiff
I have no problem about change the content, but when i save the new file, i cant open correctly again.
System.out.println(new InputStreamReader(in).getEncoding());
this method tell me that the encoding of source file is "Cp1252", so i've put an argument in the JVM (-Dfile.encoding=Cp1252), but nothing appear to change.
This is what i do:
StringBuilder fileContent = new StringBuilder();
// working with content and save result content in fileContent variable
// save the file again
FileWriter fstreamWrite = new FileWriter(f.getAbsolutePath());
out = new BufferedWriter(fstreamWrite);
out.write(fileContent.toString());
Is possible that something is going wrong with Encoding?
if i do the operation with notepad++, i obtain a correct tiff that i can open without problem.
I found the TIFF Java library that maybe gonna be useful for your requirements.
Please take a look at the readme how to read and how to write a tiff file.
Hope this can help you

Android UTF-8 encoding not working?

I am working with a CSV file right now.
In my program i am using an OutputStreamWriter to write data the csv file.
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut, Charset.forName("UTF-8").newEncoder());
I tried printing out the encoding style of this writer and get the following:
Log.i(TAG, "BODY ENCODING: " + myOutWriter.getEncoding());
Logcat: BODY ENCODING: UTF-8
But when i try to open the csv file on my desktop it says that the file is in windows-1252 so i cant read æøå chars which i need.
Am i missing something obvious here or am i not understanding the concept of outputStreamWriter? I have tried different types of encoding, but it doesn't seem to work :)
When i try to open in Excel:
Your file is actually UTF-8 not CP-1252. Your text editor/viewer detected it as CP-1251 (since no multi-byte characters). You can help your editor by adding byte order mark (BOM) in the beginning of the file. I.e.
static final byte[] UTF8_BOM = {0xEF,0xBB,0xBF};
...
fOut.write(UTF8_BOM);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut, Charset.forName("UTF-8").newEncoder());
Did you try opening it in EXCEL? For EXCEL to recognize the file as UTF-8 it needs to have BOM (https://en.wikipedia.org/wiki/Byte_order_mark)

Decode Base64InputStream from String and unzip it with GZIPInputStream in Java

I am tryin to create simple app to decode the zipped data from string. In one textarea user will paste the zipped data after button click the data will be decoded and shown in another textarea.
If I use files, it works perfectly:
zis = new GZIPInputStream(new Base64InputStream(new FileInputStream(inZippedFile)));
where inZippedFile is file. Then result is saved to outputFile.
However, if I want to use string in InputStream it will never finish.
String input = "..."
InputStream in = IOUtils.toInputStream(input, "UTF-8");
zis = new GZIPInputStream(new Base64InputStream(in));
For IOUtils I am using common apache 2.4 jars. Anything what I am doing wrong?
Thanks
The decoding and unzip the string data is working correctly, there was just mistake in parsing the data to corect form. This was causing the long run.
So this is working, no need to set the UTF-8:
new GZIPInputStream(new Base64InputStream(IOUtils.toInputStream(input)));

Categories

Resources