分享

图片显示方法-64位及直接显示

 樱花梦_张艺馨 2023-01-31 发布于山东

//图片直接显示   
public static void makeImg(String path,HttpServletResponse response) throws IOException {
        byte[] bytes = get64Img(path,response);
        response.setContentType("image/png");
        OutputStream output = response.getOutputStream();
        InputStream in = new ByteArrayInputStream(bytes);
        int len;
        byte[] buf = new byte[1024];
        while ((len = in.read(buf)) != -1) {
            output.write(buf, 0, len);
        }
        output.flush();
    }

//返回64位编码
    public static byte[]  get64Img(String path, HttpServletResponse response){
        InputStream fis = null;
        ByteArrayOutputStream outputStream = null;
        byte[] finalBytes =null ;
            try{
            fis = new FileInputStream(new File(path + "\\" + "1.png"));
            outputStream = new ByteArrayOutputStream();
            byte[] bytes = new byte[1024];
            int temp;
            while ((temp = fis.read(bytes)) != -1) {
                outputStream.write(bytes, 0, temp);
            }
            //转换后的byte[]
            finalBytes = outputStream.toByteArray();

            fis.close();
            outputStream.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
        return finalBytes;
    }

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多