RichFaces 文件上传和 NullPointerException
我正在尝试使用 rich:fileUpload 将文件上传到服务器,这是代码:
@Name("fileUploader")
public class FileUploader {
private byte[] fileData;
public void uploadFileListener(UploadEvent uploadEvent) {
fileData = uploadEvent.getUploadItem().getData();
//other code here
}
}
页面包含以下内容:
rich:fileUpload fileUploadListener="#{fileUploader.uploadFileListener}"
我得到的是 uploadEvent 包含有关文件名、大小等的数据...但是
uploadEvent.getUploadItem().getData();
返回 null...
我已经在此处看到了类似的问题...但没有答案...
谢谢!
I'm trying to upload file to the server using rich:fileUpload, here's the code:
@Name("fileUploader")
public class FileUploader {
private byte[] fileData;
public void uploadFileListener(UploadEvent uploadEvent) {
fileData = uploadEvent.getUploadItem().getData();
//other code here
}
}
page contains the following :
rich:fileUpload fileUploadListener="#{fileUploader.uploadFileListener}"
What I'm getting is that uploadEvent contains data about file name, size and so on... but
uploadEvent.getUploadItem().getData();
returns null...
I've already seen similar issue here... but there's no answer...
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否在组件 xml 中配置了 multipart-filter ?
像这样的东西:
更新:我不完全确定这是否用于 s:fileUpload 或 rich:fileUpload。在考虑此配置之前,请检查下面的代码。默认情况下,如果我没记错的话,您应该使用临时文件,我认为这是 RichFaces 的默认配置。抱歉,我这里没有我的项目来检查它。
如果您按照上面的方式进行配置,您的文件将保存到临时文件 (create-temp-files="true"),在这种情况下,您应该使用以下方式访问您的数据
您可以检查它是否存储在文件中:
既然你说文件“元数据”在那里,这看起来就是问题所在,你只是在错误的地方寻找你的数据:)
如果你将其配置为 false,你的方法应该可以工作。
我还记得一些(不完全确定)上传控件(rich:upload)需要位于 h:form 内
希望它有帮助。
Did you configured multipart-filter in components xml?
Something like this:
Update: I'm not completely sure if this is used for s:fileUpload or rich:fileUpload. Please check the code bellow before thinking about this configuration. By default, if I remember correctly, you should be using temp files which is the default configuration for RichFaces I think. Sorry but I don't have my project here to check it.
If you have it configured like the above your file will be saved to a temporary file (create-temp-files="true"), in this case you should access your data by using
You can check if it is stored in a file with:
Since you say that the file "metadata" is there, this looks to be the problem, you are just looking for your data in the wrong place :)
If you have it configured to false, your method should work.
Also I remember something (not completely sure) that the upload control (rich:upload) needed to be inside an h:form
Hope it helps.
让我们按顺序排列:
首先,您必须定义是否希望将数据直接上传到
byte[]
缓冲区或临时文件中,以及支持的最大大小。这就是为什么你必须在某个地方定义两个参数。现在,您需要定义这些参数的位置取决于您使用的组件(即seam fileUpload 组件或richfaces fileUpload 组件)以及为您的应用程序安装的技术。让我们看看:
如果您使用带有richfaces的seam框架并且不需要ajax支持(即在上传完成后显示正在加载的图像),
将是一个不错的选择,要配置它,您必须以这种方式更改您的seamcomponents.xml
文件:这意味着您发送到服务器的数据将保存在临时文件中,并且支持的最大大小为
1000000
(约 1M)。更多信息可以在这里找到: http://seamframework.org/Documentation/HowToUploadAndDownloadFilesInSeam现在并使用相同的技术堆栈(seam + richfaces)但在这种情况下您需要使用ajax支持。在这种情况下,
将是您的选择。要配置它,只需在web.xml
文件中定义的 Seam Filter 中更改此设置:请注意,尽管参数在两种情况下相同,但它们定义在不同的位置 - 我想这在 JEE6 中已更改CDDI-。
如果您的应用程序不使用 Seam 框架,但确实使用 richfaces,您可能必须在
web.xml
中的相应 richfaces 过滤器中再次定义此参数。现在有趣的部分是
的用法。这是使用该组件的名为BlockEdit.xhtml
的文件的相关部分:这是
BlockHome.java
的 fileUploadListener如果您配置了
createTempFiles< web.xml 中的接缝过滤器内的 /code> 是:
那么 item.getData() 将不为 null。但是,如果您将参数
createTempFiles
定义为true
,那么如果您想知道它是否有值,则应该询问item.getFile()。这是因为,当您决定避免创建临时文件时,item.getData() 就会被填充,如 Richfaces 演示文件上传文档。希望这有帮助
Let's put this in order:
First at all you have to define if you want your data uploaded directly in a
byte[]
buffer or in a temporary file and the maximum size supported. That is why you have to define two parameters in some place.Now the place where you need to define those parameters, depends on which component you are using (i.e. seam fileUpload component or richfaces fileUpload component) and which technology is mounted for your application. Let's see:
If you are using seam framework with richfaces and you don't need ajax support (i.e. show the image being loaded once upload is finished),
<s:fileUpload>
would be a good choice, to configure it you have to change your seamcomponents.xml
file this way:This means that the data you send to the server will be saved in a temporary file and that the maximum size supported is
1000000
(about 1M). More info can be found here: http://seamframework.org/Documentation/HowToUploadAndDownloadFilesInSeamNow and using the same technology stack (seam + richfaces) but in this case you need to use ajax support.
<rich:fileUpload>
will be in that case your choice. To configure it just change this in the Seam Filter defined inweb.xml
file:Note that although the parameters are the same in both cases they are defined in different places -I imagine this is changed in JEE6 with CDDI-.
If your application don't use seam framework but it does use richfaces probably you have to define this parameters it in the corresponding richfaces filter again in
web.xml
.Now the interesting part, the usage of
<rich:fileUpload>
. This is the relevant part of a file calledBlockEdit.xhtml
where the component is used:And this is the fileUploadListener of
BlockHome.java
If your configuration of
createTempFiles
inside the seam filter in web.xml was:then item.getData() will be not null. But if you defined the parameter
createTempFiles
totrue
, then you should ask for item.getFile() if you want to know if it has a value. This is because the item.getData() is filled just when you decide to avoid the creation of temporary files, as stated in Richfaces demo fileUpload documentation.Hope this helps