Hi,
In my web-based application using JSP & Servlets I am generartiung some reports as excel files.These excel files i am sending in response for download to the users who are using my application through web.
Now, I am able to download & save the files as excel files.
But one problem:
When i have a single file i am able to download & save it easily.
But in some cases i have mutiple files to be downloaded & saved so how can i do this thing of downloading & saving multiple files coming in the response.
Also i am not able to delete my file on the server after downloading it
Here is my code for downloading my reports:
Code:public boolean downloadReport(String dirPath,HttpServletResponse response) { FileInputStream fis = null; boolean flag = false; File f = new File(dirPath); ArrayList tempFileArrList = new ArrayList(); if(f.isDirectory()) { File[] fArray = f.listFiles(); for(int i = 0; i < fArray.length; i++) { log.debug("File Names Array Content : fArray [" + i + "]" + fArray[i]); if(fArray[i].exists()) { try { fis = new FileInputStream(fArray[i]); int fileLength = fis.available(); byte[] bytes = new byte[fileLength]; fis.read(bytes); response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-Disposition","attachment; filename = \"myxlfile.xls\" "); response.getOutputStream().write(bytes); response.getOutputStream().flush(); //response.getOutputStream().close(); //fis.close(); tempFileArrList.add(fArray[i]); //fArray[i].delete(); log.debug("DEBUG 10"); flag = true; } catch(IOException ioe) { log.debug("IOException in GMGController while download "+ioe); flag = false; } catch(Exception e) { log.debug("Exception in GMGController while download "+e); flag = false; } } else { continue; } } try { response.getOutputStream().close(); fis.close(); flag = true; } catch(IOException ioe) { log.debug("IOException in GMGController while closing "+ioe); flag = false; } catch(Exception e) { log.debug("Exception in GMGController while closing "+e); flag = false; } } for(int i = 0; i < tempFileArrList.size(); i++ ) { File file = (File)tempFileArrList.get(i); if(file.exists()) { log.info("Deleting file "+ (i+1) + " "+ file.getName()); file.delete(); } } return flag; }
Help me sorting out this...
Jignesh


Reply With Quote


Bookmarks