1. Replace the IMG address
/**
* Replace img address
* @param str- foreground SVG string
* @return
/private String Transferimgpath ( HttpServletRequest request,string str) {
System.out.println (str);
String Requesturl = Request.getrequesturl (). toString ();
String RequestUri = Request.getrequesturi ();
String Httpurl = Requesturl.replace (RequestUri, "");
String PTN = "(? i) href=\" ([^\ "]*") \ "[^>]*>";
Pattern p = pattern.compile (PTN, pattern.dotall);
Matcher m = p.matcher (str);
list<string> list = new arraylist<string> ();
while (M.find ()) {
String Imgurl = m.group (1);
if (!imgurl.contains ("http") &&!list.contains (Imgurl)) {
str = str.replaceall (Imgurl,httpurl + "/ resources/myflow-min/"+ Imgurl);
}
List.add (Imgurl);
}
return str;
}
2. Convert an SVG string to png
/** * Converts SVG strings to PNG * * @param svgcode SVG code * @param pngfilepath saved path * @throws Transcodere Xception SVG Code Exception * @throws ioexception IO error/public static void Converttopng (String svgcode, String Pngfil
Epath) throws IOException, transcoderexception {File file = new file (pngFilePath);
FileOutputStream outputstream = null;
try {file.createnewfile ();
OutputStream = new FileOutputStream (file);
Converttopng (Svgcode, OutputStream);
finally {if (OutputStream!= null) {try {outputstream.close ();
catch (IOException e) {e.printstacktrace (); /** * Converts svgcode to PNG file, output directly to stream * * @param svgcode SVG code * @param OutputStream output Stream * @throws transcoderexception exception * @throws IOException IO Exception * */public static void Converttopng (String svgcode, OutputStream outputstream) throws Transcoderexception, IOException {
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 ();
finally {if (OutputStream!= null) {try {outputstream.close ();
catch (IOException e) {e.printstacktrace (); }
}
}
}
3. Jar Bag for use
Batik-all-1.7.jar
Xml-apis-ext.jar