FileDownload和FileUpload JSF Primefaces不起作用 [英] FileDownload and FileUpload JSF Primefaces not working

查看:103
本文介绍了FileDownload和FileUpload JSF Primefaces不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PrimeFaces 3.1.2,NetBeans 7.2,JSF 2.1和GlassFish 3.1.2.

I'm using PrimeFaces 3.1.2, NetBeans 7.2, JSF 2.1 and GlassFish 3.1.2.

我正在使用从 http://www.primefaces获得的实际代码. org/showcase/ui/fileUploadAuto.jsf http://www.primefaces.org/showcase/ui/fileDownload.jsf.

当我运行文件上传代码时,它根本不起作用.该文件未上传,并且未显示成功消息.但是,如果文件大小超过上述大小,则会显示一条消息,指出文件大小太大.

When I run the file upload code, it doesn't work at all. The file doesn't get uploaded and no success message is shown. But if the file size exceeds the size mentioned, it is showing a message that the file size is too large.

这是我的观点:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">
    <h:head>
    </h:head>
    <h:body>
        <h:form enctype="multipart/form-data">
            <p:fileUpload fileUploadListener="#{fileUploadController.handleFileUpload}"
                mode="advanced"
                update="messages"
                auto="true"
                sizeLimit="100000" 
                allowTypes="/(\.|\/)(gif|jpe?g|png)$/"/>
            <p:growl id="messages" showDetail="true"/>
        </h:form>
    </h:body>
</html>

这是我的后援豆:

package com;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.primefaces.event.FileUploadEvent;

@ManagedBean
@SessionScoped
public class FileUploadController {
    public void handleFileUpload(FileUploadEvent event) {
        FacesMessage msg = new FacesMessage("Succesful",   event.getFile().getFileName() + " is uploaded.");
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }
}

与PrimeFaces展示页面中的代码基本相同.

Its basically the same code in the PrimeFaces showcase page.

类似文件下载代码;当我单击下载时,没有任何反应.弹出窗口会在我什至没有注意到的情况下打开和关闭.我已经在获取资源流中提到了图像文件(对于下载部分),但是我不知道出了什么问题.该代码也与PrimeFaces展示页面中的代码基本相同.

Similarly with file download code; when I click on download nothing happens. A pop up opens and closes before I could even notice it. I have the image file in place as mentioned in the get resource stream (for the download part), but I don't know what's the problem. The code is also basically the same as in the PrimeFaces showcase page.

我在Netbeans的Glassfish下看不到任何日志或错误.如果有必要,我也不知道如何启用日志记录.

I don't see any logs or errors under Glassfish in Netbeans. I also don't know how to enable logging if necessary.

推荐答案

您需要做的第一件事是向应用程序中添加一些库.实际上,PrimeFaces文件上传依赖于Apache commons-file-upload和commons-io库.因此,将它们下载并添加到您的WEB-INF/lib路径中:

The first thing you need is add some libraries to your application. As a matter of fact, PrimeFaces file upload relies on Apache commons-file-upload and commons-io libraries. So dowload them and add them to your WEB-INF/lib path:

您可以从以下链接下载它.

you can download it from following link.

http://commons.apache.org/io/

http://commons.apache.org/fileupload/

此外,您必须将其配置为web.xml

in addition you have to configure it into web.xml

<filter>
  <filter-name>PrimeFaces FileUpload Filter</filter-name>
  <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
  <init-param>
    <param-name>thresholdSize</param-name>
    <param-value>51200</param-value>
  </init-param>
  <init-param>
    <param-name>uploadDirectory</param-name>
    <param-value>C:\etc</param-value>
   </init-param>
</filter>
<filter-mapping>
  <filter-name>PrimeFaces FileUpload Filter</filter-name>
  <servlet-name>Faces Servlet</servlet-name>
</filter-mapping>

此外,如果您要以编程方式进行设置,请更改上传文件的目标位置:

Also If you want to set programmatically change the destination of your uploaded files have a look:

PrimeFaces FileUpload文件保存目标

这篇关于FileDownload和FileUpload JSF Primefaces不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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