Forked from luanvuhlu/BASE64DecodedMultipartFile.java
Created
March 26, 2020 01:13
-
-
Save hendisantika/857997ff262cfd56ed832f261a4bfc1a to your computer and use it in GitHub Desktop.
convert byte data to MultipartFile in Spring MVC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.ByteArrayInputStream; | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import org.apache.logging.log4j.LogManager; | |
import org.apache.logging.log4j.Logger; | |
import org.springframework.web.multipart.MultipartFile; | |
public class BASE64DecodedMultipartFile implements MultipartFile { | |
protected static final Logger log = LogManager.getLogger(BASE64DecodedMultipartFile.class); | |
private byte[] imgContent; | |
private String fileName; | |
private String ext; | |
public String getExt() { | |
return ext; | |
} | |
@Override | |
public String getName() { | |
return fileName; | |
} | |
@Override | |
public String getOriginalFilename() { | |
return fileName; | |
} | |
@Override | |
public String getContentType() { | |
if(getExt() == null) { | |
return null; | |
} | |
return MimeTypes.getMimeType(getExt()); | |
} | |
@Override | |
public boolean isEmpty() { | |
return imgContent == null || imgContent.length == 0; | |
} | |
@Override | |
public long getSize() { | |
return imgContent.length; | |
} | |
@Override | |
public byte[] getBytes() throws IOException { | |
return imgContent; | |
} | |
@Override | |
public InputStream getInputStream() throws IOException { | |
return new ByteArrayInputStream(imgContent); | |
} | |
@Override | |
public void transferTo(File dest) throws IOException { | |
try (FileOutputStream f = new FileOutputStream(dest)) { | |
f.write(imgContent); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment