0
回答
springMVC文件下载出现的问题
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   

我所书写的代码如下:

@RequestMapping("download")
 public String download(String img,HttpServletRequest request,HttpServletResponse response)throws Exception{
  String nowPath=request.getSession().getServletContext().getRealPath(img);
  File file=new File(nowPath);
  response.reset();
  response.addHeader("Content-Disposition", "attachment;filename="+new String(img.getBytes("gbk"),"iso-8859-1"));
        response.addHeader("Content-Length", "" + file.length());
        try{
          InputStream in=new BufferedInputStream(new FileInputStream(nowPath));
          byte[] array=new byte[in.available()];
          in.read(array);
          in.close();
         ServletOutputStream os= response.getOutputStream();
          OutputStream out=new BufferedOutputStream(os);
          out.write(array);
          out.flush();
          out.close();
        }catch(Exception e){
          e.printStackTrace();
      }
  return "index.jsp";
 }

程序能执行下载,但是会抛出

java.lang.IllegalStateException: getOutputStream() has already been called for this response

的错误,请问怎么解决呢?这个比较急用哈,帮帮忙

举报
吕檀溪
发帖于4年前 0回/1K+阅
顶部