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