HttpServletResponse(打开图片)

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

 

posted @ 2018-07-06 11:23  淅沥沥丶下雨  阅读(292)  评论(0编辑  收藏  举报