Base64转MultipartFile
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;
public static MultipartFile base64ToMultipartFile (String s) {
MultipartFile image = null;
StringBuilder base64 = new StringBuilder("");
if (s != null && !"".equals(s)) {
base64.append(s);
String[] baseStrs = base64.toString().split(",");
BASE64Decoder decoder = new BASE64Decoder();
byte[] b = new byte[0];
try {
b = decoder.decodeBuffer(baseStrs[1]);
} catch (IOException e) {
e.printStackTrace();
}
for (int j = 0; j < b.length; ++j) {
if (b[j] < 0) {
b[j] += 256;
}
}
image = new BASE64DecodedMultipartFile(b, baseStrs[0]);
}
return image;
}
package com.hssmartcity.common.utils;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
/**
* @ClassName BASE64DecodedMultipartFile
* @Description TODO
* @Author syh
* @Date 2023/8/7 10:48
*/
public class BASE64DecodedMultipartFile implements MultipartFile {
private final byte[] imgContent;
private final String header;
public BASE64DecodedMultipartFile(byte[] imgContent, String header) {
this.imgContent = imgContent;
this.header = header.split(";")[0];
}
@Override
public String getName() {
return System.currentTimeMillis() + Math.random() + "." + header.split("/")[1];
}
@Override
public String getOriginalFilename() {
return System.currentTimeMillis() + (int) Math.random() * 10000 + "." + header.split("/")[1];
}
@Override
public String getContentType() {
return header.split(":")[1];
}
@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, IllegalStateException {
new FileOutputStream(dest).write(imgContent);
}
}
MultipartFile转Base64
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Encoder;
import java.io.IOException;
/**
* @ClassName FilesUtils
* @Description 文件工具类
* @Author syh
* @Date 2023/9/13 14:40
*/
public class FilesUtils {
/**
* 将MultipartFile 图片文件编码为base64
*
* @param file
* @return
* @throws Exception
*/
public static String generateBase64(MultipartFile file) {
if (file == null || file.isEmpty()) {
throw new RuntimeException("图片不能为空!");
}
String fileName = file.getOriginalFilename();
String fileType = fileName.substring(fileName.lastIndexOf("."));
String contentType = file.getContentType();
byte[] imageBytes = null;
String base64EncoderImg = "";
try {
imageBytes = file.getBytes();
BASE64Encoder base64Encoder = new BASE64Encoder();
/**
* 1.Java使用BASE64Encoder 需要添加图片头("data:" + contentType + ";base64,"),
* 其中contentType是文件的内容格式。
* 2.Java中在使用BASE64Enconder().encode()会出现字符串换行问题,这是因为RFC 822中规定,
* 每72个字符中加一个换行符号,这样会造成在使用base64字符串时出现问题,
* 所以我们在使用时要先用replaceAll("[\\s*\t\n\r]", "")解决换行的问题。
*/
base64EncoderImg = "data:" + contentType + ";base64," + base64Encoder.encode(imageBytes);
base64EncoderImg = base64EncoderImg.replaceAll("[\\s*\t\n\r]", "");
} catch (IOException e) {
e.printStackTrace();
}
return base64EncoderImg;
}
}