简体   繁体   English

Primefaces上传文件的问题

[英]issue with Upload File with Primefaces

I'm trying insert a download in my webApplication. 我正在尝试在我的webApplication中插入下载内容。

First of all the page which contains the form where there is the is on 首先,该页面包含其中存在的表单

citizen/createparty.xhtml

And the folder where i'd like to upload the file is 我要上传文件的文件夹是

partysymbols/ ..

Then i show to you the XHTML code: 然后,我向您展示XHTML代码:

<h:form enctype="multipart/form-data">
     <p:fileUpload value="#{partyCreationBean.file}" mode="simple" />
     <p:commandButton value="Submit" ajax="false" actionListener="#{partyCreationBean.upload}" />

Then the partyCreationBean 然后partyCreationBean

private UploadedFile file;

public UploadedFile getFile() {
    return file;
}

public void setFile(UploadedFile file) {
    this.file = file;
}
....
public void handleFileUpload() {
        File target = new File(FacesContext.getCurrentInstance().getApplication().get);
        System.out.println("handle file upload: " + file.getFileName());
        InputStream inputStream;
        try {
            inputStream = file.getInputstream();
        OutputStream out = new FileOutputStream(file.getFileName()
                );
        int read = 0;
        byte[] bytes = new byte[1024];

        while ((read = inputStream.read(bytes)) != -1) {
            out.write(bytes, 0, read);
        }
        inputStream.close();
        out.flush();
        out.close();
        System.out.println("done");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
}
public void upload() {
    if(file != null) {
        FacesMessage message = new FacesMessage("Succesful", file.getFileName() + " is uploaded.");
        FacesContext.getCurrentInstance().addMessage(null, message);
        handleFileUpload();
    }
}

In my 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>thresholdSize</param-name>
        <param-value>51200</param-value>
    </init-param>
    <init-param>
        <param-name>uploadDirectory</param-name>
        <param-value>partysymbols</param-value>
    </init-param>
</filter>

The problem is that I reach the 问题是我到达了

System.out.println("done") 的System.out.println( “完成”)

but I have no idea of where the file is uploaded. 但我不知道文件上传到哪里。

then also, if i understood well the "uploadDirectory" parameter in the web.xml is not to set the directory where the file is setted. 然后,如果我很好理解web.xml中的“ uploadDirectory”参数,则不要设置文件设置目录。

I don't really understand how to do this stuff, also because it's first time that i work for a webapplication, and i use glassfish, and i have no idea about how the file system should work... I mean... i don't know where in reality are the pages and all the stuff... i just know where they are inside eclipse :/ 我真的不明白该怎么做,还因为这是我第一次为Web应用程序工作,并且我使用glassfish,而且我不知道文件系统应如何工作...我的意思是...我不知道页面和所有东西在哪里...我只知道它们在Eclipse中的位置:/

Thankyou a lot in advance Samuele 提前非常感谢Samuele

I guess there is an error in your handleFileUpload() method: 我猜你的handleFileUpload()方法有错误:

The line 线

OutputStream out = new FileOutputStream(file.getFileName());

should probably be: 应该可能是:

OutputStream out = new FileOutputStream(target.getAbsolutePath() + file.getFileName());

This should also be the path where the file finally is stored, you can print it with: 这也应该是文件最终存储的路径,您可以使用以下命令进行打印:

System.out.println("Path: " + target.getAbsolutePath() + file.getFileName());

The line where the target var is initialized in your code seems to miss something but I guess it retrieves the uploadDirectory param from the web.xml . 在您的代码中初始化target var的行似乎遗漏了一些东西,但是我想它从web.xml检索了uploadDirectory参数。

You may have to set up an absolute path for the uploadDirectory param like "c:\\\\tmp\\\\partysymbols" (Windows) or "/home/user/partysymbols" (Unix) in your web.xml . 您可能需要建立一个绝对路径uploadDirectory PARAM类似"c:\\\\tmp\\\\partysymbols" (Windows)或"/home/user/partysymbols"在你(UNIX) web.xml

See also: 也可以看看:

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

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