6 Replies Latest reply on Jan 28, 2008 5:12 AM by Christian Bauer

    JBoss Seam problem with download file big file

    Grigoriy Tkachuk Newbie

      I try modify File Servlet from wiki example. Because I need get file from filesystem. My code look:

      /*
       * To change this template, choose Tools | Templates
       * and open the template in the editor.
       */
      package ru.fuckorama.servlets;
      
      import javax.persistence.EntityManager;
      import javax.persistence.NoResultException;
      import javax.servlet.ServletException;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
      import javax.transaction.UserTransaction;
      import java.io.ByteArrayOutputStream;
      import java.io.DataInputStream;
      import java.io.File;
      import java.io.FileInputStream;
      import java.io.IOException;
      import java.io.InputStream;
      import java.io.OutputStream;
      
      public class FileServlet extends HttpServlet {
      
       private static final String DOWNLOAD_PATH = "/download.seam";
       private byte[] fileNotFoundImage;
      
       public FileServlet() {
       InputStream in = getClass().getResourceAsStream("/img/filenotfound.png");
       if (in != null) {
       ByteArrayOutputStream out = new ByteArrayOutputStream();
       byte[] buffer = new byte[512];
       try {
       int read = in.read(buffer);
       while (read != -1) {
       out.write(buffer, 0, read);
       read = in.read(buffer);
       }
      
       fileNotFoundImage = out.toByteArray();
       } catch (IOException e) {
       }
       }
      
       }
      
       @Override
       protected void doGet(HttpServletRequest request, HttpServletResponse response)
       throws ServletException, IOException {
       File file = null;
       int length = 0;
       if (DOWNLOAD_PATH.equals(request.getPathInfo())) {
       UserTransaction userTx = null;
       boolean startedTx = false;
       try {
      
       userTx = (UserTransaction) org.jboss.seam.Component.getInstance("org.jboss.seam.transaction.transaction");
       if (userTx.getStatus() != javax.transaction.Status.STATUS_ACTIVE) {
       startedTx = true;
       userTx.begin();
       }
      
       String id = request.getParameter("fileId");
       file = new File("c:/arch/" + id);
       length = (int) file.length();
      
       if (startedTx) {
       userTx.commit();
       }
       } catch (Exception ex) {
       try {
       if (startedTx) {
       userTx.rollback();
       }
       } catch (Exception rbEx) {
       rbEx.printStackTrace();
       }
       throw new RuntimeException(ex);
       }
       response.setContentType("application/pdf");
      
       response.setHeader("Content-Disposition", "attachement; filename=\"" + file.getName() + "\"");
      
       FileInputStream fileToDownload = new FileInputStream(file);
       response.setContentLength(fileToDownload.available());
       int c;
       while ((c = fileToDownload.read()) != -1) {
       response.getOutputStream().write(c);
       }
      
      
       response.getOutputStream().flush();
       }
       }
      }
      


      When I ask from server big file(100mb), I get error with OutMemoryHeap on Jboss server. Please somebody help me.....