RichFaces 文件上传和 NullPointerException

发布于 2024-10-22 07:04:26 字数 693 浏览 1 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

攀登最高峰 2024-10-29 07:04:26

您是否在组件 xml 中配置了 multipart-filter ?

像这样的东西:

<web:multipart-filter create-temp-files="true" max-request-size="1000000" url-pattern="*.seam"/>

更新:我不完全确定这是否用于 s:fileUpload 或 rich:fileUpload。在考虑此配置之前,请检查下面的代码。默认情况下,如果我没记错的话,您应该使用临时文件,我认为这是 RichFaces 的默认配置。抱歉,我这里没有我的项目来检查它。

如果您按照上面的方式进行配置,您的文件将保存到临时文件 (create-temp-files="true"),在这种情况下,您应该使用以下方式访问您的数据

uploadEvent.getUploadItem().getFile()

您可以检查它是否存储在文件中:

uploadEvent.getUploadItem().isTempFile()

既然你说文件“元数据”在那里,这看起来就是问题所在,你只是在错误的地方寻找你的数据:)

如果你将其配置为 false,你的方法应该可以工作。

我还记得一些(不完全确定)上传控件(rich:upload)需要位于 h:form 内

希望它有帮助。

Did you configured multipart-filter in components xml?

Something like this:

<web:multipart-filter create-temp-files="true" max-request-size="1000000" url-pattern="*.seam"/>

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

uploadEvent.getUploadItem().getFile()

You can check if it is stored in a file with:

uploadEvent.getUploadItem().isTempFile()

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.

孤独岁月 2024-10-29 07:04:26

让我们按顺序排列:

首先,您必须定义是否希望将数据直接上传到 byte[] 缓冲区或临时文件中,以及支持的最大大小。这就是为什么你必须在某个地方定义两个参数。

现在,您需要定义这些参数的位置取决于您使用的组件(即seam fileUpload 组件或richfaces fileUpload 组件)以及为您的应用程序安装的技术。让我们看看:

如果您使用带有richfaces的seam框架并且不需要ajax支持(即在上传完成后显示正在加载的图像),将是一个不错的选择,要配置它,您必须以这种方式更改您的seam components.xml 文件:

<web:multipart-filter create-temp-files="true"
                      max-request-size="1000000" 
                      url-pattern="*.seam" />

这意味着您发送到服务器的数据将保存在临时文件中,并且支持的最大大小为1000000(约 1M)。更多信息可以在这里找到: http://seamframework.org/Documentation/HowToUploadAndDownloadFilesInSeam

现在并使用相同的技术堆栈(seam + richfaces)但在这种情况下您需要使用ajax支持。在这种情况下, 将是您的选择。要配置它,只需在 web.xml 文件中定义的 Seam Filter 中更改此设置:

<filter>
    <filter-name>Seam Filter</filter-name>
    <filter-class>org.jboss.seam.servlet.SeamFilter</filter-class>
    <init-param>
        <param-name>createTempFiles</param-name>
        <param-value>false</param-value>
    </init-param>
    <init-param>
        <param-name>maxRequestSize</param-name>
        <param-value>10000000</param-value>
    </init-param>
</filter>

请注意,尽管参数在两种情况下相同,但它们定义在不同的位置 - 我想这在 JEE6 中已更改CDDI-。

如果您的应用程序不使用 Seam 框架,但确实使用 richfaces,您可能必须在 web.xml 中的相应 richfaces 过滤器中再次定义此参数。

现在有趣的部分是 的用法。这是使用该组件的名为 BlockEdit.xhtml 的文件的相关部分:

       <rich:fileUpload
            fileUploadListener="#{blockHome.sketchListener}"
            maxFilesQuantity="1"
            reRender="table" 
            id="upload"
            immediateUpload="true"
            acceptedTypes="jpg, gif, png, bmp">
                <a:support event="onuploadcomplete" reRender="sketchImage" />
       </rich:fileUpload>

这是 BlockHome.java 的 fileUploadListener

public void sketchListener(UploadEvent event) {
    UploadItem item = event.getUploadItem();
    logger.info("Sketch listener executed ...");
    if(item != null && item.getData() != null) {
        logger.info(item.getFileName()+" "+item.getFileSize());
        getInstance().getSketch().setData(item.getData());
        getInstance().getSketch().setName(item.getFileName());
        getInstance().getSketch().setContentType(item.getContentType());
        getInstance().getSketch().setSize(item.getFileSize());
    }
}

如果您配置了 createTempFiles< web.xml 中的接缝过滤器内的 /code> 是:

    <init-param>
        <param-name>createTempFiles</param-name>
        <param-value>false</param-value>
    </init-param>

那么 item.getData() 将不为 null。但是,如果您将参数createTempFiles定义为true,那么如果您想知道它是否有值,则应该询问item.getFile()。这是因为,当您决定避免创建临时文件时,item.getData() 就会被填充,如 Richfaces 演示文件上传文档

FileUpload 使用两个初始化参数,应在 Filter 中定义
web.xml 中的定义:

createTempFiles 布尔属性,定义是否
上传的文件存储在临时文件中或在侦听器中可用
就像 byte[] 数据一样(本例中为 false)。
maxRequestSize 属性定义最大大小(以字节为单位)
上传的文件(本例中为 1000000 个)。

希望这有帮助

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 seam components.xml file this way:

<web:multipart-filter create-temp-files="true"
                      max-request-size="1000000" 
                      url-pattern="*.seam" />

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/HowToUploadAndDownloadFilesInSeam

Now 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 in web.xml file:

<filter>
    <filter-name>Seam Filter</filter-name>
    <filter-class>org.jboss.seam.servlet.SeamFilter</filter-class>
    <init-param>
        <param-name>createTempFiles</param-name>
        <param-value>false</param-value>
    </init-param>
    <init-param>
        <param-name>maxRequestSize</param-name>
        <param-value>10000000</param-value>
    </init-param>
</filter>

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 called BlockEdit.xhtml where the component is used:

       <rich:fileUpload
            fileUploadListener="#{blockHome.sketchListener}"
            maxFilesQuantity="1"
            reRender="table" 
            id="upload"
            immediateUpload="true"
            acceptedTypes="jpg, gif, png, bmp">
                <a:support event="onuploadcomplete" reRender="sketchImage" />
       </rich:fileUpload>

And this is the fileUploadListener of BlockHome.java

public void sketchListener(UploadEvent event) {
    UploadItem item = event.getUploadItem();
    logger.info("Sketch listener executed ...");
    if(item != null && item.getData() != null) {
        logger.info(item.getFileName()+" "+item.getFileSize());
        getInstance().getSketch().setData(item.getData());
        getInstance().getSketch().setName(item.getFileName());
        getInstance().getSketch().setContentType(item.getContentType());
        getInstance().getSketch().setSize(item.getFileSize());
    }
}

If your configuration of createTempFiles inside the seam filter in web.xml was:

    <init-param>
        <param-name>createTempFiles</param-name>
        <param-value>false</param-value>
    </init-param>

then item.getData() will be not null. But if you defined the parameter createTempFiles to true, 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.

FileUpload uses two init parameters which should be defined in Filter
definition in web.xml:

createTempFiles boolean attribute which defines whether the
uploaded files are stored in temporary files or available in listener
just as byte[] data (false for this example).
maxRequestSize attribute defines max size in bytes of the
uploaded files (1000000 for this example).

Hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文