gpt4 book ai didi

java - 如何将上传的文件保存在表单中,直到使用 jsf-2 和 primefaces-3.4 提交

转载 作者:搜寻专家 更新时间:2023-11-01 02:29:33 25 4
gpt4 key购买 nike

当我提交表单时,我有很多输入字段加上 primefaces 组件来上传多个文件“p:fileUpload”的表单我无法获取上传的文件..manged bean 是“RequestScoped”。那么如何在不设置managed bean View scope的情况下获取上传的文件呢?

上传方式

    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});"
/>

然后是保存功能

    @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 的属性,请求范围 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.
}

如果您需要一些 key 来访问它们,请考虑将 key 存储在 session 范围内。

关于java - 如何将上传的文件保存在表单中,直到使用 jsf-2 和 primefaces-3.4 提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12913324/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com