我有很多输入字段的表单,加上上传多个文件的primefaces组件"p:fileUpload“当我提交表单时,我无法获得上传的文件。被管理的bean是"RequestScoped“。那么,如何才能在不使manged bean View作用域的情况下获得上传的文件呢?
upload方法
public void upload(FileUploadEvent event) {
try {
FacesMessage msg = new FacesMessage("Success! ", event.getFile().getFileName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, msg);
// Do what you want with the file
String thumbnail = getDestination() + event.getFile().getFileName();
int index = thumbnail.lastIndexOf('.');
SystemFile systemFile = new SystemFile();
systemFile.setAccount(getActor().getAccount());
systemFile.setName(event.getFile().getFileName());
systemFile.setPath(getTalentPath());
systemFile.setFileType(FileUtil.checkFileType(thumbnail.substring(index + 1)));
if (systemFiles == null) {
systemFiles = new ArrayList<>();
}
systemFiles.add(systemFile);
copyFile(event.getFile().getFileName(), event.getFile().getInputstream());
} catch (IOException ex) {
SystemLogger.getLogger(getClass().getSimpleName()).error(null, ex);
}
}
primefaces组件
<p:fileUpload label="#{TalentMessages.lbl_Select_File}" fileUploadListener="#{talentPropertyAction.upload}"
mode="advanced"
multiple="true"
uploadLabel="#{TalentMessages.lbl_upload_File}"
cancelLabel="#{TalentMessages.lbl_cancel_File}"
sizeLimit="2000000"
oncomplete="completeUploadFile(#{talentPropertyAction.talentId});"
/>
然后是save函数
@Setter
@Getter
private List<SystemFile> systemFiles;
try {
// save something else then save the files
if (systemFiles != null) {
System.out.println("Not Null" + systemFiles);
for (SystemFile systemFile : systemFiles) {
TalentPropertyFile talentPropertyFile = new TalentPropertyFile();
talentPropertyFile.setTalentProperty(talentProperty);
talentPropertyFile.setFile(systemFile);
getTalentService().save(getActor().getAccount(), talentPropertyFile);
}
} else {
System.out.println("Null");
}
} catch (InvalidParameter ex) {
SystemLogger.getLogger(getClass().getName()).error(null, ex);
}
发布于 2012-10-16 19:35:43
那么,如何才能在不使manged bean View作用域的情况下获得上传的文件呢?
只需将上传信息立即存储在一个更持久的位置,而不是将其作为请求作用域bean的属性,请求-响应的末尾无论如何都会对其进行清理(注意:每次上传都被视为一个单独的HTTP请求)。
public void upload(FileUploadEvent event) {
// Now, store on disk or in DB immediately. Do not assign to a property.
}
public void save() {
// Later, during submitting the form, just access them from there.
}
如果需要一些键来访问它们,请考虑将键存储在会话作用域中。
https://stackoverflow.com/questions/12913324
复制相似问题