1 changed files with 61 additions and 0 deletions
@ -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); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue