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