如何在IntelliJ / Eclipse中使用Java程序刷新文件夹?(已解决)

huangapple go评论87阅读模式
英文:

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(
					&quot;D:\\New Language\\Spring Boot Demo\\employee_details\\src\\main\\resources\\static\\images&quot;
							+ 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.   
    &lt;div class=&quot;card-header border-0&quot;&gt;
        &lt;img src=&quot;/images/${emp.path}&quot; alt=&quot;My Image&quot;&gt;
    &lt;/div&gt;                                                   

答案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目录之外的文件夹,并将所有已上传的(动态的)图像文件放在其中。

如何在IntelliJ / Eclipse中使用Java程序刷新文件夹?(已解决)

以下是我是如何解决这个问题的。
创建一个新的类并尝试以下内容。

@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.

如何在IntelliJ / Eclipse中使用Java程序刷新文件夹?(已解决)

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(&quot;/images/**&quot;).addResourceLocations(&quot;file:images/&quot;);
    }
}

huangapple
  • 本文由 发表于 2020年4月9日 18:14:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/61118856.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定