【问题标题】:primefaces upload not doing anything [duplicate]primefaces上传没有做任何事情[重复]
【发布时间】:2013-01-11 19:19:30
【问题描述】:

我现在正在尝试使用 primefaces 而不是自制的文件上传系统,我遇到的唯一问题是它似乎什么也没做,有什么想法吗?

我正在使用 glassfish,我已经添加了

commons-io-1.4.jar commons-fileupload-1.2.1/jar

到我在 netbeans 中的库

这是我的 xhtml

<p:fileUpload fileUploadListener="#{fileUploadController.upload}"
                                  mode="advanced" 
                                  update="messages"
                                  sizeLimit="100000000" 
                                  allowTypes="/(\.|\/)(gif|jpe?g|png)$/"/>

                    <p:growl id="messages" showDetail="true"/>

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:\Users\Richard\printing~subversion\fileupload\web\Uploaded</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
</filter-mapping>

文件上传控制器.java

    @ManagedBean(name="fileUploadController")
public class FileUploadController {
   private String destination="C:/Users/Richard/printing~subversion/fileupload/web/Uploaded";

    public void upload(FileUploadEvent event) {  
        FacesMessage msg = new FacesMessage("Success! ", event.getFile().getFileName() + " is uploaded.");  
        FacesContext.getCurrentInstance().addMessage(null, msg);
        // Do what you want with the file        
        try {
            copyFile(event.getFile().getFileName(), event.getFile().getInputstream());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }  

    public void copyFile(String fileName, InputStream in) {
           try {


                // write the inputStream to a FileOutputStream
                OutputStream out = new FileOutputStream(new File(destination + fileName));

                int read = 0;
                byte[] bytes = new byte[1024];

                while ((read = in.read(bytes)) != -1) {
                    out.write(bytes, 0, read);
                }

                in.close();
                out.flush();
                out.close();

                System.out.println("New file created!");
                } catch (IOException e) {
                System.out.println(e.getMessage());
                }
    }
}

我也试过这个例子:http://e-blog-java.blogspot.co.uk/2010/04/ppr-multi-file-upload-with-primefaces.html

但问题是当我复制代码时,我无法让上传显示?

有人知道发生了什么吗?

谢谢

【问题讨论】:

    标签: jsf upload primefaces


    【解决方案1】:

    您是否在项目中包含了必要的导入?如果我没记错的话,我曾经使用过 primefaces 上传,我需要为该上传内容包含一个特定的 maven 依赖项。

    快速谷歌后:

    Commons IO
    Commons File Upload
    

    或maven风格:

    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>1.3.2</version>
    </dependency>
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.2.1</version>
    </dependency>
    

    结合上面的答案:)

    fileUploadListener="#{uploaderBB.handleFileUpload}"
    

    资源: Resource

    【讨论】:

    • 谢谢,我已将依赖项添加到我的库中,我使用的是 glassfish,所以 maven 不好:(
    • @user1924104 maven 是一款可以帮助您构建组件的软件,这个答案只是试图告诉您应该添加的库,但是您的项目中有这些库的更高版本。
    • 啊,我明白了,谢谢,是的,所有库都是最新的并已添加
    【解决方案2】:

    好的解决了这个问题!,对于primefaces,你必须先放置过滤器,然后是你可能拥有的任何其他过滤器(我确实有一个来自以前文件上传的过滤器)所以现在我的web.xml看起来像:

    <!-- <filter>
                <filter-name>Upload Filter</filter-name>
                <filter-class>richard.fileupload.UploadFilter</filter-class>
                <init-param>
                    <param-name>sizeThreshold</param-name>
                    <param-value>1024</param-value>
                </init-param>
            </filter>
            <filter-mapping>
                <filter-name>Upload Filter</filter-name>
                <url-pattern>/upload/*</url-pattern>
            </filter-mapping> -->
            <context-param>
                <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
                <param-value>server</param-value>
            </context-param>
            <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>20480</param-value>
                </init-param>
                <init-param>
                    <param-name>uploadDirectory</param-name>
                    <param-value>C:/Users/Richard/printing~subversion/fileupload/web/WEB-INF/uploaded</param-value>
                </init-param>
            </filter>
            <filter-mapping>
                <filter-name>PrimeFaces FileUpload Filter</filter-name>
                <servlet-name>Faces Servlet</servlet-name>
            </filter-mapping>
            <servlet>
                <servlet-name>Faces Servlet</servlet-name>
                <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
            </servlet>
            <servlet-mapping>
                <servlet-name>Faces Servlet</servlet-name>
                <url-pattern>*.xhtml</url-pattern>
            </servlet-mapping>
            <welcome-file-list>
                <welcome-file>/GUI/index.xhtml</welcome-file>
            </welcome-file-list>
            <context-param>
                <param-name>javax.faces.PROJECT_STAGE</param-name>
                <param-value>Development</param-value>
            </context-param>
            <context-param>
                <param-name>facelets.LIBRARIES</param-name>
                <param-value>/WEB-INF/corejsf.taglib.xml</param-value>
            </context-param>
            <context-param>
                <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
                <param-value>true</param-value>
            </context-param>
    

    我唯一注意到的是,它们现在作为 TMP 文件上传,我还可以使用这个文件,例如稍后打印出来吗?

    【讨论】:

    • 临时文件不一定是供您使用的,而是供primefaces使用的。您应该检索FileUploadListener 中的二进制内容并用它做您想做的事情。
    【解决方案3】:

    编辑

    这个问题来自 2013 年 1 月,当时只有 PrimeFaces 3.5。对于 3.5,默认值为 commons-fileupload。所以对于 3.5,不需要下面的 context-param。

    如果您使用 PrimeFaces 4 并遇到此问题:

    PrimeFaces 4 的默认设置是“native”。如果你想使用 commons-fileupload 你需要在你的web.xml 中包含这个参数:

    <context-param>
        <param-name>primefaces.UPLOADER</param-name>
        <param-value>commons</param-value>
    </context-param>
    

    在使用 WebLogic 时,我使用的是默认值,但在尝试上传时,什么也没发生。没有错误。无论哪种方式,使用 Tomcat 都很好。我只用 Tomcat ant WebLogic 试过。

    【讨论】:

    • 如何回答上述问题?
    • @Kukeltje 为清楚起见编辑了我的答案。该问题特别提到使用commons-fileupload。未提供完整的 web.xml,因此不清楚是否包含此上下文参数。我需要该参数才能使其对我有用。这是对上述问题的回答。如果是您的,请删除您的反对票。如果答案仍然不好,最好先要求澄清,然后投反对票。
    • 嗯,这是您的意见。如果我不投反对票,那么改善答案的动力通常不存在,所以什么也没有发生,如果一周内没有任何改善,我需要跟踪哪些答案在之后投反对票?两周?如果我投反对票并且有人改进了这个人经常发表评论的问题,我会收到通知。然后我可以决定是否删除反对票。你可能不喜欢这种方式,但它对我最有效(我经常发表评论,就像现在我投反对票时一样)
    • 现在正题。问题是从 2013 年 1 月开始的,当时只有 PrimeFaces 3.5。上传的默认值是... commons,所以不需要配置它。在此之后,您的假设是错误的,因此如果有人遇到相同的问题但使用较新的 PF 版本(>= 4.0),您的答案才“有效”。那时还没有。它将回退到“本机”,要么工作要么导致不同的错误。因此,即使那样,答案也很可能无效。如果您将所有这些信息添加到答案中,我将删除反对票
    • 顺便说一句,请注意我没有否决您的其他类似“答案”。他们更具体
    猜你喜欢
    • 1970-01-01
    • 2019-03-10
    • 1970-01-01
    • 2019-04-10
    • 2017-04-24
    • 2012-09-07
    • 2016-10-02
    • 2013-09-28
    • 1970-01-01
    相关资源
    最近更新 更多