Downloading & Saving excel files coming in RESPONSE???


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Downloading & Saving excel files coming in RESPONSE???

Hybrid View

  1. #1
    Join Date
    Apr 2005
    Posts
    15

    Downloading & Saving excel files coming in RESPONSE???

    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

  2. #2
    Join Date
    Feb 2006
    Location
    Cologne - Germany
    Posts
    271
    what error is reported, when you invoke the deletion of the files?
    perhaps something holds a handle on that file, so you cannot delete it.
    it should throw at least an exception.
    by the way: i never saw a multi file download. i'm not shure, if that's possible with http.

Similar Threads

  1. Replies: 0
    Last Post: 12-23-2005, 12:26 PM
  2. Using DB for Saving Files
    By eeamh in forum VB Classic
    Replies: 3
    Last Post: 03-07-2002, 01:16 PM
  3. Replies: 1
    Last Post: 07-21-2000, 09:35 AM
  4. Replies: 0
    Last Post: 07-20-2000, 03:57 AM
  5. Replies: 0
    Last Post: 07-20-2000, 03:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center