package com.zyz; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; /** * Created by Administrator on 16-9-29. */ public class DownImgServlet extends HelloServlet { @Override public void doGet(HttpServletRequest request, HttpServletResponse response) { response.setHeader("Content-disposition","attachment;filename=photo.jpg"); InputStream in=this.getServletContext().getResourceAsStream("/images/i.jpeg"); byte buffer[]=new byte[1024];//设置缓冲区 int len=0; try { OutputStream out=response.getOutputStream(); while((len=in.read(buffer))>0){ out.write(buffer,0,len); } } catch (IOException e) { e.printStackTrace(); } } }