java – 如何使用jsf-2和primefaces-3.4提交表单中的上传文件
作者:互联网
我有许多输入字段和primefaces组件的表单上传多个文件“p:fileUpload”当我提交表单时我无法获取上传的文件..管理的bean是“RequestScoped”.那么如何在不制作manged bean View范围的情况下获取上传的文件呢?
上传方法
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);
}
}
主要组成部分
<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});"
/>
然后是保存功能
@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);
}
解决方法:
So how can I get the uploaded files without making the manged bean View scope?
只需将上传信息立即存储在一个更永久的位置,而不是作为请求范围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.
}
如果您需要某些密钥来访问它们,请考虑将密钥存储在会话范围中.
标签:java,spring,jsf,primefaces,multifile-uploader 来源: https://codeday.me/bug/20190620/1247428.html