web项目流显示图片

来源:互联网 发布:美工工资 编辑:程序博客网 时间:2024/06/05 22:29



 /**
     * 读取文件并base64
     */
    String readFileAsImage(String locationFilePath) throws Exception {
        InputStream in = null;
        try {
            File file = new File(locationFilePath);
            in = new FileInputStream(file);
            byte[] data = new byte[in.available()];
            in.read(data);

            //String fileContent = FileUtils.readFileToString(file,Charsets.UTF_8);

            BASE64Encoder encoder = new BASE64Encoder();
            return "dataMap:image/png;base64," + encoder.encode(data);

        } catch (Exception e) {
            logger.error("读取文件并base64异常", e);
        } finally {
            if (in != null) {
                in.close();
            }
        }
        return null;
    }

import sun.misc.BASE64Encoder;



0 0