struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Mrazovic, Maik" <Maik.Mrazo...@t-systems.com>
Subject Strutsproblem: Sending files from Server to client!
Date Mon, 22 Jul 2002 10:49:30 GMT
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:struts-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:struts-user-help@jakarta.apache.org>


Mime
View raw message