通过浏览器文件下载问题

写一个通过浏览器下载服务器共享文件的功能,out.write()后,文件传入前端浏览器了,但没有下载到本地,也没有弹出下载框,怎么解决呢?

是否设置响应头格式 是否刷新流 是否关闭流:


public class ResponseFileServlet extends HttpServlet {
 
   public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
 
      downfile(response);
 
   }
 
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
 
      downfile(response);
 
   }
 
   private void downfile(HttpServletResponse response) throws IOException {
 
   //获取下载的文件路径(注意获取这里获取的是绝对路径,先获取ServletContext再使用ServletContext的getRealPath方法获取绝对路径)
 
   String path = this.getServletContext().getRealPath("/Resourse/Student.xml");
 
   //设置响应头控制浏览器以下载的形式打开文件
 
   response.setHeader("content-disposition",
 
"attachment;fileName="+"Student.xml");
 
   InputStream in = new FileInputStream(path); //获取下载文件的输入流
 
   int count =0;
 
   byte[] by = new byte[1024];
 
   //通过response对象获取OutputStream流
 
    OutputStream out=  response.getOutputStream();
 
   while((count=in.read(by))!=-1){
 
      out.write(by, 0, count);//将缓冲区的数据输出到浏览器
 
   }
 
   in.close();
 
   out.flush();
 
   out.close();}}