下面这段代码为什么不能实现文件下载,而是在页面上直接显示? package cn.itcast.ResPonse; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class PictureDownload extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String path=this.getServletContext().getRealPath("/download/9.jpg"); String filename=path.substring(path.lastIndexOf("\\")+1); // System.out.println(filename); response.setHeader("content-disposition", "attachement;filename="+filename); InputStream in=null; OutputStream out=null; byte [] by=new byte[1024]; int count=0; in=new FileInputStream(path); out=response.getOutputStream(); while((count=in.read(by))>0){ out.write(by, 0, count); } in.close(); out.close(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } } |