0

I have a form that takes a PDF file and submits to servlet. After reading the content of PDF I am creating a xls file in servlet using Apache POI that I want to save to my local disc.

            HSSFWorkbook workbook = new HSSFWorkbook();
            HSSFSheet sheet = workbook.createSheet("Data");

            ArrayList<String> keys = new ArrayList<String>(excelData.keySet());

            int counter = 0;

            for(int i=keys.size()-1; i>=0;i--){

              String key = keys.get(i);
              String value = excelData.get(key);

              Row row = sheet.createRow(counter++);

              Cell cell = row.createCell(0);
              cell.setCellValue(key);

              Cell cell1 = row.createCell(1);
              cell1.setCellValue(value);  
            }

//work book is the xls data that currently I am saving like ,

                FileOutputStream out = new FileOutputStream(new File("C:\\Users\\user\\Desktop\\new.xls"));
                workbook.write(out);
                out.close();

                //return back to same page.
                response.sendRedirect(request.getHeader("Referer"));

Instead of saving file directly to C:\\Users\\user\\Desktop\\new.xls,I want to give a save as option to select the path and name of the file that user want to save. Some thing like :

enter image description here

How can I send this file back to the client browser and show save as option.

Naveen
  • 773
  • 3
  • 17
  • 40

1 Answers1

0
                //read file back after saving to some temp path.
                File pdfFile = new File("D:\\temp\\"+ fileName +".xls");

                response.setContentType("application/vnd.ms-excel");
                response.setHeader("Content-Disposition", "attachment; filename="+fileName+".xls");

                FileInputStream fileInputStream = new FileInputStream(pdfFile);
                OutputStream responseOutputStream = response.getOutputStream();
                int bytes;
                while ((bytes = fileInputStream.read()) != -1) {
                    responseOutputStream.write(bytes);
                }

                fileInputStream.close();
Naveen
  • 773
  • 3
  • 17
  • 40