下载一个图片ban.jpg 别人的写法: public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); response.setCharacterEncoding("utf-8"); response.setHeader("Content-Disposition", "attachment; filename=ban.jpg"); String path=this.getServletContext().getRealPath("/image/ban.jpg"); FileInputStream fis=new FileInputStream(path); byte buff[]=new byte[1024]; int len=0; OutputStream os=response.getOutputStream(); while((len=fis.read(buff))>0){ os.write(buff, 0, len);} os.close(); fis.close(); } 请问为何不这样写: public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); response.setCharacterEncoding("utf-8"); response.setHeader("Content-Disposition", "attachment; filename=ban.jpg"); String path=this.getServletContext().getRealPath("/image/ban.jpg"); FileInputStream fis=new FileInputStream(path); byte buff[]=new byte[1024]; int len=0; OutputStream os=response.getOutputStream(); while((len=fis.read(buff))>0){ fis.read(buff);//这个地方 os.write(buff, 0, len); } os.close(); fis.close(); }
为何不写这句?
fis.read(buff);
Genment
相关分类