I am not clear about your problem but the following code sample works 
fine. Maybe the only important difference is that the perform method 
returns null ?

   public ActionForward perform(ActionMapping mapping,
                                ActionForm form,
                                HttpServletRequest request,
                                HttpServletResponse response)
       throws IOException, ServletException {

     HttpSession     session = request.getSession();

     (...)

     // a temporary output stream
     ByteArrayOutputStream bout = new ByteArrayOutputStream();

     // a writer to write String to output Stream.
     Writer writer = new BufferedWriter(new OutputStreamWriter(bout));
     writer.write(xmldoc); // xmldoc could be your "csv.dat" as String
     writer.close();

     byte[] content = bout.toByteArray();
     // set the exact content length from our temp output stream
     response.setContentLength(content.length);

     // write the bytes content
     response.getOutputStream().write(content);

     // flush the stream
     response.getOutputStream().flush();

     return null;
   }

Mrazovic, Maik wrote:
> Server generates a textfile. After generating server read the file and sends
> it to the client. Well, thats noe problem I thought, but a problem within
> Struts!
> 
> While server wants to make this above within an Action there come en error: 
> 
> "2002-07-22 12:46:44 - Ctx( /impact ): IllegalStateException in: R( /impact
> + /createCSV2.do + null) Cannot forward as OutputStream or Writer has
> already been obtained"
> 
> 
> But if server makes this by a servlet it works! Isnīt that strange? 
> 
> The code:
> 
> //userObject from session
>         HttpSession session = request.getSession(true);
>         de.tsystems.impact.model.User user =
>             ( de.tsystems.impact.model.User )
> session.getAttribute(de.tsystems.impact.constants.Constants.USER_KEY);
> 
>           //debug-Info
>           de.tsystems.impact.util.ParameterTest.printParameters(request);
> 
>         //App.Key: "sql.csv.generate"
>         String stmt = "something...'";
> 
>         DBConnector con = DBConnector.getInstance();
>         java.util.List listResult = con.executeDBStatement(stmt);
> 
>         java.util.Iterator iter = listResult.iterator();
> 
>         //Vector we fill with ImpactCSV-Objects
>         java.util.Vector vectorWithCSVData = new java.util.Vector();
> 
>         while (iter.hasNext()) {
>           java.util.List vec = (java.util.List) iter.next();
>             vectorWithCSVData.add(new de.tsystems.impact.model.ImpactCSV(
>               ((String) vec.get(2)), //land
>               ((String) vec.get(0)), //bm
>               ((String) vec.get(1)), //Version
>               ((String) vec.get(4)), //Monat
>               ((String) vec.get(3)), //Jahr
>               ((String) vec.get(5))  //MengeLPP
>               ));
>         }
> 
>         //create empty CSV-File on server
>         String file = getServletContext().getRealPath("") +
> java.io.File.separator + "web-inf" + java.io.File.separator
>                       + "csv.dat";
> 
>         //Buffersize for reading and sending data
>         int buffer = 10 * 1024;   //10 kb, increase if necessary
> 
>         //fill the "csv.dat" at "..\WEB-INF\csv.dat".
>         FileOutputStream fos = new FileOutputStream(new File(file));
>         PrintWriter pw = new PrintWriter(fos);
> 
>         for (int i = 0; i < vectorWithCSVData.size(); i++)
>         {
>             de.tsystems.impact.model.ImpactCSV impactSCV =
>                 ( de.tsystems.impact.model.ImpactCSV )
> vectorWithCSVData.get(i);
> 
>             pw.write(impactSCV.getLandnummer() + ";" +
> impactSCV.getBaumuster() + ";" + impactSCV.getVersion() + ";"
>                      + impactSCV.getMonat() + ";" + impactSCV.getJahr() +
> ";" + impactSCV.getWert() + "\n");
>         }
> 
>         //clean up all ressources
>         pw.flush();
>         pw.close();
>         fos.close();
> 
>         //Now sending this file to client
>         try
>         {
>             //BufferedOutputStream out = new
> BufferedOutputStream(response.getOutputStream(), buffer);
>             javax.servlet.ServletOutputStream out =
> response.getOutputStream();
> 
>             String contentType = getServletContext().getMimeType(file);
> 
>             response.setContentType(contentType);
> 
>             FileInputStream fis = new FileInputStream(file);
>             byte[] buf = new byte [buffer];
>             int bytesRead;
> 
>             while ((bytesRead = fis.read(buf)) != -1)
>             {
>                 out.write(buf, 0, bytesRead);
>             }
> 
>             out.flush();
>             fis.close();
>         }
>         catch (Exception e)
>         {
>             e.printStackTrace();
>             response.sendError(response.SC_NOT_FOUND, e.toString());
>         }
> 
> --
> To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>
> 
> 



--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to