如何使用 xPages 上传控件将上传的文件存储到本地文件系统? [英] How to store uploaded file to local file system using xPages upload control?

查看:29
本文介绍了如何使用 xPages 上传控件将上传的文件存储到本地文件系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 xPages 上传控件将上传的文件存储到本地磁盘?我的 xPages 上只有一个带有文本字段和 fileUpload 控件的简单表单.(没有绑定到文档,因此我正在访问 xpages 组件以获取它们在提交时的值)如何从我的 Java 代码访问这样上传的文件?我可以从我的 java 代码中获得这个上传控件,所以我有 'XspFileUpload' 对象.但是我看不到如何访问原始 File 对象以将其保存在文件系统上的任何方式......有人可以帮助我吗?

How to store uploaded file to local disk using xPages upload control? I have just a simple form with text field and fileUpload control on my xPages.(there are no binding to document so I'm accessing xpages components to get their values on submit) How can I access such uploaded file from my java code? I can get this upload control from my java code so I have 'XspFileUpload' object. But I cant see any way how to access the raw File object to be able to save it on files system ... Can someone help me with this?

推荐答案

要从上传控件中检索文件,您可以使用这段代码(它的 java 所以您需要将其转换为 ssjs ..)

To retrieve a file from an upload control you can use this piece of code ( its java so you need to convert it to ssjs..)

// get file from httpservletrequest 

HttpServletRequest hsr = (HttpServletRequest) FacesContext      .getCurrentInstance().getExternalContext().getRequest();
fileUploadID = 'XspFileUpload control'.getClientId(FacesContext.getCurrentInstance());
Map<?, ?> map = hsr.getParameterMap();
UploadedFile f = ((UploadedFile) map.get(fileUploadID));

if (f == null) {
  throw new java.lang.Exception("File could not be found");
}

String fileName = f.getServerFileName()
if (super.isValid() && !this.isHidden()) {
   File serverFile = f.getServerFile();
   if (serverFile != null && serverFile.exists()) {
       String dir = serverFile.getParent();
       File tempFile = new File(dir + File.separator + fileName); // create a handle to the file on server 
   }
}

这篇关于如何使用 xPages 上传控件将上传的文件存储到本地文件系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆