代码
base64Url 不能有前缀data:image/jpeg;base64,
或者data:image/png;base64,
里面没用的字符要替换干净,比如像 \n、[“、”]、这些都是无用字符串,需要清除。
@Api("测试base64Url转存本地")
@RestController
@Slf4j
public class TestController {
@PostMapping("/test")
public void getBase64Url(@RequestBody String base64Url) {
String url = "/Users/super0/Desktop/1.png";
// String newUrl = "/home/op/project/file/leerhuo-business-server/1.png";
String newBase64Url = base64Url.replace("\\n", "").replace("[\"", "").replace("\"]", "");
log.info("新地址:" + newBase64Url);
BASE64Decoder decoder = new BASE64Decoder();
try {
// Base64解码
byte[] b = decoder.decodeBuffer(newBase64Url);
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {
b[i] += 256;
}
}
OutputStream out = new FileOutputStream(url);
out.write(b);
out.flush();
out.close();
} catch (Exception ignored) {
}
}
}