簡體   English   中英

Primefaces上傳文件的問題

[英]issue with Upload File with Primefaces

我正在嘗試在我的webApplication中插入下載內容。

首先,該頁面包含其中存在的表單

citizen/createparty.xhtml

我要上傳文件的文件夾是

partysymbols/ ..

然后,我向您展示XHTML代碼:

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

然后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();
    }
}

在我的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>

問題是我到達了

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

但我不知道文件上傳到哪里。

然后,如果我很好理解web.xml中的“ uploadDirectory”參數,則不要設置文件設置目錄。

我真的不明白該怎么做,還因為這是我第一次為Web應用程序工作,並且我使用glassfish,而且我不知道文件系統應如何工作...我的意思是...我不知道頁面和所有東西在哪里...我只知道它們在Eclipse中的位置:/

提前非常感謝Samuele

我猜你的handleFileUpload()方法有錯誤:

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

應該可能是:

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

這也應該是文件最終存儲的路徑,您可以使用以下命令進行打印:

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

在您的代碼中初始化target var的行似乎遺漏了一些東西,但是我想它從web.xml檢索了uploadDirectory參數。

您可能需要建立一個絕對路徑uploadDirectory PARAM類似"c:\\\\tmp\\\\partysymbols" (Windows)或"/home/user/partysymbols"在你(UNIX) web.xml

也可以看看:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM