代码

base64Url 不能有前缀data:image/jpeg;base64,或者data:image/png;base64,
里面没用的字符要替换干净,比如像 \n、[“、”]、这些都是无用字符串,需要清除。

  1. @Api("测试base64Url转存本地")
  2. @RestController
  3. @Slf4j
  4. public class TestController {
  5. @PostMapping("/test")
  6. public void getBase64Url(@RequestBody String base64Url) {
  7. String url = "/Users/super0/Desktop/1.png";
  8. // String newUrl = "/home/op/project/file/leerhuo-business-server/1.png";
  9. String newBase64Url = base64Url.replace("\\n", "").replace("[\"", "").replace("\"]", "");
  10. log.info("新地址:" + newBase64Url);
  11. BASE64Decoder decoder = new BASE64Decoder();
  12. try {
  13. // Base64解码
  14. byte[] b = decoder.decodeBuffer(newBase64Url);
  15. for (int i = 0; i < b.length; ++i) {
  16. if (b[i] < 0) {
  17. b[i] += 256;
  18. }
  19. }
  20. OutputStream out = new FileOutputStream(url);
  21. out.write(b);
  22. out.flush();
  23. out.close();
  24. } catch (Exception ignored) {
  25. }
  26. }
  27. }