前台请求一个ajax,导出一个word,如果导出成功则返回一个成功标志报错

前台请求一个ajax,导出一个word,如果导出成功则返回一个成功标志,代码如下,为什么执行到红色的哪一行就报错了呢,错误如下:

严重: Servlet.service() for servlet action threw exception
java.lang.IllegalStateException: getOutputStream() has already been called for this response

 

代码如下:

if("AJAXEXPORTWORD".equals(actiontype)){
File file =(File)rowSet.getVpo();
String uuid = UUID.randomUUID().toString() + ".doc";
response.reset();
response.setContentType("application/msword;charset=GBK");
response.setHeader("Content-Disposition", (new StringBuilder(
"attachment; filename=")).append(uuid).toString());
BufferedOutputStream bsout = new BufferedOutputStream(response.getOutputStream());
      try {
        IOUtils.write(FileUtils.readFileToByteArray(file), bsout);
        bsout.flush();
      }
      finally {
        IOUtils.closeQuietly(bsout);
      } 
      PrintWriter out = response.getWriter();
      out.print(getJsonFromKeyValue(new String[]{"result"},new String[]{"1"}));
return null;
}

 如果这样写不对请指正或者有更好的写法吗

BufferedOutputStream bsout = new BufferedOutputStream(response.getOutputStream());
这里说明已经向浏览器输出流了,
PrintWriter out = response.getWriter(); 这里也是向浏览器里写入字符,所以是不能同时调用的。
java.lang.IllegalStateException: getOutputStream() has already been called for this response
这句话已经说明了问题了,就是你已经调用了getOutputStream()

IOUtils.closeQuietly(bsout); 这个colse导致的, 你把它向后移就不会报错了 试试看

有个比较简单的方法,直接定位到一个新的页面,在新页面导出,这样你就不用管它的返回标记了。