使用RichFaces上传文件 [英] File upload using RichFaces

查看:123
本文介绍了使用RichFaces上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在寻找使用Java Server Faces上传的文件。我使用RichFaces找到了这篇精彩的介绍。但是,我在这里理解这个过程有些麻烦。

I am currently looking in to some file uploading using Java Server Faces. I've found this great introduction to it using RichFaces. However, I have some troubles understanding the process here.

首先用户选择一个文件,如果立即上传设置为true,则使用ajax处理文件,到目前为止超好的。然而,当谈到下一步时,Bean端的监听器让我感到困惑:

First the user selects a file and if the immediate upload is set to true the file is processed using ajax, so far so good. When it comes to the next step however, the listener on the Bean-side the following confuses me:

public void listener(UploadEvent event) throws Exception{
    UploadItem item = event.getUploadItem();

    File f = item.getFile();

    System.out.println(f.getAbsolutePath());
}

绝对路径是我计算机上的临时目录,我当然明白,但是如何将文件提供给webbapplication?我的应用程序部署为WAR文件。是否可以将其上传到WAR?可能听起来很愚蠢,但它实际上可能很方便。

The Absolute path is to a temp directory on my computer, sure I understand that, but how would you make the file available to the webbapplication? My application is deployed as a WAR-file. Is it possible to Upload it to the WAR? Might sound stupid or so, but it might actually be handy.

我完全知道我可以重命名文件以将其复制到新位置,但是要走的路?

I am fully aware that I can rename the file to copy it to a new location, but is that the way to go?

推荐答案

将内容写入WAR目录(假设WAR甚至作为目录展开)通常是,一个坏主意(出于同样的原因,将数据存储在应用程序目录中通常是一个坏主意)。

Writing things to your WAR directory (assuming the WAR is even exploded as a directory) is, in generally, a bad idea (for the same reasons that storing your data in your application directory is usually a bad idea).

你可能希望它最终成为一个持久性存储,通常是数据库。究竟如何管理由您决定 - 无论您是使用文件系统还是在数据库中存储引用,还是直接在数据库中存储BLOB以及是否使用JDBC或JPA或Hibernate等。

You'd probably want it to end up in a persistence store, usually a database. Exactly how you manage that is up to you - whether you use the file system and store a reference in the database or store a BLOB in the database directly and whether you use JDBC or JPA or Hibernate, etc.

然后可以通过使用诸如< a4j:support event =onuploadcompletereRender =info之类的东西刷新包含它们的面板,从数据库中读取上传文件的列表。 />

The list of uploaded files could then be read from the database by refreshing the panel that contained them using something like <a4j:support event="onuploadcomplete" reRender="info" />.

文件下载servlet(如果RichFaces没有)很容易编写。

A file download servlet (if RichFaces doesn't have one) is fairly easy to write.

这篇关于使用RichFaces上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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