🎉 新增文件工具

This commit is contained in:
fuhouyin 2023-02-01 14:33:22 +08:00
parent 1d364100c6
commit 6c8ae76267
2 changed files with 150 additions and 0 deletions

View File

@ -0,0 +1,86 @@
package utils;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;
import java.io.*;
/**
* @author fuhouyin
* @time 2023/1/30 11:21
*/
public class Base64ToMultipartFile implements MultipartFile {
private final byte[] imgContent;
private final String header;
public Base64ToMultipartFile(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);
}
/**
* base转为MultipartFile base64需要带着头
*/
public static MultipartFile base64ToMultipart(String base64) {
try {
String[] baseStrs = base64.split(",");
BASE64Decoder decoder = new BASE64Decoder();
byte[] b = new byte[0];
b = decoder.decodeBuffer(baseStrs[1]);
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {
b[i] += 256;
}
}
return new Base64ToMultipartFile(b, baseStrs[0]);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}

View File

@ -0,0 +1,64 @@
package utils;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;
/**
* @author fuhouyin
* @time 2023/2/1 11:55
*/
public class FileUtils {
/**
* 根据文件后缀名获取base64的文件头
* @param str 文件名后缀
*/
public static String suffix(String str){
String strSuffix = null;
switch (str) {
case "txt": strSuffix = "data:text/plain;base64,"; break;
case "doc": strSuffix = "data:application/msword;base64,";break;
case "docx": strSuffix = "data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64,";break;
case "xls": strSuffix = "data:application/vnd.ms-excel;base64,";break;
case "xlsx": strSuffix = "data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,";break;
case "pdf": strSuffix = "data:application/pdf;base64,";break;
case "pptx": strSuffix = "data:application/vnd.openxmlformats-officedocument.presentationml.presentation;base64,";break;
case "ppt": strSuffix = "data:application/vnd.ms-powerpoint;base64,";break;
case "png": strSuffix = "data:image/png;base64,";break;
case "jpg": strSuffix = "data:image/jpeg;base64,";break;
case "gif": strSuffix = "data:image/gif;base64,";break;
case "svg": strSuffix = "data:image/svg+xml;base64,";break;
case "ico": strSuffix = "data:image/x-icon;base64,";break;
case "bmp": strSuffix = "data:image/bmp;base64,";break;
}
return strSuffix;
}
/**
* 文件转base64
* @param path 文件地址
*/
public static String fileToBase64(String path) throws Exception {
byte[] b = Files.readAllBytes(Paths.get(path));
String strSuffix = suffix(path.substring(path.lastIndexOf(".") + 1));
String base64 = Base64.getEncoder().encodeToString(b);
return strSuffix + base64;
}
/**
* 保存文件
* @param path 文件地址
* @param multipartFile 文件
* @throws Exception
*/
public static void saveFile(String path, MultipartFile multipartFile) throws Exception {
File dest = new File(path);
assert multipartFile != null;
multipartFile.transferTo(dest);
}
}