din*_*707 12 java image mime-types
我得到一个文件,它是Base64编码的字符串作为图像。但我认为它的内容包含有关 png、jpeg 等文件类型的信息。我该如何检测?有什么图书馆可以帮助我吗?
如果你想获得 Mime 类型,请使用这个
const body = {profilepic:"data:image/png;base64,abcdefghijklmnopqrstuvwxyz0123456789"};
let mimeType = body.profilepic.match(/[^:]\w+\/[\w-+\d.]+(?=;|,)/)[0];
Run Code Online (Sandbox Code Playgroud)
============================================
如果您只想获取类型它像(png,jpg)等
const body2 = {profilepic:"data:image/png;base64,abcdefghijklmnopqrstuvwxyz0123456789"};
let mimeType2 = body2.profilepic.match(/[^:/]\w+(?=;|,)/)[0];
Run Code Online (Sandbox Code Playgroud)
我已经使用解决了我的问题mimeType = URLConnection.guessContentTypeFromStream(inputstream);
{ //Decode the Base64 encoded string into byte array
// tokenize the data since the 64 encoded data look like this "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAoAAAAKAC"
String delims="[,]";
String[] parts = base64ImageString.split(delims);
String imageString = parts[1];
byte[] imageByteArray = Base64.decode(imageString );
InputStream is = new ByteArrayInputStream(imageByteArray);
//Find out image type
String mimeType = null;
String fileExtension = null;
try {
mimeType = URLConnection.guessContentTypeFromStream(is); //mimeType is something like "image/jpeg"
String delimiter="[/]";
String[] tokens = mimeType.split(delimiter);
fileExtension = tokens[1];
} catch (IOException ioException){
}
}
Run Code Online (Sandbox Code Playgroud)