上传的图片仅在刷新页面后可用
当我上传图片时,文件保存成功,路径设置成功。但表单提交后上传的图像并没有立即显示。仅当我重新加载页面时,才会显示上传的图像。
我保存上传的文件如下:
InputStream is;
try {
File file = new File("C:\\****\\*****\\Documents\\NetBeansProjects\\EventsCalendary\\web\\resources\\images\\uploadPhoto.png");
is = event.getFile().getInputstream();
OutputStream os = new FileOutputStream(file);
setUserPhoto("\\EventsCalendary\\resources\\images\\"+file.getName());
byte buf[] = new byte[1024];
int len;
while ((len = is.read(buf)) > 0) {
os.write(buf, 0, len);
}
os.close();
is.close();
} catch (IOException ex) {
System.out.println(ex.getStackTrace());
}
为什么上传的图像只有在重新加载页面后才显示?我该如何解决这个问题?
When I upload a picture, the file is successfully saved and the path is successfully set. But the uploaded image is not displayed immediately after the form submit. Only when I reload the page, the uploaded image is displayed.
I'm saving the uploaded file as below:
InputStream is;
try {
File file = new File("C:\\****\\*****\\Documents\\NetBeansProjects\\EventsCalendary\\web\\resources\\images\\uploadPhoto.png");
is = event.getFile().getInputstream();
OutputStream os = new FileOutputStream(file);
setUserPhoto("\\EventsCalendary\\resources\\images\\"+file.getName());
byte buf[] = new byte[1024];
int len;
while ((len = is.read(buf)) > 0) {
os.write(buf, 0, len);
}
os.close();
is.close();
} catch (IOException ex) {
System.out.println(ex.getStackTrace());
}
Why is the uploaded image only displayed after reloading the page and how can I solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将文件直接写入 IDE 的项目文件夹,并且您的意图似乎是将文件保存在 web 应用程序的部署文件夹中。这是一个坏主意,但由于以下 3 个主要原因:
IDE 项目文件夹中的更改不会立即反映在服务器的工作文件夹中。 IDE 中有一种后台作业,负责将服务器的工作文件夹与最新更新同步(这在 IDE 术语中称为“发布”)。这是您所看到的问题的主要原因。
在现实世界的代码中,有些情况下将上传的文件存储在 web 应用程序的部署文件夹中根本不起作用。某些服务器(默认情况下或通过配置)不会将部署的 WAR 文件扩展至本地磁盘文件系统,而是完全扩展至内存中。如果不编辑已部署的 WAR 文件并重新部署它,就无法在内存中创建新文件。
即使服务器将部署的 WAR 文件扩展到本地磁盘文件系统,所有新创建的文件在重新部署甚至简单的重新启动时都会丢失,因为这些新文件不是原始 WAR 文件的一部分。< /p>
您需要将其写入项目/部署文件夹之外的固定路径。例如,
/var/webapp/uploads
。然后,要让它由您的 Web 应用程序提供服务,只需将其作为新的 Web 应用程序上下文添加到服务器即可。根据您之前的问题,我知道您正在使用 Glassfish 3.1。在这个服务器中,它被称为“虚拟主机”。您可以在管理控制台中的服务器级别配置它 http://localhost:4848 >配置> HTTP服务>虚拟服务器,或者在 Web 应用程序级别,通过将以下行添加到
/WEB-INF/glassfish-web.xml
(您的 IDE 应该自动生成一个文件;请注意,该文件位于 Glassfish 之前) 3.1 称为sun-web.xml
,所以如果您看到手册/博客/教程引用它,是的,它是完全相同的文件):无论哪种方式,您都应该能够使用 http://localhost:8080/contextname/uploads/* 提供
< 上传的图像;img>
通常的方式。另请参阅:
You're writing the file straight into the IDE's project folder and your intent seems to save the file in the webapp's deploy folder. This is a bad idea and well due to the following 3 main reasons:
Changes in the IDE's project folder does not immediately get reflected in the server's work folder. There's kind of a background job in the IDE which takes care that the server's work folder get synced with last updates (this is in IDE terms called "publishing"). This is the main cause of the problem you're seeing.
In real world code there are circumstances where storing uploaded files in the webapp's deploy folder will not work at all. Some servers do (either by default or by configuration) not expand the deployed WAR file into the local disk file system, but instead fully in the memory. You can't create new files in the memory without basically editing the deployed WAR file and redeploying it.
Even when the server expands the deployed WAR file into the local disk file system, all newly created files will get lost on a redeploy or even a simple restart, simply because those new files are not part of the original WAR file.
You need to write it to a fixed path outside the project/deploy folder instead. For example,
/var/webapp/uploads
. Then, to get it to be served by your webapp, just add it as a new web application context to the server.Based on your previous question, I know that you're using Glassfish 3.1. In this server, it's called a "virtual host". You can configure it at server level in the admin console at http://localhost:4848 > Configuration > HTTP Service > Virtual Servers, or at webapp level by adding the following line to the
/WEB-INF/glassfish-web.xml
(your IDE should have autogenerated one; note that this file is before Glassfish 3.1 calledsun-web.xml
, so if you're seeing manuals/blogs/tutorials referencing it, yes it's exactly the same file):Either way, you should then be able to use http://localhost:8080/contextname/uploads/* to serve those uploaded images from by
<img>
the usual way.See also: