您的位置:首页 > 其它

以png格式保存svg图片

2018-03-09 15:58 246 查看
转载请注明: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);
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: