GithubHelp home page GithubHelp logo

Comments (2)

jjaychen1e avatar jjaychen1e commented on August 17, 2024

问题在于,前端所上传的 Base64 数据前带一串用于描述数据类型的字符串:data:image/png;base64,/9j/4AAQSkZJRgA....,它不应该被包含在待处理的 Base64 流中,因此我们需要手动处理,并且我们可以从其中获取到文件类型:

String partSeparator = ",";
if (imgStr.contains(partSeparator)) {
    String encodedImg = imgStr.split(partSeparator)[1];
    String fileType = imgStr.split(";")[0].split("/")[1];
    byte[] decodedImg = Base64.getDecoder().decode(encodedImg.getBytes(StandardCharsets.UTF_8));
}

from meethere-backend.

jjaychen1e avatar jjaychen1e commented on August 17, 2024

另外,由于前端可能发送相同文件名的图片,比如许多人上传的图片名为: 未命名.png, 1.png 等,因此我们将文件名随机化后保存:

String random = String.valueOf(imgStr.hashCode());
int length = 20 < random.length() ? 20 : random.length();
String filename = String.valueOf(imgStr.hashCode()).substring(1, length);

from meethere-backend.

Related Issues (6)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.