1

我现在正在尝试使用 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"/>

网页.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

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

有人知道发生了什么吗?

谢谢

4

3 回答 3

2

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

快速谷歌后:

Commons IO
Commons File Upload

或行家风格:

<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}"

资源: 资源

于 2013-01-28T13:22:29.867 回答
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 文件上传,我还可以使用这个文件,例如稍后打印出来吗?

于 2013-01-28T14:58:02.537 回答
-1

编辑

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

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

PrimeFaces 4 的默认设置是“本机”。如果你想使用 commons-fileupload 你需要在你的web.xml

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

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

于 2015-09-18T16:31:36.690 回答