首页 > 技术文章 > 读取Linux上图片

weihuang6620 2018-10-09 09:20 原文

/**
     * 读取图片
     * @param path
     * @param response
     * @throws Exception
     */
    public static void showImg(String path,HttpServletResponse response) throws Exception{
        File file = new File(path);
        if (file.exists()) {
            FileInputStream fileIs= new FileInputStream(path);
            int i=fileIs.available(); //得到文件大小
            byte data[]=new byte[i];
            fileIs.read(data); //读数据
            //JSP禁止图片缓存
            response.setHeader( "Pragma", "no-cache" );
            response.addHeader( "Cache-Control", "must-revalidate" );
            response.addHeader( "Cache-Control", "no-cache" );
            response.addHeader( "Cache-Control", "no-store" );
            response.setDateHeader("Expires", 0);

            response.setHeader("Content-type", "image/png");
            OutputStream outStream=response.getOutputStream(); //得到向客户端输出二进制数据的对象
            outStream.write(data); //输出数据
            outStream.flush();
            outStream.close();
            fileIs.close();
        }

    }

  

推荐阅读