5 Replies Latest reply on Aug 28, 2008 10:30 AM by Peter Johnson

    Servlets sending zip files to clients do not work anymore af

    Marco Avila Newbie

      Hello,


      We have recently upgraded from Jboss 3.0.2 to Jboss 5.0 but code like this does not work anymore, if someone could confirm please and give some advice. We are using the next JSDK version:

      java version "1.6.0_06"
      Java(TM) SE Runtime Environment (build 1.6.0_06-b02)
      Java HotSpot(TM) Client VM (build 10.0-b22, mixed mode, sharing)

      Thanks.


      package servlets;
      
      import java.io.BufferedOutputStream;
      import java.io.ByteArrayOutputStream;
      import java.io.DataInputStream;
      import java.io.IOException;
      import java.io.PrintWriter;
      import java.net.URL;
      import java.net.URLConnection;
      import java.util.zip.ZipEntry;
      import java.util.zip.ZipOutputStream;
      
      import javax.servlet.ServletContext;
      import javax.servlet.ServletException;
      import javax.servlet.ServletOutputStream;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
      
      
      public class GenericPackager extends HttpServlet
      {
      
       /**
       * serialVersionUID.
       */
       private static final long serialVersionUID = -6328821464635051815L;
      
      
       /**
       * Constructor of the object.
       */
       public GenericPackager()
       {
       super();
       }
      
       /**
       * Destruction of the servlet. <br>
       */
       @Override
       public void destroy()
       {
       super.destroy(); // Just puts "destroy" string in log
       }
      
       /**
       * The doGet method of the servlet. <br>
       *
       * This method is called when a form has its tag value method equals to get.
       *
       * @param request
       * the request send by the client to the server
       * @param response
       * the response send by the server to the client
       * @throws ServletException
       * if an error occurred
       * @throws IOException
       * if an error occurred
       */
       @Override
       public void doGet(HttpServletRequest request, HttpServletResponse response)
       throws ServletException, IOException
       {
      
       doPost(request, response);
       }
      
       /**
       * The doPost method of the servlet. <br>
       *
       * This method is called when a form has its tag value method equals to
       * post.
       *
       * @param request
       * the request send by the client to the server
       * @param response
       * the response send by the server to the client
       * @throws ServletException
       * if an error occurred
       * @throws IOException
       * if an error occurred
       */
       @Override
       public void doPost(HttpServletRequest request, HttpServletResponse response)
       throws ServletException, IOException
       {
      
       byte b[] = new byte[300000];
      
       URL url;
       String temp;
      
       ByteArrayOutputStream bout = new ByteArrayOutputStream();
       ZipOutputStream zout = new ZipOutputStream(bout);
       //ServletOutputStream out = response.getOutputStream();
       PrintWriter out = response.getWriter();
       ServletContext servletContext = getServletContext();
      
      
       if ((temp = request.getParameter("url")) != null)
       {
       try
       {
       url = new URL(temp);
       URLConnection uc = url.openConnection();
      
       DataInputStream input = new DataInputStream(uc.getInputStream());
      
       int numRead = 0;
       int size = 0;
      
       while (numRead != -1)
       {
       numRead = input.read(b, size, 20000);
       size += numRead;
       }
      
       zout.putNextEntry(new ZipEntry("file.html"));
       zout.write(b, 0, size);
       zout.closeEntry();
       zout.finish();
       String zip = bout.toString();
      
       response.setContentType("application/zip");
       response.setHeader("Transfer-Encoding", "chunked");
       response.setHeader("Content-Disposition","inline; filename=output.zip;");
      
       out.println(zip);
       out.flush();
      
       } catch (Exception e)
       {
       response.setContentType("text/html");
       out.println("<html><head><title>Error</title></head>");
       out.println("<body><b>");
       out.println("An error has occured while processing " + temp
       + "<br>");
       out.println("Here is the exception: <br>" + e + "<br>");
       e.printStackTrace(new PrintWriter(out));
       out.println("</body>");
       out.println("</html>");
       }
       }
      
       }
      
       /**
       * Returns information about the servlet, such as author, version, and
       * copyright.
       *
       * @return String information about this servlet
       */
       @Override
       public String getServletInfo()
       {
       return "$Id: GenericPackager.java,v 1.1 2006/12/14 10:40:29 marco Exp $";
       }
      
       /**
       * Initialization of the servlet. <br>
       *
       * @throws ServletException
       * if an error occure
       */
       @Override
       public void init() throws ServletException
       {
      
       }
      
      }
      


      In web.xml
      ...
      <servlet>
       <servlet-name>TestPackaer</servlet-name>
       <display-name>Servlet for testing the zip file servlet works</display-name>
       <description>This servlet receives as parameter url=[url], zips the content and sends back the output in zipped file to the client.</description>
       <servlet-class>servlets.GenericPackager</servlet-class>
      
       </servlet>
      
      ...
      <servlet-mapping>
       <servlet-name>TestPackaer</servlet-name>
       <url-pattern>/servlet/testPacker</url-pattern>
       </servlet-mapping>
      ...