java后台实现svg字符串转换为png

 //使用的jar包

batik-all-1.7.jar
xml-apis-ext.jar

    



    /**
	 * @param svgCode为svg字符串
	 * @param pngFilePath为png的导出地址
	 */
	public static void convertToPng(String svgCode, String pngFilePath) {

        File file = new File(pngFilePath);

        FileOutputStream outputStream = null;
        try {
            file.createNewFile();
            outputStream = new FileOutputStream(file);
            convertToPng(svgCode, outputStream);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
	
	
	/**
	 * @param svgCode为svg字符串
	 * @param outputStream
	 **/
    public static void convertToPng(String svgCode, OutputStream outputStream) {
        try {
            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();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

//附上jar包

链接:https://pan.baidu.com/s/1E9yYXy6L-EOHls5c_SlgbQ       提取码:lbvf (失效请联系博主)

常用jar包下载地址(可收藏)http://www.java2s.com/

(以上代码出错请联系博主,一起探讨,thanks)

你可能感兴趣的:(问题解决,答案)