只需解码 base64 部分并将其保存在文件中(我假设该部分确实是 True Type 字体)。
如果您使用的是 Java 7:
String fontData = "font/truetype;charset=utf-8;base64,QUJDREU= ... ";
fontData = fontData.substring(fontData.indexOf("base64,") + 7);
File ttFile = new File("/path/to/your/file.ttf");
try (FileOutputStream output = new FileOutputStream(ttFile)) {
byte[] binary = javax.xml.bind.DatatypeConverter.parseBase64Binary(fontData);
output.write(binary);
} catch (IOException e) {
e.printStackTrace();
}
如果您使用的是 Java 6:
String fontData = "font/truetype;charset=utf-8;base64,QUJDREU= ... ";
fontData = fontData.substring(fontData.indexOf("base64,") + 7);
File ttFile = new File("/path/to/your/file.ttf");
try {
FileOutputStream output = null;
try {
output = new FileOutputStream(ttFile);
byte[] binary = javax.xml.bind.DatatypeConverter.parseBase64Binary(fontData);
output.write(binary);
} finally {
if(output != null) {
output.close();
}
}
} catch (IOException e) {
e.printStackTrace();
}
如果您使用的是旧版本,请下载并添加到您的类路径Apache Commons Codec,然后尝试以下操作:
String fontData = "font/truetype;charset=utf-8;base64,QUJDREU= ... ";
fontData = fontData.substring(fontData.indexOf("base64,") + 7);
File ttFile = new File("/path/to/your/file.ttf");
FileOutputStream output = null;
try {
try {
output = new FileOutputStream(ttFile);
byte[] binary = new org.apache.commons.codec.binary.Base64().decode(fontData);
output.write(binary);
} finally {
if(output != null) {
output.close();
}
}
} catch (IOException e) {
e.printStackTrace();
}