I am writing a get API for getting excel sheet file stored in src/main/resources.
The InputStream has the content of excel file (xlsm format). But the response is having corrupted data.
@GetMapping("/downloadTemplate")
public ResponseEntity<?> downloadTemplate() throws IOException {
String fn = "filename";
try {
File file = new File(getClass().getClassLoader().getResource(fn).getFile());
InputStreamSource iss = new FileSystemResource(file);
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE)
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename= SimpleStandardTemplate.xlsm")
.body(IOUtils.toByteArray(iss.getInputStream()));
} catch (Exception e) {
log.error("An error occurred while trying to downloadTemplate: {}", e);
throw new ApiException(ApiErrorCode.DEFAULT_410, "Error while downloading");
}
}
Seems like this line has a problem "body(IOUtils.toByteArray(iss.getInputStream()));" . If yet any alternate way? Else what could be the issue?
Edit: I think the issue is with content length.
Added this plugin to the pom.
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<configuration>
<encoding>UTF-8</encoding>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>xls</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments