我想上传一张图片,并在使用JSF2.2(Mojarra 2.2.5),WildFly 8.0.0.Final和PrimeFaces 6.0上传后在同一页面上显示。
文件上传成功,我还将其另外保存在应用程序服务器外的临时文件夹中。
但是,在尝试显示时,我得到了以下的删除:
04:53:12,680 SEVERE [org.primefaces.application.resource.StreamedContentHandler] (default task-4) Error in streaming dynamic resource. null
异常发生在
的第70行org.primefaces.application.resource.StreamedContentHandler.handle()
如下图所示:
exception row exception handling
不幸的是,现有的帖子并没有帮助我解决我的问题:
Error in streaming dynamic resource. null
Display dynamic image from database with p:graphicImage and StreamedContent
所以,我需要帮助!
这是我的xhtml摘录:
<p:fileUpload id="fileUploader" fileUploadListener="#{assignmentBean.upload}" auto="true" dragDropSupport="true" update="graphicImage"
sizeLimit="10000000" fileLimit="3" allowTypes="/(\.|\/)(gif|jpe?g|png)$/" rendered="#{assignmentBean.existing}"/>
<p:graphicImage id="graphicImage" width = "50" height="50" value="#{assignmentBean.uploadedFileAsStream}" /> <!-- rendered="#{not empty assignmentBean.uploadedFileAsStream}" /> -->
这是我的支持bean的摘录:
@ViewScoped
@ManagedBean(name = "assignmentBean")
public class AssignmentBean implements Serializable {
public static final String UPLOAD_FILE_PATH = "C:/Temp/IamOKYouAreOK";
public void upload(FileUploadEvent event) {
if ((this.uploadedFile = event.getFile()) == null)
return;
try(InputStream input = this.uploadedFile.getInputstream()){
String filename = FilenameUtils.getBaseName(uploadedFile.getFileName());
String extension = FilenameUtils.getExtension(uploadedFile.getFileName());
Path targetFile = Paths.get(getUploadSavePath() + "/" + filename + "." + extension);
Files.copy(input, targetFile, StandardCopyOption.REPLACE_EXISTING);
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Successful", event.getFile().getFileName() + " is uploaded"));
} catch (IOException e1) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "ERROR I/O", "Code of the error: DC1"));
e1.printStackTrace();
}
}
private String getUploadSavePath(){
return UPLOAD_FILE_PATH + "/" + "A_" + this.assId;
}
public StreamedContent getUploadedFileAsStream() throws IOException {
FacesContext context = FacesContext.getCurrentInstance();
if (this.uploadedFile != null) {
String filename = FilenameUtils.getBaseName(uploadedFile.getFileName());
String extension = FilenameUtils.getExtension(uploadedFile.getFileName());
Path copiedFile = Paths.get(getUploadSavePath() + "/" + filename + "." + extension);
ByteArrayContent fileContent = new ByteArrayContent(Files.readAllBytes(copiedFile), "img/" + extension);
return fileContent;
}
return null;
}
}