Excel file download using java - Error File is corrupted

0 votes
I want to download a .xls file from the src/main/resources/DAO folder in my spring boot project.

file name: DAOtemplate.xls

Can someone please help me with this?
Dec 28, 2022 in Others by Kithuzzz
• 38,020 points
2,229 views

1 answer to this question.

0 votes

Try this:

 @GetMapping("/downloadDOA")
    public ResponseEntity<Object>  downloadTemplate(HttpServletRequest request, HttpServletResponse response) throws IOException{
   
        
        String filename = "C://Users//Sai//git//r5//src//main//resources//DOA//DOAtemplate.xls";
        File file = new File(filename);
        
        InputStreamResource iresource = new InputStreamResource(new FileInputStream(file));
        
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Disposition","attachment;filename=" +file.getName());
        headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
        headers.add("Pragma", "no-cache");
        headers.add("Expires", "0");

        ResponseEntity<Object> responseEntity = ResponseEntity.ok().headers(headers)
                .contentLength(file.length())
                .contentType(MediaType.parseMediaType("application/vnd.ms-excel")).body(iresource);

        return responseEntity;
        
    
        
    }
answered Dec 28, 2022 by narikkadan
• 63,720 points

Related Questions In Others

0 votes
1 answer

Excel VBA - Using Error Traps where Another Routine is Called

Macro1 is illegible as syntax. After the ...READ MORE

answered Jan 12, 2023 in Others by narikkadan
• 63,720 points
347 views
0 votes
1 answer

ERROR:'keytool' is not recognized as an internal or external command, operable program or batch file

Check if the directory and the keytool ...READ MORE

answered Feb 18, 2022 in Others by Rahul
• 9,680 points
8,652 views
0 votes
1 answer

Removing specific rows in an Excel file using Azure Data Factory

Under the 'Source' tab, choose the number ...READ MORE

answered Sep 23, 2022 in Others by narikkadan
• 63,720 points
2,501 views
0 votes
1 answer

Writing large Excel in Java causing high CPU usage using apache-poi

Giving SXSSFWorkbook a window size appears to ...READ MORE

answered Jan 29, 2023 in Others by narikkadan
• 63,720 points
1,559 views
0 votes
1 answer

Hosting spring boot app

There are a couple of things wrong ...READ MORE

answered Sep 11, 2018 in AWS by Priyaj
• 58,100 points
2,419 views
0 votes
1 answer

Validate a jwt token from cognito

Here is a documentation on how to ...READ MORE

answered Oct 31, 2018 in AWS by Priyaj
• 58,100 points
3,930 views
0 votes
1 answer
0 votes
1 answer

Excel formula gives error when write using Apache-poi library in Java

I tested some sample code. The cell ...READ MORE

answered Nov 5, 2022 in Others by narikkadan
• 63,720 points
1,866 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP