diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/file/Base64ToMultipartFile.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/file/Base64ToMultipartFile.java new file mode 100644 index 0000000..4f8b84b --- /dev/null +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/file/Base64ToMultipartFile.java @@ -0,0 +1,61 @@ +package com.ruoyi.common.utils.file; + +import org.springframework.web.multipart.MultipartFile; +import org.apache.commons.io.IOUtils; +import java.io.*; +import java.util.Base64; + +public class Base64ToMultipartFile implements MultipartFile { + + private final byte[] fileContent; + private final String fileName; + private final String contentType; + + public Base64ToMultipartFile(String base64, String fileName, String contentType) { + this.fileContent = Base64.getDecoder().decode(base64); + this.fileName = fileName; + this.contentType = contentType; + } + + @Override + public String getName() { + return fileName; + } + + @Override + public String getOriginalFilename() { + return fileName; + } + + @Override + public String getContentType() { + return contentType; + } + + @Override + public boolean isEmpty() { + return fileContent == null || fileContent.length == 0; + } + + @Override + public long getSize() { + return fileContent.length; + } + + @Override + public byte[] getBytes() throws IOException { + return fileContent; + } + + @Override + public InputStream getInputStream() throws IOException { + return new ByteArrayInputStream(fileContent); + } + + @Override + public void transferTo(File dest) throws IOException, IllegalStateException { + try (FileOutputStream fos = new FileOutputStream(dest)) { + fos.write(fileContent); + } + } +} \ No newline at end of file