public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 使用response获取字节输出流 ServletOutputStream out = response.getOutputStream(); // 确定定数据源,将文件读到程序中 String realpath = this.getServletContext().getRealPath("dameinv.jpg"); System.out.println(realpath); InputStream in = new FileInputStream(realpath); int len = 0; byte[] b = new byte[1024]; while ((len = in.read(b)) != -1) { // 将字节数组写给客户端 out.write(b, 0, len); } // 释放资源 in.close(); }