开发者社区> 问答> 正文

java servlet?报错

我用file来将一个字符串写入一个xls文件中,然后再用response.getOutputStream()的方法下载为啥会报错,大神们求指教。。。。。。。

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String str1="Hellow Data";
        File file=new File("I:\\bn\\nihao.xls");
if(!file.exists()){
file.createNewFile();
}
byte bytes[]=new byte[512];
bytes=str1.getBytes();   
int len=str1.length();   
FileOutputStream fileout=new FileOutputStream(file);
fileout.write(bytes,0,len);
fileout.close();
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/x-msdownload");
String fileName = "servletDownload.docx";
response.setHeader("Content-Disposition","attachment;filename=" + URLEncoder.encode(fileName,"UTF-8"));
OutputStream out1 = response.getOutputStream();
String str = "I:\\bn\\nihao.xls";
InputStream in = new FileInputStream(str);
byte[] buffer = new byte[1024];
int len = 0;
while((len = in.read(buffer))!=-1){
out1.write(buffer,0,len);
}
in.close();
}

展开
收起
爱吃鱼的程序员 2020-06-09 16:38:22 602 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    首先,没有错误日志信息。不知道是哪一部分出的错~

    其次,你这样写,就算java认为没错,但是人类是不能认同的::xls是 带格式的二进制文件,直接写纯文本不太好吧;然后你下载文件时在content-type里又给了一个docx的后缀!

    @IT菜_鸟我想让他在根目录下新建一个.xls文件然后将查询的数据写入,然后出现下载界面 @IT菜_鸟我是想我想想让他在根目录下新建一个.xls文件然后将查询的数据写入,然后出现下载界面回复 @Sel8616:大神我的错误信息已经贴出来了回复 @IT菜鸟求学:没有错误信息~~ @IT菜鸟求学

    已经很明显了,response的getOutputStream()方法只能调用一次,且和response.getWriter()冲突。

    你上面的代码中没有重复调用,但是显然在 JspWriterImpl里还调用了这个response的getWriter()

    百度一下下面的错误信息,可以有一堆答案,都可以帮你。

    但最好的方法是,看一下Servlet 源码中 HttpServletResponse的getWriter()和getOutputStream()的注释,已经写得很清楚了。

    java.lang.IllegalStateException:getOutputStream()hasalreadybeencalledforthisresponse



    非常感谢大神[13]你如果要出现下载页面,应该重定向到对应的download.jsp页面。不是直接往当前的response里写文件流。文件下载最好配置静态资源的映射,不要在用户的servlet里面处理。你用了apache的jsp视图框架,输出流被它接管了。

    @Sel8616: 

    2016-3-1815:47:25org.apache.catalina.core.StandardWrapperValveinvoke

    严重:Servlet.service()forservletjspthrewexception
    java.lang.IllegalStateException:getOutputStream()hasalreadybeencalledforthisresponse
    atorg.apache.catalina.connector.Response.getWriter(Response.java:610)
    atorg.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:198)
    atorg.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)
    atorg.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118)
    atorg.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:188)
    atorg.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:118)
    atorg.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:77)
    atorg.apache.jsp.download_jsp._jspService(download_jsp.java:118)
    atorg.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    atjavax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    atorg.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
    atorg.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
    atorg.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
    atjavax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    atTestFilter.doFilter(TestFilter.java:30)
    atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    atorg.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    atorg.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    atorg.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    atorg.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    atorg.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    atorg.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    atorg.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    atorg.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
    atorg.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    atorg.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
    atjava.lang.Thread.run(Thread.java:619)

    非常感谢大神
    2020-06-09 16:38:40
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载