英文:
How to refresh a folder using java program in intellij / Eclipse? (Solved)
问题
我目前正在一个Spring Boot项目上工作,在上传图片时,图片会被创建在resources\static\images
目录下,但是当我尝试显示这个图片时,它没有展示出来。在我刷新文件夹之后,图片才会显示出来。以下是我的代码:
// 用于上传图片的方法。
public void uploadImage(MultipartFile file) {
byte[] bytes = new byte[0];
try {
bytes = file.getBytes();
} catch (IOException e) {
e.printStackTrace();
}
BufferedOutputStream stream = null;
try {
stream = new BufferedOutputStream(new FileOutputStream(new File(
"D:\\New Language\\Spring Boot Demo\\employee_details\\src\\main\\resources\\static\\images"
+ File.separator + file.getOriginalFilename())));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
stream.write(bytes);
stream.flush();
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
<!-- 用于显示图片的 JSP 代码。-->
<div class="card-header border-0">
<img src="/images/${emp.path}" alt="My Image">
</div>
英文:
I am currently working on a Spring Boot project while uploading images the image get created in resources\static\images
but when I'm trying to display the image it is not showing. After I refresh the folder it get reflected.
Here is my code:
// Method for uploading image.
public void uploadImage(MultipartFile file) {
byte[] bytes = new byte[0];
try {
bytes = file.getBytes();
} catch (IOException e) {
e.printStackTrace();
}
BufferedOutputStream stream = null;
try {
stream = new BufferedOutputStream(new FileOutputStream(new File(
"D:\\New Language\\Spring Boot Demo\\employee_details\\src\\main\\resources\\static\\images"
+ File.separator + file.getOriginalFilename())));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
stream.write(bytes);
stream.flush();
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// JSP Code For displaying image.
<div class="card-header border-0">
<img src="/images/${emp.path}" alt="My Image">
</div>
答案1
得分: 1
你不应该在源文件夹中进行更改。我不是100%确定,但我认为IntelliJ会将.../target/classes/
用作类路径,并在编译过程中将文件复制到那里。Spring Boot会加载类路径上找到的任何/static
文件夹。因此,您可以在那里覆盖文件,而不是在.../src/main/resources
下。在进行编译或执行mvn clean install
期间,这将有效。
另外,如果单独运行Spring Boot应用程序,资源将在一个jar文件内,因此不建议将其用作动态存储的好方法。
最好为动态存储创建一个单独的文件夹,并进行以下配置:
spring.resources.static-locations=classpath:/static/,file:/D:/...
当然,如果您在运行时更新该文件夹,它就不再是静态的了。还可以参考 https://www.baeldung.com/spring-mvc-static-resources。
英文:
You shouldn't change it in the source folder. I'm not 100% sure, but I think IntelliJ will use .../target/classes/
as classpath, and will copy files there during compile. Spring Boot will load any /static
folder it finds on the classpath.
So you could overwrite files there instead of under .../src/main/resources
. That will work until IntelliJ decides to overwrite them during a compile or doing a mvn clean install
.
Also, if you run the spring boot app standalone, the resources will be inside a jar file, so that's not a good idea to use as dynamic storage.
Better to create a separate folder for dynamic storage, and configure it as follows:
spring.resources.static-locations=classpath:/static/,file:/D:/...
Off course, if you update that folder at runtime, it's not really static anymore. Check also https://www.baeldung.com/spring-mvc-static-resources
答案2
得分: 0
所以最终在经过3天的努力后,我找到了解决这个问题的方法。
由于resources\static\images\
目录通常用于静态内容,并且在应用程序内部保存上传的(动态的)内容通常不是一个好主意。因此,我创建了一个位于resources
目录之外的文件夹,并将所有已上传的(动态的)图像文件放在其中。
以下是我是如何解决这个问题的。
创建一个新的类并尝试以下内容。
@Configuration
public class ResourceWebConfiguration implements WebMvcConfigurer {
@Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
registry.addResourceHandler("/images/**").addResourceLocations("file:images/");
}
}
英文:
So finally after 3 days I found the solution to this problem.
Since resources\static\images\
place is about static content and since it is in general a bad idea to save uploaded (dynamic) content inside your application. So I created a folder outside of resources
folder and put all the uploaded (dynamic) images files.
Here is how I solved this problem.
Create a new class and try this.
@Configuration
public class ResourceWebConfiguration implements WebMvcConfigurer {
@Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
registry.addResourceHandler("/images/**").addResourceLocations("file:images/");
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论