|
@@ -10,6 +10,7 @@ import org.apache.commons.io.FilenameUtils;
|
|
|
import org.apache.commons.io.IOUtils;
|
|
|
import org.apache.commons.lang3.ArrayUtils;
|
|
|
import org.apache.tika.Tika;
|
|
|
+import org.springframework.mock.web.MockMultipartFile;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
import org.springframework.web.multipart.commons.CommonsMultipartFile;
|
|
|
|
|
@@ -18,9 +19,6 @@ import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.*;
|
|
|
import java.net.URLEncoder;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
-import java.nio.file.Files;
|
|
|
-import java.nio.file.Path;
|
|
|
-import java.nio.file.Paths;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
|
|
@@ -342,7 +340,7 @@ public class FileUtils {
|
|
|
* @throws Exception 异常
|
|
|
*/
|
|
|
public static MultipartFile convertFileToMultipartFile(File file) throws Exception {
|
|
|
- DiskFileItem fileItem = new DiskFileItem("file", "image/png", true, file.getName(), (int) file.length(), file.getParentFile());
|
|
|
+ DiskFileItem fileItem = new DiskFileItem("file", "image/jpg", true, file.getName(), (int) file.length(), file.getParentFile());
|
|
|
|
|
|
try (FileInputStream input = new FileInputStream(file); OutputStream os = fileItem.getOutputStream()) {
|
|
|
IOUtils.copy(input, os);
|
|
@@ -351,24 +349,19 @@ public class FileUtils {
|
|
|
return new CommonsMultipartFile(fileItem);
|
|
|
}
|
|
|
|
|
|
- // 将HEIC格式图片转换为JPG并返回MultipartFile
|
|
|
- public static MultipartFile convertToJpg(MultipartFile file) throws Exception {
|
|
|
- // 指定文件保存路径(例如:上传目录下的转换后文件名)
|
|
|
- String fileName = "converted_" + System.currentTimeMillis() + ".jpg";
|
|
|
- Path filePath = Paths.get("F:\\", fileName);
|
|
|
-
|
|
|
- // 使用thumbnailator进行图片转换
|
|
|
- InputStream inputStream = file.getInputStream();
|
|
|
- try {
|
|
|
- Thumbnails.of(inputStream)
|
|
|
- .outputFormat("jpg")
|
|
|
- .toFile(filePath.toFile()); // 转换为JPG并保存到指定路径
|
|
|
- } finally {
|
|
|
- // 确保流在处理完后关闭
|
|
|
- inputStream.close();
|
|
|
- }
|
|
|
- // 将临时文件转换为MultipartFile
|
|
|
- return convertFileToMultipartFile(filePath.toFile());
|
|
|
+ public static MultipartFile convertToJpg(MultipartFile file) throws IOException {
|
|
|
+ byte[] bytes = file.getBytes();
|
|
|
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
|
+ Thumbnails.of(new ByteArrayInputStream(bytes))
|
|
|
+ .scale(1)
|
|
|
+ .outputFormat("jpg")
|
|
|
+ .toOutputStream(outputStream);
|
|
|
+ return new MockMultipartFile(
|
|
|
+ file.getName(),
|
|
|
+ file.getOriginalFilename().replaceAll("\\.[^.]+$", ".jpg"),
|
|
|
+ "image/jpeg",
|
|
|
+ outputStream.toByteArray()
|
|
|
+ );
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -397,23 +390,4 @@ public class FileUtils {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- // 清理临时文件的方法
|
|
|
- public static void deleteTempFile(File tempFile) {
|
|
|
- if (tempFile.exists()) {
|
|
|
- boolean deleted = tempFile.delete();
|
|
|
- if (!deleted) {
|
|
|
- System.err.println("Failed to delete temporary file: " + tempFile.getAbsolutePath());
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public static void deleteTempFile(Path tempFilePath) {
|
|
|
- try {
|
|
|
- Files.delete(tempFilePath);
|
|
|
- } catch (IOException e) {
|
|
|
- System.err.println("Failed to delete temporary file: " + tempFilePath);
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
}
|