I have different types of files stored and I have a controller to download the files. The files are stored in base64 and I'm trying to download the file and the file seems to be corrupted althought the download works. I could really use some pointers to fix my problem.
Here is what I have so far.
#GetMapping("/attachments")
public ResponseEntity<InputStreamResource> getAttachment() {
String file = "iVBORw0KGgoAAAANSUhEUgAAALoAAAAyCAYAAADr7cFEAAAgAElEQVR4nO2df3idVZXvP99MzGTOye1Ne1NuH26pEJgKiLXlxJogKhRFYBwFfyDiNMjAiUKioMg4yFAeisAwWLxqU0qODJAq6r0KyI+K6FRBxxbmHIhMp3QQIpdCp1Ny29w+nPN0Mpl87x/vz3NykqYV7p3Rrj7pec/77nfttddee+211l57HzgIB+F3APRaIj+is3epxBXAO4BGoIh90/CmNQ+9lvUehINQC6+ZoLd39p4NvhPUXFWTmUB8HvjS8Mb+qncGh17+c1vrz1vS9tRrRddB+N2EhtcCaXtX7zzjWwzNJvzn8A832L4BWDT5Te2S2Ds4NNL4WtB1EH534TURdOwzJc0RIAkJhMJrIanR+Pza17oXtw0AHcDfDw6NtAAMPvny0sGhkdLgky/Pry0/ODTy2tB/EH7r4IA0Z3tnbwPiUPChoJ3gF4c3rhmPnhveIKg2jGqMJKGjpkC/1fZ9kvaG3/dgtiL2pgsNPjlyInD74NDI7cD13YvbDqQpVZAtlOYAzQaEAY0DO8v53P7gaAQOAQIMyeeucj63N1sotRjPCoZ+8Iygvu3lfMf+1JMxtCYWYZrF8bdxoGLzSqVn320IaGMWEd0GBUi37w8P/j3CfmvEIzovPsr4QeA54DGbXxn9rL2rtzMqI+llCNhtAoY59R3A9q56+I23Is0zfnBwaOTQ7iVztyKfj3X24NDIGYNDI2ElLDU+Cjh9f9tQDzKFYhP23wEvyX4J8xL4acP+jqBjgZdiPPASwecZALY/LRTXIfsl4ZdA+6d0zLmyX3JYR3xtvwSKrreBf41cygyUvpItFE8IB2J9lOZSwUtyRHPwZ/wf3pTcL0E/oqt3gdBPhE4DmmwQNAo6gR+lhH09MC6DgjLxJw5EXXBPvTqEOmV3yzoVODe8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rZD21ffkvhEtlCKfLE5BLt7fhxo/ABq8g1fQGrPFkoLCX4qcwHwsahkCLsUnLTcni2UNgNN5XxuzCmNXs7nyBaK3yI4e/528FZQq83lMg871Oz1TktIUq4DyBRKnxXcgb0L6UWkvxfBceOvqqB3L5lbe3YhAMOb+ifaO3uvQjxIwIx2rJ0S9yA9cND2fs1hHuIezPOZgeJ2ggjZM5IGAQRfsv0Wib+zeRaYj9iuWHDBeCId95a5z/LlmBKwA9SK2GSCY+HCl4oWmwWP2H6e8CxFoYmqxGXz34XfgihhthofLmk78LFKnKBlcHXcXWLC9kSyydrnA1cbnsJuRBwt85lyT8fYlJbPQfjtgmyheChWR7hL61mgmM7yC53C4wULsXdY+nkllcqcLZQONW6s5DvizQzZgVLG8jKhJuBRzDhinvEz0WnImYFShqBMMzBUzueezRSKbUKt5Xzu2XT9wPFAtKf45+lU6kyheKigsZyuv1BqA7dCkNAVbrQ5HtFOkP69KTr65P8CEtHnF7Z82nMAAAAASUVORK5CYIIA";
String decoded = new String(Base64.decodeBase64(file.getBytes()));
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Encoding", "UTF-8");
headers.add(HttpHeaders.CONTENT_DISPOSITION,"attachment; filename=" + fileName);
InputStream targetStream = IOUtils.toInputStream(decoded, "UTF-8");
return ResponseEntity.ok().headers(headers).contentLength(decoded.length())
.contentType(MediaType.parseMediaType("application/octet-stream")).body(new InputStreamResource(targetStream));
}
The base 64 encoded file is a PNG file. PNG files are binary files and cannot be represented as string and are not UTF-8 encoded. Just return the byte array.
#GetMapping("/attachments")
public ResponseEntity<byte[]> getAttachment() {
String file = "iVBORw0KG ... 5CYIIA";
byte[] decoded = Base64.getDecoder().decode(file);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
ContentDisposition contentDisposition = ContentDisposition.builder("attachment")
.filename(fileName).build();
headers.setContentDisposition(contentDisposition);
return ResponseEntity.ok().headers(headers)
.contentLength(decoded.length)
.body(decoded);
}
Related
I want to serve an image to a client by converting it to a byte but for some reason byteArrayOutputStream.toByteArray() is empty. I get a response status of 200 which means it is served. I looked at various documentations on reading an image file from a directory using BufferedImage and then converting BufferedImage to a byteArray from oracle https://docs.oracle.com/javase/tutorial/2d/images/loadimage.html and https://docs.oracle.com/javase/tutorial/2d/images/saveimage.html but for some reason byteArray is still empty
This controller
#GetMapping(path = "/get/image/{name}")
public ResponseEntity<byte[]> displayImage(String name) throws IOException {
String photoPathFromDatabase = productRepository.findPhotoByName(name);
Path path = Paths.get(photoPathFromDatabase);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
BufferedImage image = ImageIO.read(path.toFile()); // Reading the image from path or file
String fileType = Files.probeContentType(path.toFile().toPath()); // Getting the file type
ImageIO.write(image, fileType, byteArrayOutputStream); // convert from BufferedImage to byte array
byte[] bytes = byteArrayOutputStream.toByteArray();
return ResponseEntity
.ok()
.contentType(MediaType.valueOf(fileType))
.body(bytes);
}
After I debugged the method
You should read the bytes of the file directly, rather than use an excessive amount of methods from different classes. This can be done with the class java.nio.file.Files.
byte[] contentBytes = Files.readAllBytes(path); //Throws IOException
Probably the file extension is not getting set properly.
You can create a new method to get the file extension or you can use FilenameUtils.getExtension from Apache Commons IO.
public static Optional<String> getExtensionByStringHandling(String filename) {
return Optional.ofNullable(filename)
.filter(f -> f.contains("."))
.map(f -> f.substring(filename.lastIndexOf(".") + 1));
}
then change your ImageIO to take this file extention.
String fileExtention= getExtensionByStringHandling(file.getName()).orElseThrow(()->new RuntimeException("File extension not found"));
ImageIO.write(image, fileExtention, byteArrayOutputStream);
Giving the following implementation I face the problem that, on another system, the XML file is missing the Umlaute (ä, ü, ö) compared to the origin XML file. Instead of the Umlaute the replacement character is inserted in the XML file. (0xEF 0xBF 0xBD (efbfbd))
Get a zip file containing a XML with Umlauts
Decompress the zip file
Encode the xml content to a Base64 payload and save it to the db
Querys the entity
Get the Base64 payload
Decode the Base64 content
Decoded Base64 content is a XML which should contain the origin Umlauts
Whats driving me crazy is the fact that the decoded Base64 content is missing the Umlaute on another system. Instead of the umlaute I get the replacement character. On my system the same implementation is working without the replacement.
The following code is just a MCVE to explain the problem which works fine on my system but on a other system (Windows Server 2013) misses the umlaute after decode.
String requestUrl = "https://myserver/mypath/Message_166741.zip";
HttpGet httpget = new HttpGet(String requestUrl = "https://myserver/mypath/Message_166741.zip";);
HttpResponse response = httpClient.execute(httpget);
HttpEntity entity = response.getEntity();
InputStream inputStream = entity.getContent();
byte[] decompressedInputStream = decompress(inputStream);
String content = null;
content = new String(decompressedInputStream, StandardCharsets.UTF_8);
String originFileName = new SimpleDateFormat("yyyyMMddHHmm'_origin.xml'").format(new Date());
String originFileNameWithPath = String.format("C:\\temp\\Tests\\%1$s", originFileName);
// File contains the expected umlauts
FileUtils.writeStringToFile(new File(originFileNameWithPath), content);
String payloadUTF8 = Base64.encodeBase64String(ZipUtils.compress(content.getBytes("UTF-8")));
String payload = Base64.encodeBase64String(ZipUtils.compress(content.getBytes()));
String payloadJavaBase64 = new String(java.util.Base64.getEncoder().encode(ZipUtils.compress(content.getBytes())));
String xmlMessageJavaBase64;
byte[] compressedBinaryJavaBase64 = java.util.Base64.getDecoder().decode(payloadJavaBase64);
byte[] decompressedBinaryJavaBase64= ZipUtils.decompress(compressedBinaryJavaBase64);
xmlMessageJavaBase64 = new String(decompressedBinaryJavaBase64, "UTF-8");
String xmlMessageUTF8;
byte[] compressedBinaryUTF8 = java.util.Base64.getDecoder().decode(payloadUTF8);
byte[] decompressedBinaryUTF8 = ZipUtils.decompress(compressedBinaryUTF8);
xmlMessageUTF8 = new String(decompressedBinaryUTF8, "UTF-8");
String xmlMessage;
byte[] compressedBinary = java.util.Base64.getDecoder().decode(payload);
byte[] decompressedBinary = ZipUtils.decompress(compressedBinary);
xmlMessage = new String(decompressedBinary, "UTF-8");
String processedFileName = new SimpleDateFormat("yyyyMMddHHmm'_processed.xml'").format(new Date());
String processedFileNameUTF8 = new SimpleDateFormat("yyyyMMddHHmm'_processedUTF8.xml'").format(new Date());
String processedFileNameJavaBase64 = new SimpleDateFormat("yyyyMMddHHmm'_processedJavaBase64.xml'").format(new Date());
// These files do not contain the umlauts anymore.
// Instead of the umlauts a replacement character is inserted (0xEF 0xBF 0xBD (efbfbd))
String processedFileNameWithPath = String.format("C:\\temp\\Tests\\%1$s", processedFileName);
String processedFileNameWithPathUTF8 = String.format("C:\\temp\\Tests\\%1$s", processedFileNameUTF8);
String processedFileNameWithPathJavaBase64 = String.format("C:\\temp\\Tests\\%1$s", processedFileNameJavaBase64);
FileUtils.writeStringToFile(new File(processedFileNameWithPath), xmlMessage);
FileUtils.writeStringToFile(new File(processedFileNameWithPathUTF8), xmlMessageUTF8);
FileUtils.writeStringToFile(new File(processedFileNameWithPathJavaBase64), xmlMessageJavaBase64);
The three files are just for testing purpose but I hope you getting the problem
Edit
Both ways create XML file with ü, ö, ä on my machine
Only the WITHOUT implementation create an XML XML file with ü, ö, ä on another system The "content" string of WITH UTF-8 contains for ü =>
// WITHOUT UTF-8 IN BYTE[] => STRING CTOR
byte[] dci = decompress(inputStream);
content = new String(dci);
byte[] compressedBinary = java.util.Base64.getDecoder().decode(content);
byte[] decompressedBinary = ZipUtils.decompress(compressedBinary);
String xml = new String(decompressedBinary);
// WITH UTF-8 IN BYTE[] => STRING CTOR
byte[] dci = decompress(inputStream);
content = String(dci, StandardCharsets.UTF_8);;
byte[] compressedBinary = java.util.Base64.getDecoder().decode(content);
byte[] decompressedBinary = ZipUtils.decompress(compressedBinary);
String xml = new String(decompressedBinary, "UTF-8");
Edit #2
There also seems to be a difference between running the code in IntelliJ and outside of IntelliJ on my machine. Did not know that this makes such a huge difference. So - if I run the code outside of IntelliJ (java.exe -jar myjarfile) the WITH UTF8 Part replaces the Ü. with ... I don't know. Notepad++ shows xFC. Funny: My raspberry pi shows both files with Ü where my Windows / notepad++ shows xFC.
That whole thing confuses me and I would like to understand whats the problem is. Also because the XML file contains the UTF8 as encode in header.
Edit #3 Final Solution
// ## SERVER
// Get ZIP from request URL
HttpGet httpget = new HttpGet(requestUrl);
HttpResponse response = httpClient.execute(httpget);
HttpEntity entity = response.getEntity();
InputStream inputStream = entity.getContent();
byte[] decompressedInputStream = decompress(inputStream);
// Produces a XML string which SHOULD contain ü, ö, ä
String xmlOfZipFileContent = new String(decompressedInputStream, StandardCharsets.UTF_8);
// Just for testing write to file
String xmlOfZipFileSavePath = String.format("C:\\temp\\Tests\\%1$s", new SimpleDateFormat("yyyyMMddHHmm'_original.xml'").format(new Date()));
FileUtils.writeStringToFile(new File(xmlOfZipFileSavePath), xmlOfZipFileContent, StandardCharsets.UTF_8);
// The payloadExplicitUtf8 gets stored into the DB
String payload = java.util.Base64.getEncoder().encodeToString(ZipUtils.compress(xmlOfZipFileContent.getBytes(StandardCharsets.UTF_8)));
// Store payload to db
// Client queries database and gets the payload
// payload = dbEntity.get().payload
// The following three lines is on client
byte[] compressedBinaryPayload = java.util.Base64.getDecoder().decode(payload);
byte[] decompressedBinaryPayload = ZipUtils.decompress(compressedBinaryPayload);
String xmlMessageOutOfPayload = new String(decompressedBinaryPayload, StandardCharsets.UTF_8);
String xmlOfPayloadSavePath = String.format("C:\\temp\\Tests\\%1$s", new SimpleDateFormat("yyyyMMddHHmm'_payload.xml'").format(new Date()));
FileUtils.writeStringToFile(new File(xmlOfPayloadSavePath), xmlMessageOutOfPayload, StandardCharsets.UTF_8);
If I understood correctly, your situation seems to be the following:
// Decompress data from the server, it's in ISO-8859-1 or similar 1 byte encoding
byte[] dci = decompress(inputStream);
// Data gets corrupted because of wrong charset
// This is where ü gets converted to unicode replacement character
content = new String(dci, StandardCharsets.UTF_8);
The rest of the code uses UTF8 explicitly, but it doesn't matter as the data has already been corrupted at this point. In the end you expect an UTF-8 encoded file.
Also because the XML file contains the UTF8 as encode in header.
That doesn't prove anything. If you treat it as just a text file, you can write it out in as many encodings as you want to, and it would still claim to be UTF8.
InputStream inputStream = entity.getContent();
byte[] decompressedInputStream = decompress(inputStream);
Fine, and it is assumed that the bytes are in UTF-8, as:
String content = new String(decompressedInputStream, StandardCharsets.UTF_8);
Should the bytes not be in UTF-8, you could try Windows Latin-1:
Charset.forName("Windows-1252")
Otherwise decompressedInputStream can be used whereever content is converted to bytes in UTF-8.
...
The FileUtils.writeStringToFile without encoding specified uses the default platform encoding.
// File contains the expected umlauts
//FileUtils.writeStringToFile(new File(originFileNameWithPath), content);
Better is to ensure that UTF-8 is written. Either add the encoding to convert the Unicode String to bytes in UTF-8, or simply write the original bytes:
Files.write(Paths.get(originFileNameWithPath), decompressedInputStream);
Also the Base64 encoded UTF-8 bytes of the String should be used:
String payloadUTF8 = Base64.encodeBase64String(ZipUtils.compress(
content.getBytes(StandardCharsets.UTF_8)));
String payloadJavaBase64 = new String(java.util.Base64.getEncoder().encode(
ipUtils.compress(content.getBytes(StandardCharsets.UTF_8))));
The standard JavaSE Base64 will do; though do not use its decodeString and encodeString as that uses ISO-8859-1, Latin-1.
How to write java code for egnyte chunked upload and send to rest service of egnyte api.
https://developers.egnyte.com/docs/read/File_System_Management_API_Documentation#Chunked-Upload
long size = f.getTotalSpace();
int sizeOfFiles = 1024 * 1024;// 1MB
byte[] buffer = new byte[sizeOfFiles];
ResponseEntity<String> responseEntity = null;
String fileName = f.getName();
String url = DOWNLOAD_OR_UPLOAD + "-chunked" + egnyteSourcePath + f.getName();
HttpHeaders headers = buildEgnyteEntity();
HttpEntity entity = new HttpEntity<>(headers);
//try-with-resources to ensure closing stream
try (FileInputStream fis = new FileInputStream(f);
BufferedInputStream bis = new BufferedInputStream(fis)) {
int bytesAmount = 0;
while ((bytesAmount = bis.read(buffer)) > 0) {
//write each chunk of data into separate file with different number in name
String filePartName = String.format("%s.%03d", fileName, partCounter++);
File newFile = new File(f.getParent(), filePartName);
responseEntity = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
}
}
return responseEntity;
I think there's a couple of things missing in your code.
First thing is that you don't specify required headers. You should provide X-Egnyte-Chunk-Num with int value with number of your chunk, starting from 1. In X-Egnyte-Chunk-Sha512-Checksum header you should provide SHA512 checksum.
Second thing is that first request will give you an UploadId in response header in X-Egnyte-Upload-Id. You need to specify that as a header in your second and following requests.
Third thing is that I don't see you use your bytesAmount in the request. I'm not sure you're providing the data.
I'm not a Java guy, more of a C# one, but I've written a post how to upload and download big files with Egnyte API on my blog: http://www.michalbialecki.com/2018/02/11/sending-receiving-big-files-using-egnyte-api-nuget-package/. This can give you an idea how sending loop can be structured.
Hope that helps.
I want to download images from website using RestAssured. My code:
Response response = given()
.log().all()
.cookie(cookie)
.get(format(image_endpoint, "46581657"));
Endpoint returns status code 200 and image in "application/octet-stream" type. How can I save it as jpg file?
I have tried:
String bin = getImageResponse()
.prettyPrint();
saveFile("foto.jpg", bin); //this method only save string to file
But I cannot open jpg file. How can I save it?
MIME type application/octet-stream consists of bytes encoded in Base64. Use Base64 class for decoding.
private void saveFile(String path, String octetStream) throws IOException{
byte[] bytes = Base64.getDecoder().decode(octetStream);
try(FileOutputStream fos = new FileOutputStream(path)){
fos.write(bytes);
}
}
I know nothing about RestAssured, so maybe there is any way provided by framework.
I cannot find out why the mp3 file is different after download from my server than original one saved previously there.
This is my controller method. The content of file (byte[] content) is identical with original file on this stage - the original file is the same as file retrieved from database (checked in debugger).
#ResponseBody
#RequestMapping(method = RequestMethod.GET, value = "/{sampleId}/file")
public HttpEntity<byte[]> getFile(#PathVariable Long sampleId) {
ResourceEntity resourceEntity = testSampleRepository.getFile(sampleId);
byte[] content = resourceEntity.getContent();
String fileName = resourceEntity.getFileName();
HttpHeaders header = new HttpHeaders();
header.setContentType(new MediaType("audio", "mpeg"));
header.set(HttpHeaders.CONTENT_DISPOSITION,
"attachment; filename=" + fileName.replace(" ", "_"));
header.setContentLength(content.length);
return new HttpEntity<byte[]>(content, header);
}
This is how files differ (the left is original one):
Why passing using HTTP distors my file? Should mediaTypes enforce certain encoding? (there was no difference with "audio/mpeg" mediaType and without it).
It should work, if you set the produces = "application/octet-stream" attribute (MediaType.APPLICATION_OCTET_STREAM). Otherwise, you are trapped by Spring's converter framework.
You may want to have a look here, seems your problem is very similar: Spring MVC: How to return image in #ResponseBody? .