首页 > 代码库 > 【jsp】读取WebRoot下的图像文件

【jsp】读取WebRoot下的图像文件

package readPic;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class ReadPic extends HttpServlet{        /**     *      */    private static final long serialVersionUID = 1L;    public void doGet(HttpServletRequest req,HttpServletResponse resp)    throws IOException,ServletException{        resp.setHeader("content-type","image/jpeg");                InputStream in=this.getServletContext().getResourceAsStream("1.jpg");        //1.jpg 放在WebRoot根目录下                int len=0;        byte buffer[]=new byte[1024];                OutputStream out=resp.getOutputStream();        while( (len=in.read()) > 0){            out.write(buffer,0,len);        }    }}

 

【jsp】读取WebRoot下的图像文件