当前位置:   article > 正文

base64转MultipartFile避坑_java base64转multipartfile

java base64转multipartfile

需求:接收base64编码的文件,然后上传到文件服务器。

思路:首先将base64转为MultipartFile类型,然后通过feign调用将文件上传至服务器

代码如下:

  1. @RequestMapping(method = RequestMethod.POST, value = "/doc/file/uploadForAPI",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
  2. Response docUpload(@RequestPart("file") MultipartFile file,
  3. @RequestParam("appCode") String appCode,
  4. @RequestParam("fileProp") String fileProp,
  5. @RequestParam(value = "folderName",required = false) String folderName) throws Exception;

base64转成对应的MultipartFile,代码如下:

  1. package com.*.product.evidence.storage.model;
  2. import org.springframework.web.multipart.MultipartFile;
  3. import java.io.ByteArrayInputStream;
  4. import java.io.File;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.nio.charset.StandardCharsets;
  8. import java.util.Base64;
  9. /**
  10. * @author
  11. * @date 2022/12/06 16:40
  12. * @description 转换base64为文件流
  13. */
  14. public class Base64ToMultipartFile implements MultipartFile {
  15. private final byte[] fileContent;
  16. private final String name;
  17. private final String extension;
  18. private final String contentType;
  19. private final String originalFilename;
  20. /**
  21. * @param base64
  22. * @param dataUri 格式类似于: data:image/png;base64
  23. */
  24. public Base64ToMultipartFile(String base64, String dataUri, String name, String originalfilename) {
  25. this.fileContent = Base64.getDecoder().decode(base64.getBytes(StandardCharsets.UTF_8));
  26. this.extension = dataUri.split(";")[0].split("/")[1];
  27. this.contentType = dataUri.split(";")[0].split(":")[1];
  28. this.originalFilename = originalfilename;
  29. this.name = name;
  30. }
  31. /**
  32. * 【重要】必须与请求接收方参数名称一致,否则找不到参数
  33. * @return
  34. */
  35. @Override
  36. public String getName() {
  37. return this.name;
  38. }
  39. @Override
  40. public String getOriginalFilename() {
  41. return originalFilename;
  42. }
  43. @Override
  44. public String getContentType() {
  45. return contentType;
  46. }
  47. @Override
  48. public boolean isEmpty() {
  49. return fileContent == null || fileContent.length == 0;
  50. }
  51. @Override
  52. public long getSize() {
  53. return fileContent.length;
  54. }
  55. @Override
  56. public byte[] getBytes() throws IOException {
  57. return fileContent;
  58. }
  59. @Override
  60. public ByteArrayInputStream getInputStream() throws IOException {
  61. return new ByteArrayInputStream(fileContent);
  62. }
  63. @Override
  64. public void transferTo(File file) throws IOException, IllegalStateException {
  65. try (FileOutputStream fos = new FileOutputStream(file)) {
  66. fos.write(fileContent);
  67. }
  68. }
  69. }

调试后文件服务器报错提示找不到请求参数“file”,经过长时间的排查发现重写的Base64ToMultipartFile类的getName()方法必须返回和接收参数同名的值,即file,否则feign的接收方无法收到对应的参数。

对应的实例化方法:

        MultipartFile file = new Base64ToMultipartFile(data, dataUri, "file", entity.getFileName());

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/399292
推荐阅读
相关标签
  

闽ICP备14008679号