下載一個圖片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);
添加回答
舉報
0/150
提交
取消