How to construct the response body for InputStream which has an Excel sheet file?

Mohammed Shirhaan :

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.

Mohammed Shirhaan :

This answer fixed the problem

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.

edited at
0

Comments

0 comments
Login to comment

Related

How to receive the response body as an InputStream in Unirest?

how to read an excel file which has no path

How to Construct a POST Request Which Expects No Body

How to hide columns which has all values less than a number in excel sheet?

How to get the rows which has a value greater than a number across all columns in excel sheet?

How to download the excel file which has no link in Beautifulsoup?

How to close InputStream which fed into Response(jax.rs)

Excel - How to find a number exist in which sheet

How add empty sheet to `excel` file that has already created using pandas

How to use C# code to create an Excel sheet that contains a pivot table which has direct connection to my database using a query?

read excel file which has = in a column in r

sheet.nrows has a wrong value - python excel file

exported excel sheet has no .xls file extension in Mozilla

How to turn response to Excel file

how to import a sheet of an excel file into the access database?

How to write the contents of the text file into Excel sheet

How to check if the response Body has a string for a nested JSON in POSTMAN

How to write an excel file which has dictionary keys as column name and dictionary values as column values?

How to identify and write an excel file with .xls extension which has .xlsx content

How to link two excel sheet in same excel file

How to construct Dataframe from a Excel (xls,xlsx) file in Scala Spark?

How to copy only formulas from one excel sheet which can dynamically grow to another sheet using macro

How to assign File to InputStream?

Retrofit2: How to get binary file inside response body

Gatling: How to display full HTTP response body in the console or print it into a file

How to HTTP PUT a file in Powershell and capture response body?

How to get File object from response body in OkHttp?

Paste table from Email body to an Excel sheet

How to construct two plots one of which has fewer y values than the other in R?