人员下发修改
This commit is contained in:
parent
c90848855e
commit
373ccb38c0
|
|
@ -34,7 +34,7 @@ public class MongoConfig {
|
|||
@Value("${spring.data.mongodb.username}")
|
||||
private String UN ;
|
||||
|
||||
@Value("${spring.data.mongodb.password}")
|
||||
@Value(".${spring.data.mongodb.password}")
|
||||
private String PD ;
|
||||
|
||||
private MongoDatabase mongoDatabase;
|
||||
|
|
|
|||
|
|
@ -188,27 +188,59 @@ public class IntelligentLibraryUtil {
|
|||
}
|
||||
}
|
||||
|
||||
public static File[] convertUrlToFile(String imageUrl) throws Exception {
|
||||
// 打开URL输入流
|
||||
try (InputStream in = new URL(imageUrl).openStream()) {
|
||||
// 创建临时文件(保留原始扩展名)
|
||||
String suffix = getExtensionFromUrl(imageUrl);
|
||||
public static File[] convertUrlToFile(String imageUrlOrPath) throws Exception {
|
||||
// 判断是本地路径还是URL
|
||||
if (isLocalFile(imageUrlOrPath)) {
|
||||
// 如果是本地路径,创建一个临时文件复制该文件内容
|
||||
File originalFile = new File(imageUrlOrPath);
|
||||
String suffix = getExtensionFromFilePath(imageUrlOrPath);
|
||||
File tempFile = File.createTempFile("image-", suffix);
|
||||
tempFile.deleteOnExit(); // 程序退出时自动删除
|
||||
|
||||
// 写入到临时文件
|
||||
try (FileOutputStream out = new FileOutputStream(tempFile)) {
|
||||
try (FileInputStream in = new FileInputStream(originalFile);
|
||||
FileOutputStream out = new FileOutputStream(tempFile)) {
|
||||
byte[] buffer = new byte[1024];
|
||||
int bytesRead;
|
||||
while ((bytesRead = in.read(buffer)) != -1) {
|
||||
out.write(buffer, 0, bytesRead);
|
||||
}
|
||||
}
|
||||
|
||||
return new File[]{tempFile};
|
||||
} else {
|
||||
// 打开URL输入流
|
||||
try (InputStream in = new URL(imageUrlOrPath).openStream()) {
|
||||
// 创建临时文件(保留原始扩展名)
|
||||
String suffix = getExtensionFromUrl(imageUrlOrPath);
|
||||
File tempFile = File.createTempFile("image-", suffix);
|
||||
tempFile.deleteOnExit(); // 程序退出时自动删除
|
||||
|
||||
// 写入到临时文件
|
||||
try (FileOutputStream out = new FileOutputStream(tempFile)) {
|
||||
byte[] buffer = new byte[1024];
|
||||
int bytesRead;
|
||||
while ((bytesRead = in.read(buffer)) != -1) {
|
||||
out.write(buffer, 0, bytesRead);
|
||||
}
|
||||
}
|
||||
|
||||
return new File[]{tempFile};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 从文件路径中提取后缀名
|
||||
private static String getExtensionFromFilePath(String filePath) {
|
||||
String fileName = new File(filePath).getName();
|
||||
int dotIndex = fileName.lastIndexOf('.');
|
||||
return (dotIndex == -1) ? "" : fileName.substring(dotIndex);
|
||||
}
|
||||
|
||||
|
||||
private static boolean isLocalFile(String path) {
|
||||
File file = new File(path);
|
||||
return file.exists() && file.isFile();
|
||||
}
|
||||
|
||||
// 从URL中提取后缀名,比如 ".jpg", ".png"
|
||||
private static String getExtensionFromUrl(String url) {
|
||||
String lowerCaseUrl = url.toLowerCase();
|
||||
|
|
|
|||
Loading…
Reference in New Issue