How to use outputStream to write data to web client?

48 views
Skip to first unread message

Terry

unread,
Oct 30, 2012, 12:52:52 PM10/30/12
to scooter-...@googlegroups.com
Hi John,

I'm trying to use this good framework in my new proj. And I just want to know is it possible to use outputStream(httpServletResponse) to write data(file) to web browser?

Now I created a controller and a method in it, and tried to writer a xls file to the client:

public String testFileDownLoad() {

HttpServletResponse  response = null;
OutputStream os=null;
try{
response = getHttpServletResponse();
os=response.getOutputStream();
response.setHeader( "Content-Disposition", "attachment;filename="  + new String("testExport.xls".getBytes(),"UTF-8"));
response.setContentType("application/msexcel");    
                        /* write some data

                        .........................................
                         */
                        
} catch(Exception e) {
log.error("export exception", e);
}
}

But I got the exception like this,
ERROR RestfulRequestProcessor - Error in "/dataexport/publicroomexport": java.lang.IllegalStateException: Committed
java.lang.IllegalStateException: Committed
at org.mortbay.jetty.Response.resetBuffer(Response.java:1023)
at javax.servlet.ServletResponseWrapper.resetBuffer(ServletResponseWrapper.java:202)
at org.mortbay.jetty.servlet.Dispatcher.forward(Dispatcher.java:211)
at org.mortbay.jetty.servlet.Dispatcher.forward(Dispatcher.java:126)
at com.scooterframework.web.controller.ActionControl.doForward(ActionControl.java:1414)
at com.scooterframework.web.controller.BaseRequestProcessor.doForward(BaseRequestProcessor.java:734)
at com.scooterframework.web.controller.BaseRequestProcessor.processNullResult(BaseRequestProcessor.java:561)
at com.scooterframework.web.controller.BaseRequestProcessor.process(BaseRequestProcessor.java:115)
at com.scooterframework.web.controller.MainActionServlet.process(MainActionServlet.java:199)
at com.scooterframework.web.controller.MainActionServlet.service(MainActionServlet.java:168)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1221)
at com.opensymphony.module.sitemesh.filter.PageFilter.parsePage(PageFilter.java:119)
at com.opensymphony.module.sitemesh.filter.PageFilter.doFilter(PageFilter.java:55)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1212)
at com.scooterframework.web.controller.ScooterRequestFilter.doFilter(ScooterRequestFilter.java:138)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1212)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:399)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:766)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:450)
at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:230)
at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:322)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:928)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:549)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:410)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
12/10/31 00:48:28,945 ERROR ScooterRequestFilter - Error from chain.doFilter: Committed
java.lang.IllegalStateException: Committed
at org.mortbay.jetty.Response.resetBuffer(Response.java:1023)
at javax.servlet.ServletResponseWrapper.resetBuffer(ServletResponseWrapper.java:202)
at org.mortbay.jetty.servlet.Dispatcher.forward(Dispatcher.java:211)
at org.mortbay.jetty.servlet.Dispatcher.forward(Dispatcher.java:126)
at com.scooterframework.web.controller.ActionControl.doForward(ActionControl.java:1414)
at com.scooterframework.web.controller.BaseRequestProcessor.doForward(BaseRequestProcessor.java:734)
at com.scooterframework.web.controller.BaseRequestProcessor.doForwardToErrorPage(BaseRequestProcessor.java:709)
at com.scooterframework.web.controller.BaseRequestProcessor.processException(BaseRequestProcessor.java:644)
at com.scooterframework.web.controller.BaseRequestProcessor.process(BaseRequestProcessor.java:120)
at com.scooterframework.web.controller.MainActionServlet.process(MainActionServlet.java:199)
at com.scooterframework.web.controller.MainActionServlet.service(MainActionServlet.java:168)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1221)
at com.opensymphony.module.sitemesh.filter.PageFilter.parsePage(PageFilter.java:119)
at com.opensymphony.module.sitemesh.filter.PageFilter.doFilter(PageFilter.java:55)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1212)
at com.scooterframework.web.controller.ScooterRequestFilter.doFilter(ScooterRequestFilter.java:138)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1212)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:399)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:766)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:450)
at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:230)
at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:322)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:928)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:549)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:410)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
12/10/31 00:48:28,948  INFO ScooterRequestFilter - "GET /dataexport/publicroomexport" takes: 225 ms

John Chen

unread,
Oct 31, 2012, 10:18:19 PM10/31/12
to scooter-...@googlegroups.com
Terry,

Sorry to respond to you late. 

Can you do a static import of ActionControl class? There are a few publishFile() APIs for you to use in that class.

I think you can try this API:
    /**
     * Publishes a file. Its MIME type is detected from the file extension.
     * 
     * <p>
     * The file extension detection rule is as follows:
     * 1. If the <tt>file</tt> input has an extension, use it. Otherwise, 
     * 2. If the <tt>displayableName</tt> input has an extension, use it.
     * 
* @param file  The file to be published.
* @param displayableName  The display name of the file in the download dialog.
public static void publishFile(File file, String displayableName, boolean forDownload)

John

Terry

unread,
Nov 1, 2012, 6:17:00 AM11/1/12
to scooter-...@googlegroups.com
Thanks! I'll try it later.
Reply all
Reply to author
Forward
0 new messages