Comments (2)
问题在于,前端所上传的 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.
另外,由于前端可能发送相同文件名的图片,比如许多人上传的图片名为: 未命名.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
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from meethere-backend.