simulation-backend/src/main/java/com/hivekion/file/FileUntils.java
2025-08-07 18:04:07 +08:00

43 lines
1.4 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.hivekion.file;
import com.hivekion.common.exception.BusinessException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
@Component
public class FileUntils {
@Value("${importFile.3D}")
private String uploadPath;
public String getFilePath(MultipartFile file) {
String fileName = file.getOriginalFilename();
String nowDate = new SimpleDateFormat("yyyyMMdd").format((new Date()));
String fileDir = uploadPath + "/" + nowDate + "/" + System.currentTimeMillis();
File dir = new File(fileDir);
if (!dir.exists()) {
boolean mFlag = dir.mkdirs();
if (!mFlag){
throw new BusinessException(500,"新建文件目录失败");
}
}
String newFileName = fileDir + "/" + fileName;
assert fileName != null;
if(!fileName.endsWith("ive")){
throw new BusinessException(500,"请选择正确的文件格式(.ive");
}
File newFile = new File(newFileName);
try {
file.transferTo(newFile);
} catch (
IOException e) {
e.printStackTrace();
}
return newFile.getPath();
}
}