9 Replies Latest reply: Jun 10, 2011 3:35 AM by gimbal2 RSS

    Browser freezes after export data to csv file using struts action & Servlet

    698590
      Hello Everyone,

      I tried two approaches to export CSV data and each one gives a different issue.

      Approach 1 - Using a Struts Action class and Servlet*

      Here the problem is when i export the file, it gives me an option to save/open the file and after that the mouse cursor will show the waiting icon and i cant do anything on the browser window. the browser completely freeze there and i cant do anything on it unless i refresh the page. Anybody know whats going on here? See the code below:

      Struts action class
       
      public class ExportUserDetail extends Action {
      public ActionForward execute(ActionMapping mapping, ActionForm form,
                     HttpServletRequest request, HttpServletResponse response) 
          {
           MyForm aForm = (MyForm) form;
           String exportpath = "servlets/ExportServlet?userType="
                     + aForm.getUserType() + "&fromDate=" + aForm.getFromDate()
                     + "&toDate=" + aForm.getToDate() + "&userId="
                     + aForm.getUserId();
           try {
                URI uri = null;
                PageURL urlRender = PageURL.createPageURL(request, response);
                urlRender.setContextualPath(exportpath);
                String urlpath = urlRender.toString();
                urlpath = urlpath.substring(0, urlpath.lastIndexOf("?"));
                uri = new URI(urlpath);
                System.out.println("uri : " + uri);
                response.sendRedirect(uri.toString());
      
           } catch (Exception e) {
                     e.printStackTrace();
           }
           return null;
           }
      }
      Export Servlet
      public class ExportServlet extends HttpServlet 
      {
           public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
           {
                     
              ServletOutputStream out = null;
               BufferedInputStream  bis = null;
              BufferedOutputStream bos = null;   
                try 
                {
                     MyForm form = new MyForm();                              
      
                     String userType = request.getParameter("userType");
                     String fromDate = request.getParameter("fromDate");
                     String toDate = request.getParameter("toDate");
                     String userId = request.getParameter("userId");
      
                     form.setUserType(userType);
                     form.setFromDate(fromDate);
                     form.setToDate(toDate);
                     form.setUserId(userId);
      
                     List list = AppUtil.getUserNameAndCount(form);
                     String outputDocument=getCSVString(list);                    
                          
                     try {
                          out = response.getOutputStream ();                    
                     } catch(IOException e) {                       
                          e.printStackTrace()                     
                     }
      
                     response.setContentType("application/vnd.ms-excel");                      
                     response.setHeader("Content-disposition","attachment; filename=export.csv");
                     bis = new BufferedInputStream(new ByteArrayInputStream(outputDocument.getBytes()));
      
                     bos = new BufferedOutputStream(out);
      
                     byte[] buff = new byte[2048];
                     int bytesRead;
                     while(-1 != (bytesRead = bis.read(buff, 0, buff.length))){
      
                          bos.write(buff, 0, bytesRead);
                          bos.flush();             
                     } 
                     out.flush(); 
                       
                } catch (Exception e) {
                     e.printStackTrace();
                }finally{
      
                     if (out != null) {                        
                     out.close();
                     out = null;
                     }
      
                     if (bis != null) {
                          bis.close();
                           bis =null;
                   }
                   if (bos != null) {
                           bos.close();
                           bos=null;
                   }
                }                    
           }
      }
      Struts.xml file entry.

      <action path="/filedownload" type="com.myapp.ExportUserDetail" name="myForm" scope="session" validate="false">
      </action>


      Approach 2 - Using a Struts DownloadAction class*

      This approach is giving me new issue. I got the below error when i tried to execute the ReportDownload Action class. code attached

      Error:

      <Jun 7, 2011 4:55:23 PM CDT> <Warning> <org.apache.struts.action.RequestProcessor> <BEA-000000> <Unhandled Exception thrown: class java.lang.IllegalStateException>

      My Class which extends DownloadAction
      public class ReportDownload extends DownloadAction  {
      
            protected StreamInfo getStreamInfo(ActionMapping mapping, ActionForm form,
                     HttpServletRequest request, HttpServletResponse response)
                     throws Exception {
      
                MyForm aForm = (MyForm) form;
                List list = ActivityUtil.getUserNameAndCount(aForm);
                String outputDocument = getCSVString(list);
                
                response.setContentType("application/vnd.ms-excel");
                response.setHeader("Content-disposition","attachment; filename=export.slk");
                String contentType = "application/vnd.ms-excel";
                byte[] slkBytes = outputDocument.getBytes();          
                return new ByteArrayStreamInfo(contentType, slkBytes);
      
            }
           
           protected class ByteArrayStreamInfo implements StreamInfo 
           {
                protected String contentType;
                protected byte[] bytes;
                public ByteArrayStreamInfo(String contentType, byte[] bytes) {
                     this.contentType = contentType;
                     this.bytes = bytes;
                }
                public String getContentType() {
                     return contentType;
                }
                public InputStream getInputStream() throws IOException {
                     System.out.println("getStreamInfo-getInputStream-1" + bytes);
                     return new ByteArrayInputStream(bytes);
                }
           }
      
           public String getCSVString(List list){
                //GENERATES CSV STRING
              return csv.toString();
           }
      }
      Finally when i ran the application, console printed the SOP line inside the getInputStream function and then i got the "RequestProcessor" error message which is given above.

      I am running out of options here. Please help

      Edited by: john on Jun 8, 2011 8:13 AM

      Edited by: john on Jun 8, 2011 12:19 PM