spring-data-jpa 从base64获取文件扩展名用Java编码

9o685dep  于 2022-11-10  发布在  Spring
关注(0)|答案(1)|浏览(129)

假设这个文件类型是sharath.zip。假设我把sharath.zip编码成了Base64字符串。当我把base64String给我的朋友时。我的朋友应该如何从base64String中得到sharath.zip作为文件名。
base64编码的字符串是“5K2Q0IMKAADrq7bllYsAAAAAAAAAAAAAAOaNgealtOa5r+一个MruStkMiBHwoAAOurtuWViwAAAAAAAAAAAAgkAAAAAAAAAAAAAAOaNgealtOa5r+一个MrgogAAABGOSRgeajgO22mseY5JGB5qOA7baax5jkkYHmo4DttprHmOStkNiFAAABAVoAJgAA”

kxxlusnw

kxxlusnw1#

我认为对于解码,你可以尝试这样的东西,

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){

    }

相关问题