【发布时间】:2020-04-09 10:14:32
【问题描述】:
我目前正在处理一个 Spring Boot 项目,同时上传图像,该图像是在 resources\static\images 中创建的,但是当我尝试显示图像时它没有显示。刷新文件夹后,它会反映出来。
这是我的代码:
// 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>
【问题讨论】:
-
“刷新文件夹”是什么意思?刷新浏览器?在浏览器的网络选项卡中,您能否查看图片是否再次下载或是否从缓存中提供?
-
@GreyFairer 不,我的意思是刷新文件夹。在网络标签中,它显示
Failed to load resource: the server responded with a status of 404 ()。但是当我去 IDE 并刷新文件夹时,图像就会显示在浏览器中。 -
是的,2 分钟后我意识到真正的问题是您更新了 src/main/resources 而不是 target/classes(请参阅下面的答案)。解决了吗?
-
@GreyFairer 谢谢你的回答,但对我没有用。当我在资源外部和项目内部创建图像文件夹时,它对我有用。
标签: java spring spring-boot spring-mvc jsp