primefaces上传没有做任何事情

时间:2013-01-28 12:41:05

标签: jsf upload primefaces

我现在正在尝试使用primefaces而不是家庭酿造的文件上传系统,只有我遇到的问题似乎是什么都不做,任何想法为什么?

我正在使用glassfish,我已添加

公地-IO-1.4.jar 公地文件上传-1.2.1 /罐

到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>

FileUploadController.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

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

任何人都有任何想法发生了什么?

由于

3 个答案:

答案 0 :(得分:2)

您是否在项目中包含了必要的导入?如果我没记错的话,我曾经使用过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

答案 1 :(得分: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文件上传的,我是否仍然可以使用此文件例如稍后打印出来?

答案 2 :(得分:-1)

修改

这个问题是从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尝试过它。