首页 新闻 搜索 专区 学院

getWrite和getOutputStream冲突问题

0
悬赏园豆:5 [已解决问题] 解决于 2020-01-30 22:16

package shiyao.frist.test.文件下载;

import java.io.FileInputStream;
import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**

  • Servlet implementation class FileDownLoad
    */
    @WebServlet("/FileDownLoad")
    public class FileDownLoad extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
    • @see HttpServlet#HttpServlet()
      */
      public FileDownLoad() {
      super();
      // TODO Auto-generated constructor stub
      }
    /**
    • @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
      */
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      // TODO Auto-generated method stub
      response.getWriter().append("Served at: ").append(request.getContextPath());

      //1.获取文件请求参数,也就是文件名称
      String Filename=request.getParameter("Filename");
      //2.获取文件的服务器路径即真实路径
      ServletContext sc=this.getServletContext();
      String path=sc.getRealPath("/img/"+Filename);
      //3.将文件和相关的流关联起来
      FileInputStream fis=new FileInputStream(path);

      //为了解决输入流已经被调用的情况
      response.reset();

      //为了解决文件名是中文的问题:使用DownLoadUtils工具类
      //获取User-Agent请求头得知浏览器种类
      String user_agent=request.getHeader("user-agent");
      Filename=DownLoadUtils.getFileName(user_agent, Filename);

      //4.设置响应头
      //4.1设置content-type
      String mimeType=sc.getMimeType(Filename);
      response.setHeader("content-type", mimeType);
      //4.2设置响应头打开方式:content-disposition为附件打开方式
      response.setHeader("content-disposition","attachment;filename="+Filename);
      //5将输入流的数据读出到输出流中
      ServletOutputStream sos=response.getOutputStream();
      byte[] buff=new byte[1024*8];
      int len=0;
      while((len=fis.read(buff))!=-1)
      {
      sos.write(buff,0,len);
      }
      fis.close();
      }

    /**
    • @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
      */
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      // TODO Auto-generated method stub
      doPost(request, response);
      }

}

这里只有一个getOutputStream没有getWrite为什么也报getWriter() has already been called for this response错误?

诗瑶1的主页 诗瑶1 | 初学一级 | 园豆:175
提问于:2020-01-30 19:07
< >
分享
最佳答案
1

response.getWriter().append("Served at: ").append(request.getContextPath());
ServletOutputStream sos=response.getOutputStream();
......第一条是debug吧....用日志输出不行么.....
你眼神要好点了...

收获园豆:5
风中的雪糕 | 菜鸟二级 |园豆:418 | 2020-01-30 21:21

哈哈忘记了,这是模板自带的

诗瑶1 | 园豆:175 (初学一级) | 2020-01-30 22:15
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册