52 lines
1.6 KiB
Java
52 lines
1.6 KiB
Java
package com.yj.earth.common.util;
|
|
|
|
import org.springframework.http.MediaType;
|
|
import org.springframework.mock.web.MockMultipartFile;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.io.IOException;
|
|
|
|
public class FileCommonUtil {
|
|
|
|
/**
|
|
* 将本地文件转换为 MultipartFile
|
|
*/
|
|
public static MultipartFile convertToMultipartFile(File file) {
|
|
try (FileInputStream inputStream = new FileInputStream(file)) {
|
|
return new MockMultipartFile(
|
|
"files",
|
|
file.getName(),
|
|
MediaType.APPLICATION_OCTET_STREAM_VALUE,
|
|
inputStream
|
|
);
|
|
} catch (IOException e) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static MediaType getImageMediaType(String suffix) {
|
|
String lowerSuffix = suffix.toLowerCase();
|
|
switch (lowerSuffix) {
|
|
case "jpg":
|
|
case "jpeg":
|
|
return MediaType.IMAGE_JPEG;
|
|
case "glb":
|
|
return MediaType.valueOf("model/gltf-binary");
|
|
case "png":
|
|
return MediaType.IMAGE_PNG;
|
|
case "gif":
|
|
return MediaType.IMAGE_GIF;
|
|
case "bmp":
|
|
return MediaType.valueOf("image/bmp");
|
|
case "webp":
|
|
return MediaType.valueOf("image/webp");
|
|
case "svg":
|
|
return MediaType.valueOf("image/svg+xml");
|
|
default:
|
|
return MediaType.APPLICATION_OCTET_STREAM;
|
|
}
|
|
}
|
|
}
|