你好,我的 Spring Boot Java 代码有问题。我将图像上传到我的文件夹(src/main/resources/static/upload-dir/),并将其路径保存在数据库中,以便能够在我的角度前面显示它。一切都很好,但是为了能够看到我的图像,我必须重新启动 inteliJ。我认为这是因为我的上传文件夹是静态的。如果我取出上传文件夹并将其放入(“src/main/resources/upload-dir/”),上传仍然有效,但我无法再显示我的图像。
谁有解决办法吗?
//我的用于将文件保存在文件夹中的java代码
private String saveFile(MultipartFile file) throws Exception {
try {
// Obtenir le chemin du dossier des ressources
// ("src/main/resources/static/upload-dir/");
Path uploadPath = Paths.get ("src/main/resources/upload-dir/");
String fileName = System.currentTimeMillis() + "_" + file.getOriginalFilename();
// Copier le fichier vers le dossier des ressources
Path filePath = uploadPath.resolve(fileName);
Files.copy(file.getInputStream(), filePath, StandardCopyOption.REPLACE_EXISTING);
return filePath.toString();
} catch (Exception e) {
throw new Exception("Erreur lors de l'enregistrement du fichier", e);
}
}
//我的用于翻译图像路径的 Angular 组件代码
getFullImagePath(dvd:any): string {
if(dvd && dvd.id)
{
if(dvd.picturePath)
{
const fullPath = `http://localhost:80${dvd.picturePath.replace('src/main/resources','')}`;
return fullPath;
}
}
return "";
}
//我的显示html代码
<div class="w-1/2 mx-auto justify-center flex overflow-hidden rounded-xl">
<a href="/newdvd/?filter={{dvds.id}}">
<img *ngIf="dvds" class="object-cover w-30 lg:w-40" src="{{getFullImagePath(dvds)}}" alt="image jacket"></a>
</div>
您不应在应用程序中引用源 (src/...) 目录。当应用程序真正部署在IDE之外时,它就不会存在。在服务器中使用单独的目录。