上传的图像仅在刷新页面后才可用 [英] Uploaded image only available after refreshing the page

查看:199
本文介绍了上传的图像仅在刷新页面后才可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上传图片时,文件成功保存,路径设置成功。但是在提交表单之后,上传的图像不会立即显示。只有当我重新加载页面,显示上传的图像。



我保存上传文件如下:

  InputStream是; 
尝试{
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());
字节buf [] =新字节[1024];
int len; ((len = is.read(buf))> 0){
os.write(buf,0,len);
}
os.close();
is.close();
$ b $ catch(IOException ex){
System.out.println(ex.getStackTrace());

$ / code>

为什么上传的图片只在重新加载页面后才显示,我该如何解决这是什么?

解决方案

您正在将文件直接写入IDE的项目文件夹,而您的意图似乎将文件保存在webapp部署文件夹。这是一个坏主意,以及由于以下3个主要原因:


  1. IDE的项目文件夹中的更改不会立即获得体现在服务器的工作文件夹中。在IDE中有一种后台作业,它注意到服务器的工作文件夹与上次更新(在IDE中称为发布)是同步的。这是你看到的问题的主要原因。在现实世界的代码中存在的情况下,上传的文件存储在webapp的部署文件夹将无法正常工作在所有。一些服务器(默认或通过配置)不会将已部署的WAR文件扩展到本地磁盘文件系统,而是完全在内存中。如果没有基本上编辑已部署的WAR文件并重新部署它,则无法在内存中创建新文件。
    即使服务器将部署的WAR文件展开为本地磁盘文件系统,所有新创建的文件在重新部署或甚至简单重新启动时都会丢失,只是因为这些新文件不是原始WAR文件的一部分。


您需要将其写入项目/ deploy文件夹之外的固定路径。例如, / var / webapp / uploads 。然后,为了让你的web应用程序服务,只需将它作为新的Web应用程序上下文添加到服务器。

根据您以前的问题,我知道您使用的是Glassfish 3.1。在这个服务器中,它被称为虚拟主机。您可以在管理控制台的服务器级别在 http:// localhost:4848 >配置> HTTP服务>虚拟服务器,或者通过将以下行添加到 /WEB-INF/glassfish-web.xml (您的IDE应该自动生成一个;注意这个文件在Glassfish 3.1之前调用 sun-web.xml ,所以如果你看到手册/博客/教程引用它,是的它是完全相同的文件):


$ b

 < property name =alternatedocroot_1value =from = / uploads / * dir = / var / webapp/> 

无论哪种方式,您都可以使用 http:// localhost:8080 / contextname / uploads / *可以通过< img> 通常的方式。

另请参阅:




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?

解决方案

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:

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

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

  3. 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 called sun-web.xml, so if you're seeing manuals/blogs/tutorials referencing it, yes it's exactly the same file):

<property name="alternatedocroot_1" value="from=/uploads/* dir=/var/webapp" />

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:

这篇关于上传的图像仅在刷新页面后才可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆