1

I want the excel file to download when user calls this method. File is downloading successfully but this is creating another excel file in the classPath of project. Can anyone please help me to avoid this classPath file creation. Thanks in advance.

@Override public void downloadExcel(HttpServletRequest request,HttpServletResponse response) throws IOException { File file = new File("Segmentdetail.xlsx"); XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet spreadsheet = workbook.createSheet("SegmentLogs Info"); spreadsheet.setDefaultColumnWidth(20); .....Here is the logic for generating sheet which is quite big so iam skipping it. } FileOutputStream out = new FileOutputStream(file); workbook.write(out); downloadFile(file,response); out.close(); workbook.close(); } private void downloadFile(File file, HttpServletResponse response){ try { response.setContentType("application/vnd.ms-excel"); response.addHeader("content-disposition", "attachment; filename=Segmentdetail.xlsx"); response.setHeader("Pragma", "public"); response.setHeader("Cache-Control", "no-store"); response.addHeader("Cache-Control", "max-age=0"); FileInputStream fin = null; try { fin = new FileInputStream(file); } catch (final FileNotFoundException e) { e.printStackTrace(); } final int size = 1024; try { response.setContentLength(fin.available()); final byte[] buffer = new byte[size]; ServletOutputStream outputStream = null; outputStream = response.getOutputStream(); int length = 0; while ((length = fin.read(buffer)) != -1) { outputStream.write(buffer, 0, length); } fin.close(); outputStream.flush(); outputStream.close(); } catch (final IOException e) { e.printStackTrace(); } }catch (final Exception ex){ ex.printStackTrace(); } } 

2 Answers 2

1

Use an absolute path for the file where you are writing your excel to:

File file = new File( "C:\\Segmentdetail.xlsx"); // windows File file = new File("/home/usr/Segmentdetail.xlsx"); // unix 

A reasonable addition is to use a variable:

File file = new File(System.getenv("user.home"), "Segmentdetail.xlsx"); 

You can of course define a custom variable and use it, too.

Sign up to request clarification or add additional context in comments.

1 Comment

I don't want the file to create in my local machine.It should be in session and download from there.
0

Because you are creating file using below command

File file = new File("Segmentdetail.xlsx"); 

it will generate file in classpath

better to give path of file and same file path while you are downloading

File file = new File("c://Segmentdetail.xlsx"); 

3 Comments

sorry, I tried with the above one but ended with file not found exception. By the way i am using Ubuntu.
then specify path as below /home/usr/Filename and give same path while your are downloading as filename =
No it is not working same exception. Is there anyway to keep the excel sheet in session and download from there ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.