传统方法
把文件输入流转成文件输出流,直接在web.xml中请求
<img src="images/a.jpg" />
访问控制器
@WebServlet("/demo")
public class DemoServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req,
HttpServletResponse resp) throws ServletException, IOException {
//输出文字流
// PrintWriter out = resp.getWriter();
//获取响应流
ServletOutputStream os = resp.getOutputStream();
InputStream is = new FileInputStream(new
File(getServletContext().getRealPath("images"),"a.png"));
int index = -1;
while((index=is.read())!=-1){
os.write(index);
}
}
}
而是
<img src="demo" />
综上:访问静态资源和访问控制器得到的结果是一样的