转载请注明:https://blog.youkuaiyun.com/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);
}
}
}
}