以png格式保存svg图片

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qfashly/article/details/79498701

转载请注明:https://blog.csdn.net/qfashly/article/details/79498701
最近由于业务需要,需要将前端查询的数据在浏览器上以图片的形式展示,然后回传到后台以文件形式存储在硬盘上。

/**
 * @Title: convertToPng
 * @Description: 
 * @param svgCode 前端传回的svg字符串
 * @param pngFilePath 输出文件名,如:D:\Myworkspace\svg.png
 * @throws IOException
 * @throws TranscoderException
 * @date 2018-3-9 下午3:51:32
 */
public static void convertToPng(String svgCode, String pngFilePath) throws IOException, TranscoderException {
    File file = new File(pngFilePath);
    FileOutputStream outputStream = null;
    try {
        file.createNewFile();
        outputStream = new FileOutputStream(file);
        byte[] bytes = svgCode.getBytes("UTF-8");
        PNGTranscoder t = new PNGTranscoder();
        TranscoderInput input = new TranscoderInput(new ByteArrayInputStream(bytes));
        TranscoderOutput output = new TranscoderOutput(outputStream);
        t.transcode(input, output);
        outputStream.flush();
    } finally {
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (IOException e) {
                logger.error("",e);
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qfashly/article/details/79498701