【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);
        }
    }
}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。