NutzCN Logo
问答 EXCEL文件下载的问题
发布于 2563天前 作者 qq_09dec3d3 1490 次浏览 复制 上一个帖子 下一个帖子
标签:

数据库中的记录转成EXCEL文件放在服务器上,下载时报

17-12-16 14:59:46.795 WARN [http-nio-8084-exec-189] Error@/update/download :
java.lang.IllegalStateException: getOutputStream() has already been called for this response
	at org.apache.catalina.connector.Response.getWriter(Response.java:578)
	at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:212)
	at org.nutz.mvc.view.UTF8JsonView.render(UTF8JsonView.java:65)
	at org.nutz.mvc.impl.processor.ViewProcessor.process(ViewProcessor.java:66)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.MethodInvokeProcessor.process(MethodInvokeProcessor.java:33)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.AdaptorProcessor.process(AdaptorProcessor.java:30)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.ActionFiltersProcessor.process(ActionFiltersProcessor.java:58)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.ModuleProcessor.process(ModuleProcessor.java:123)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.EncodingProcessor.process(EncodingProcessor.java:27)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor.process(UpdateRequestAttributesProcessor.java:15)
	at org.nutz.mvc.impl.NutActionChain.doChain(NutActionChain.java:44)
	at org.nutz.mvc.impl.ActionInvoker.invoke(ActionInvoker.java:67)
	at org.nutz.mvc.ActionHandler.handle(ActionHandler.java:31)
	at org.nutz.mvc.NutFilter.doFilter(NutFilter.java:202)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
	at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:745)

而实际上我根本就没有用Response.getWriter,而是用的outputStream,这个是问什么?

filename=new String(filename.getBytes(),"ISO8859-1");
        /*if (userAgent.contains("MSIE")||userAgent.contains("Trident")) {
             filename = java.net.URLEncoder.encode(filename, "UTF-8");
              } else {
//非IE浏览器的处理:
               filename = new String(filename.getBytes("UTF-8"),"ISO-8859-1");
              }*/
              response.reset();
              response.setHeader("Content-disposition", String.format("attachment; filename=\"%s\"", filename));
              response.setContentType("application/vnd.ms-excel;charset=utf-8");
              response.setCharacterEncoding("UTF-8"); 
              response.setContentType("application/vnd.ms-excel");
       
        OutputStream out = null;
        InputStream in=null;
        try {
            out = response.getOutputStream();
            //out.flush();
            in = new FileInputStream(downfile);
            byte[] charbuffer = new byte[1024];
            int length = 0;
             while ((length=in.read(charbuffer))!=-1) 
             {
                
                 out.write(charbuffer, 0, length);
                 out.flush();
             }

重数据库到记录生成EXCEL,需要在服务器上生成后下载吗?可以直接送前台吗,谢谢

6 回复

用了这个倒是无报错了,但前台无下载框出现啊

来自炫酷的 NutzCN

从数据库中导记录形成文件供前台下载还有简便方法没?

来自炫酷的 NutzCN

走ajax?得开iframe或者找个ajaxdownload插件才可以的

这么麻烦,还有招么,老大

我知道的就这2个办法

添加回复
请先登陆
回到顶部