如何通过Base64编码的图像字符串识别文件类型

时间:2014-09-10 10:52:06

标签: java image mime-types

我得到一个Base64编码字符串的文件作为图像。但我认为其内容包含有关文件类型的信息,如png,jpeg等。如何检测?有没有可以帮助我的图书馆?

6 个答案:

答案 0 :(得分:3)

我使用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){

    }
}

答案 1 :(得分:0)

/**
 * Extract the MIME type from a base64 string
 * @param encoded Base64 string
 * @return MIME type string
 */
private static String extractMimeType(final String encoded) {
    final Pattern mime = Pattern.compile("^data:([a-zA-Z0-9]+/[a-zA-Z0-9]+).*,.*");
    final Matcher matcher = mime.matcher(encoded);
    if (!matcher.find())
        return "";
    return matcher.group(1).toLowerCase();
}

用法:

final String encoded = "data:image/png;base64,iVBORw0KGgoAA...5CYII=";
extractMimeType(encoded); // "image/png"
extractMimeType("garbage"); // ""

答案 2 :(得分:0)

如果要获取Mime类型,请使用此

const body = {profilepic:"data:image/png;base64,abcdefghijklmnopqrstuvwxyz0123456789"};
let mimeType = body.profilepic.match(/[^:]\w+\/[\w-+\d.]+(?=;|,)/)[0];

online Demo here

=========================================

如果您只想获取它的类型,例如(png,jpg)等

const body2 = {profilepic:"data:image/png;base64,abcdefghijklmnopqrstuvwxyz0123456789"};
let mimeType2 = body2.profilepic.match(/[^:/]\w+(?=;|,)/)[0];

online Demo here

答案 3 :(得分:0)

您可以像这样检查:

String[] strings = base64String.split(",");
String extension;
switch (strings[0]) {//check image's extension
    case "data:image/jpeg;base64":
        extension = "jpeg";
        break;
    case "data:image/png;base64":
        extension = "png";
        break;
    default://should write cases for more images types
        extension = "jpg";
        break;
}

答案 4 :(得分:-1)

我这样做了: (文件是我的base64字符串)

    int extentionStartIndex = file.indexOf('/');
    int extensionEndIndex = file.indexOf(';');
    int filetypeStartIndex = file.indexOf(':');

    String fileType = file.substring(filetypeStartIndex + 1, extentionStartIndex);
    String fileExtension = file.substring(extentionStartIndex + 1, extensionEndIndex);

    System.out.println("fileType : " + fileType);
    System.out.println("file Extension :" + fileExtension);

答案 5 :(得分:-2)

此代码使用正则表达式模式从Base64字符串中提取mime类型。虽然它是用JavaScript编写的,但您可以尝试在Java中实现相同的功能。

function base64Mime(encoded) {
  var result = null;

  if (typeof encoded !== 'string') {
    return result;
  }

  var mime = encoded.match(/data:([a-zA-Z0-9]+\/[a-zA-Z0-9-.+]+).*,.*/);

  if (mime && mime.length) {
    result = mime[1];
  }

  return result;
}

用法:

var encoded = 'data:image/png;base64,iVBORw0KGgoAA...5CYII=';

console.log(base64Mime(encoded)); // "image/png"
console.log(base64Mime('garbage')); // null

来源:miguelmota/base64mime (GitHub)