简体   繁体   English

使用Primefaces上传文件系统上的文件

[英]Upload a file on filesystem with Primefaces

I read many pages but, i'm sure, I did some errors somewhere and I can't do the upload of a file simply as It should be. 我读了很多页但是,我确定,我在某个地方做了一些错误,我不能像上面那样上传文件。

First, I'm developing using JSF2.0, Primefaces 3.2 and JPA2 on Glassfish 3.1 with Netbeans 7 and JDK 1.7. 首先,我正在使用Netbeans 7和JDK 1.7在Glassfish 3.1上使用JSF2.0,Primefaces 3.2和JPA2进行开发。

The next thing that I must say is that I'm tringg to insert this code into an interface that will be extended from many others classes and that should store the files in many folders on filesystem! 接下来我要说的是,我要把这个代码插入到一个接口中,该接口将从许多其他类扩展,并且应该将文件存储在文件系统的许多文件夹中! So now I will report what I wrote and, please, tell me where is the problem! 所以现在我将报告我写的内容,请告诉我问题出在哪里!

This is the code into web.xml: 这是web.xml中的代码:

<filter>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
    <init-param>
        <param-name>uploadDirectory</param-name>
        <param-value>D:/Cyborg/</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
</filter-mapping>  

The page that contains the p:uploadFile contains also many other form's fields and I will copy only what I think is of your interest: 包含p:uploadFile的页面还包含许多其他表单的字段,我将仅复制我认为您感兴趣的内容:

...
<h:form method="POST">
    ...
    <h:outputLabel for="uploadFile" value="#{bundle.LabelUploadFile}" />
    <p:fileUpload value="#{progettiController.uploadFile}" mode="simple" />
    ...
    <h:commandLink action="#{progettiController.create}" value="#{bundle.SaveLink}" />
    ...

this is the code into the interface called AbstractController: 这是名为AbstractController的接口代码:

protected UploadedFile uploadFile;

public void setUploadFile(UploadedFile uploadFile) {
    this.uploadFile=uploadFile;
}

public UploadedFile getUploadFile() {
    return uploadFile;
}

and finally the method Create into ProgettiController: 最后创建到ProgettiController的方法:

public String create() {
    try {
        getFacade().create(current);
        System.out.println("Message: ProgettiController: Create: new row created successfully!");

        try {
            current=getFacade().findLast();
            String ext=uploadFile.getFileName();
            System.out.println("Message: ProgettiController: Create: FileName="+ext);

            ext=ext.substring(ext.lastIndexOf("."),ext.length());
            System.out.println("Messaggio: ProgettiController: ext="+ext);

            current.setNomeFile(ext.substring(0,ext.lastIndexOf(".")));
            current.setTipoFile(ext.substring(ext.lastIndexOf(".")+1,ext.length()));
            getFacade().edit(current);
            ext=urlFilesystem+current.getId()+ext.substring(ext.lastIndexOf(".")+1,ext.length());
            System.out.println("Message: ProgettiController: Create: posizione e nome del file="+ext);

            File oldFile= (File)uploadFile;
            File newFile= new File(ext);
            oldFile.renameTo(newFile);
        } catch (Exception e) {
            System.out.println("Messaggio: ProgettiController: Create: errore nel try legato al upload="+e.getMessage());
            e.printStackTrace();
        }

        JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("ProgettiCreated"));
        recreateModel();
        System.out.println("Messaggio: ProgettiController: create try");
        return url+"List?faces-redirect=true";
    } catch (Exception e) {
        JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
        System.out.println("Messaggio: ProgettiController: create catch="+e.getMessage());
        e.printStackTrace();
        return url+"List?faces-redirect=true";
    }
}

If I try this code the problem is that when the page call the method returns null into uploadFile, if I add the "enctype="multipart/form-data"" into h:form tag the application didn't call the method! 如果我尝试这个代码的问题是,当页面调用方法返回null到uploadFile时,如果我将“enctype =”multipart / form-data“”添加到h:form标签中,应用程序没有调用该方法! Can someone help me? 有人能帮我吗? Thanks a lot! 非常感谢!

if I add the "enctype="multipart/form-data"" into h:form tag the application didn't call the method! 如果我将“enctype =”multipart / form-data“”添加到h:form标签中,应用程序就不会调用该方法!

That would mean that the PrimeFaces file upload filter wasn't able to do its job properly. 这意味着PrimeFaces文件上传过滤器无法正常工作。 It is responsible for parsing a multipart/form-data request and providing the parsed data further to JSF in an understandable form (until the upcoming JSF 2.2 version, JSF does by default not support multipart/form-data requests). 它负责解析multipart/form-data请求,并以可理解的形式将解析后的数据提供给JSF(直到即将推出的JSF 2.2版本,JSF默认不支持multipart/form-data请求)。

That can have at least the following main reasons: 这可能至少有以下主要原因:

  • The <servlet-name> in the <filter-mapping> is incorrect. <filter-mapping><servlet-name>不正确。 You need to make sure that it's exactly the same <servlet-name> of the <servlet> configuration of the FacesServlet in the same web.xml . 您需要确保它与同一web.xml FacesServlet<servlet>配置的<servlet-name>完全相同。

  • The Commons IO and FileUpload JARs are not in the webapp's runtime classpath. Commons IO和FileUpload JAR不在webapp的运行时类路径中。 I don't use Netbeans, so I can't go in detail, but whatever way you use to include the JARs in the webapp, you need to make absolutely sure that they end up in /WEB-INF/lib folder of the deployed webapp. 我不使用Netbeans,所以我不能详细介绍,但无论你使用什么方式将JAR包含在webapp中,你都需要确保它们最终在部署的/WEB-INF/lib文件夹中Web应用程序。 You can verify that by checking the deploy in the server. 您可以通过检查服务器中的部署来验证这一点。 In Eclipse, that can among others be achieved by just dropping those JARs straight in exactly that folder of the web project. 在Eclipse中,只需将这些JAR直接放在Web项目的文件夹中即可实现。

  • You happen to have another Filter in your web application which does roughly the same job as the PrimeFaces file upload filter and is executed before the PrimeFaces file upload filter. 您碰巧在Web应用程序中有另一个Filter ,它与PrimeFaces文件上载过滤器的工作大致相同,并且在PrimeFaces文件上载过滤器之前执行。 For example, the MyFaces ExtensionsFilter which is bundled in Tomahawk. 例如,捆绑在Tomahawk中的MyFaces ExtensionsFilter The request body can namely be parsed only once. 请求体只能解析一次。

there's an error in your code causing the null pointer 导致空指针的代码出错

        {..
        ext=ext.substring(ext.lastIndexOf("."),ext.length()); 
        // here ext contains the extension of the file only

        System.out.println("Messaggio: ProgettiController: ext="+ext);

        current.setNomeFile(ext.substring(0,ext.lastIndexOf(".")));
        // used the extension as the file name
        current.setTipoFile(ext.substring(ext.lastIndexOf(".")+1,ext.length()));
        // the file type is an empty string causing the error
        ..}

try the following 尝试以下方法

        {..
        ext=ext.substring(ext.lastIndexOf("."),ext.length()); 

        System.out.println("Messaggio: ProgettiController: ext="+ext);

       current.setNomeFile(uploadFile.getFileName().
                           substring(0,uploadFile.getFileName().lastIndexOf(".")));
        // the file name is setted properly
        current.setTipoFile(uploadFile.getFileName().
                           substring(uploadFile.getFileName().
                           lastIndexOf(".")+1,uploadFile.getFileName().length()));
        // the file type is setted properly
        ..}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM