java – 如何使用jsf-2和primefaces-3.4提交表单中的上传文件
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 如何使用jsf-2和primefaces-3.4提交表单中的上传文件,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2462字,纯文字阅读大概需要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 – 如何使用jsf-2和primefaces-3.4提交表单中的上传文件全部内容,希望文章能够帮你解决java – 如何使用jsf-2和primefaces-3.4提交表单中的上传文件所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。