POM引入
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>batik-all</artifactId>
<version>1.13</version>
<type>pom</type>
</dependency>
Java代码
public static void svg2Png(String folderPath, String outPath) {
try {
//开始
File folder = new File(folderPath);
if (!folder.exists()) {
return;
}
File outFolder = new File(outPath);
if (!outFolder.exists()) {
outFolder.mkdirs();
}
for (File file : folder.listFiles()) {
if (!file.getName().endsWith(".svg")) {
continue;
}
File png = new File(outPath + File.separator + file.getName().replace(".svg", ".png"));
OutputStream out = Files.newOutputStream(png.toPath());
try {
out = new BufferedOutputStream(out);
Transcoder transcoder = new PNGTranscoder();
TranscoderInput input = new TranscoderInput(file.toURI().toString());
TranscoderOutput output = new TranscoderOutput(out);
transcoder.transcode(input, output);
} catch (Exception e) {
} finally {
out.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}