Hi everyone.  Apologies for reposting, but I am really stuck on this one, and
wondering if anyone has any ideas?!

After calling the action below to download a data directory, everything goes
fine, but somehow I lose the response output stream permanently!  Thereafter,
whatever I try and view on the site is processed, but nothing gets displayed!

Do I somehow have to reset the stream?  Is there any side effects from returning
null?

Any help would really be appreciated!

Thanks,

Dave

--------------------------------------------------------------------------
DownloadDataDirAction.java:

package beans;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.zip.ZipOutputStream;
import java.util.zip.ZipException;
import java.util.zip.ZipEntry;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 * Implementation of <strong>Action</strong> that downloads the filtered log to
 * the users disk.
 */

public final class DownloadDataDirAction extends Action
{

   // --------------------------------------------------------- Public Methods

   /**
    * Process the specified HTTP request, and create the corresponding HTTP
    * response (or forward to another web component that will create it).
    * Return an <code>ActionForward</code> instance describing where and how
    * control should be forwarded, or <code>null</code> if the response has
    * already been completed.
    *
    * @param mapping The ActionMapping used to select this instance
    * @param actionForm The optional ActionForm bean for this request (if any)
    * @param request The HTTP request we are processing
    * @param response The HTTP response we are creating
    *
    * @exception IOException if an input/output error occurs
    * @exception ServletException if a servlet exception occurs
    */
   public ActionForward perform(ActionMapping mapping,
                                ActionForm form,
                                HttpServletRequest request,
                                HttpServletResponse response)
   throws IOException, ServletException
   {

      HttpSession session = request.getSession();
      ServerBean sb = (ServerBean)session.getAttribute("ServerBean");
      if (sb == null)
      {
         try
         {
            sb = new ServerBean();
            session.setAttribute("ServerBean", sb);
         }
         catch (ServerConnectionException sce)
         {
            throw new ServletException("Connection Error", sce);
         }
      }

      try
      {

         String dataDir =
sb.getAbsolutePath(sb.getParameter("directories.dataDir"));
         System.out.println("dataDir: " + dataDir);


         //create output stream to write entries to
         response.setContentType("zip");
         response.setHeader("Content-disposition", "attachment;
filename=\"DataDir.zip\"");
         response.addHeader("Content-description", "my description");

         //create zip stream, and recursively add files/subdirectories to zip
         ZipOutputStream out = new ZipOutputStream(response.getOutputStream());

         addToZip(dataDir, out);


         out.flush();
         out.close();
      }
      catch (ServerOperationException soe)
      {
         throw new ServletException("Operation Error - getting data directory
name", soe);
      }
      catch (ServerConnectionException sce)
      {
         throw new ServletException("Connection Error", sce);
      }
      catch (Exception e)
      {
         //TODO: forward to error page
         System.out.println("Exception in ShowLogAction " + e.toString());
         e.printStackTrace();
         //throw new LogFileFormatException("ShowLogFilterAction: Exception
processing Log Filter " + e.toString());
      }

      //TODO: DELETE SESSION VARIABLES!!
   }

   /**
    * Helper method which recursively zips and adds files and sub-directories to
 a
    * zip output stream.
    *
    * @param   filename String representing a file or directory to add
    * @param   stream   open zip output stream to add to
    */
   private void addToZip(String filename, ZipOutputStream out)
   throws FileNotFoundException, java.io.IOException
   {
      File file = new File(filename);
      //if file is a file, add to zip
      if(file.exists() && file.isFile())
      {
         BufferedInputStream bis = new BufferedInputStream(new
FileInputStream(file));
         ZipEntry ze = new ZipEntry(filename);
         out.putNextEntry(ze);

         //read/write file to zip
         byte[] data = new byte[1024];
         int byteCount;

         while((byteCount = bis.read(data, 0, 1024)) > -1)
         {
            out.write(data, 0, byteCount);
         }
      }
      //else, get files in directory, and recursively call myself for each one
      else if(file.exists() && file.isDirectory())
      {
         File[] files = file.listFiles();

         for(int i = 0; i < files.length; i++)
         {
            addToZip(files[i].getPath(), out);
         }
      }
   }
}
---------------------- Forwarded by David Hay/Lex/Lexmark on 07/23/2001 04:06 PM
---------------------------


David Hay
07/20/2001 04:36 PM

To:   [EMAIL PROTECTED],
      [EMAIL PROTECTED]
cc:

Subject:  Where did it go?!  (Document link: David Hay)

Hi everyone.

I have something strange happening.  I have a 2 frame frameset, with buttons in
the top fram to let a user view a logfile in the lower frame, or download it.
If the chose to download the file, I get the ServletOutputStream from the
response in my action, and write to it.  I then close it and return null.

All that works fine - the user is prompted to download the file etc. - but I
then hit my strange problem.  If I click on the view button in the top frame,
nothing appears in the bottom frame, although I see in the tomcat window that
everything is geting processed like before.  It is as if it is writing it into
thin air!

Would really appreciate any help to find it!!

Cheers,

Dave





Reply via email to