0
我已經做了形式JSF上傳文件失敗
<td>
<h:form enctype="multipart/form-data">
<p:fileUpload fileUploadListener="#{uploadBean.handleFileUpload(event)}"
mode="advanced"
update="messages"
multiple="true"
sizeLimit="2000000"
allowTypes="/(\.|\/)(pdf|doc?x|xls?x)$/"/>
<p:growl id="messages" showDetail="true"/>
</h:form>
</td>
的這一部分,此事件處理程序類:
public class UploadBean {
/** Creates a new instance of UploadBean */
public UploadBean() {
}
private static final int BUFFER_SIZE = 6124;
public void handleFileUpload(FileUploadEvent event) {
ExternalContext extContext = FacesContext.getCurrentInstance().
getExternalContext();
File result = new File(extContext.getRealPath
("//WEB-INF//upload") + "//" + event.getFile().getFileName());
try {
FileOutputStream fileOutputStream = new FileOutputStream(result);
byte[] buffer = new byte[BUFFER_SIZE];
int bulk;
InputStream inputStream = event.getFile().getInputstream();
while (true) {
bulk = inputStream.read(buffer);
if (bulk < 0) {
break;
}
fileOutputStream.write(buffer, 0, bulk);
fileOutputStream.flush();
}
fileOutputStream.close();
inputStream.close();
FacesMessage msg = new FacesMessage("Succesful",
event.getFile().getFileName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, msg);
} catch (IOException e) {
FacesMessage error = new FacesMessage("The files were not uploaded!");
FacesContext.getCurrentInstance().addMessage(null, error);
}
}
}
現在的處理方法,我從網站得到它。我不知道爲什麼這是無法上傳。我看起來沒問題。也許我錯過了什麼?所以控件出現在我的頁面上,我可以選擇文件,但上傳進度條只是快速進行...沒有咆哮通知顯示,當然也沒有上傳文件。 謝謝,
羅因爲每次我這樣做的時候,它崩潰我不包括在我的XML。我不知道爲什麼。我找不到如何在XML文件中添加此過濾器的完整示例。正如我所說,代碼是從一個網站,聲稱它的作品複製...我應該刪除它? – 2012-03-13 10:08:04
我在PF文檔中提到過,使用PF 3.0,在Tomcat 7.X上使用JSF 2.1.4,它適用於我。它崩潰了什麼是錯誤,我想你還需要一些apache公共庫,即commons-io-X.jar,commons-fileupload-X.jar。 – 2012-03-13 10:25:43
@sys_debug found @ SO http://stackoverflow.com/questions/8875818/how-to-use-primefaces-pfileupload-listener-method-is-never-invoked/8880083#8880083 – 2012-03-13 10:36:36