如何在java中删除附加到BASE64字符串的基本信息。我收到一个元素包含BASE64图片的xml,但是在字符串的开头我得到了以下内容:
<![CDATA[Content-type: image/jpeg
Content-transfer-encoding: base64
begin-base64 644 Billede installation_2818.jpg
并且在字符串的末尾我有关闭路径:
]]>
我试图将conent变成java String类型并使用一些正则表达式调用replace,如
".*.jpg"
但是不起作用,我认为这是因为图片太大了,但我不确定,我的想法已经用完了。我甚至做了一个像这样的小测试程序:
public class Base64RegExExample {
public static void main(String[] args) {
// TODO Auto-generated method stub
String xml = "<![CDATA[Content-type: image/jpeg Content-transfer-encoding: base-64begin-base64 644 Billede installation_2818.jpg/9j/4QtCRXhpZgAASUkqAAgAAAAMAAABBAABAAAAIAoAAAEBBAABAAAAmAcAAA8BAgAIAAAAngAAABABAgAJAAAApgAAABIBAwABAAAAAQAAABoBBQABAAAAsAAAABsBBQABAAAAuAAAACgBAwABAAAAAgAAADEBAgAOAAAAwAAAADIBAgAUAAAAzgAAABMCAwABAAAAAQAAAGmHBAABAAAA4gAAAHwDAABTQU1TVU5HAFNNLUczODhGAABIAAAAAQAAAEgAAAABAAAARzM4OEZYWFUxQU9IMwAyMDE1OjExOjE4IDEyOjI4OjIzACIAmoIFAAEAAACAAgAAnYIFAAEAAACIAgAAIogDAAEAAAACAAAAJ4gDAAEAAAC0AAAAAJAHAAQAAAAwMjIwA5ACABQAAACQAgAABJACABQAAACkAgAAAZEHAAQAAAABAgMAAZIKAAEAAAC4AgAAApIFAAEAAADAAgAAA5IKAAEAAADIAgAABJIKAAEAAADQAgAABZIFAAEAAADYAgAAB5IDAAEAAAACAAAACJIDAAEAAAAAAAAACZIDAAEAAAAAAAAACpIFAAEAAADgAgAAfJIHAG4AAADoAgAAAKAHAAQAAAAwMTAwAaADAAEAAAD]]>";
xml = xml.replaceFirst(".*\\.jpg","").replaceFirst("]]>", "");
xml = xml.replaceFirst("]]>", "");
/*byte[] bytes = javax.xml.bind.DatatypeConverter.parseBase64Binary(imageData);
System.out.println(new String(bytes));*/
}
}
只是为了测试正则表达式,并且我可以删除字符串中不需要的部分。可原因是在原始字符串中有一些我必须在正则表达式中考虑的新行吗?
如果我得到以下输入:
<![CDATA[Content-type: image/jpeg
Content-transfer-encoding: base64
begin-base64 644 Billede installation_2818.jpg
/9j/4QtCRXhpZgAASUkqAAgAAAAMAAABBAABAAAAIAoAAAEBBAABAAAAmAcAAA8BAgAIAAAAngAA
ABABAgAJAAAApgAAABIBAwABAAAAAQAAABoBBQABAAAAsAAAABsBBQABAAAAuAAAACgBAwABAAAA
AgAAADEBAgAOAAAAwAAAADIBAgAUAAAAzgAAABMCAwABAAAAAQAAAGmHBAABAAAA4gAAAHwDAABT
QU1TVU5HAFNNLUczODhGAABIAAAAAQAAAEgAAAABAAAARzM4OEZYWFUxQU9IMwAyMDE1OjExOjE4
IDEyOjI4OjIzACIAmoIFAAEAAACAAgAAnYIFAAEAAACIAgAAIogDAAEAAAACAAAAJ4gDAAEAAAC0
AAAAAJAHAAQAAAAwMjIwA5ACABQAAACQAgAABJACABQAAACkAgAAAZEHAAQAAAABAgMAAZIKAAEA...]]>
我想结束这部分输入:
/9j/4QtCRXhpZgAASUkqAAgAAAAMAAABBAABAAAAIAoAAAEBBAABAAAAmAcAAA8BAgAIAAAAngAA
ABABAgAJAAAApgAAABIBAwABAAAAAQAAABoBBQABAAAAsAAAABsBBQABAAAAuAAAACgBAwABAAAA
AgAAADEBAgAOAAAAwAAAADIBAgAUAAAAzgAAABMCAwABAAAAAQAAAGmHBAABAAAA4gAAAHwDAABT
QU1TVU5HAFNNLUczODhGAABIAAAAAQAAAEgAAAABAAAARzM4OEZYWFUxQU9IMwAyMDE1OjExOjE4
IDEyOjI4OjIzACIAmoIFAAEAAACAAgAAnYIFAAEAAACIAgAAIogDAAEAAAACAAAAJ4gDAAEAAAC0
AAAAAJAHAAQAAAAwMjIwA5ACABQAAACQAgAABJACABQAAACkAgAAAZEHAAQAAAABAgMAAZIKAAEA...