xpage调用java_如何通过XPage Java Bean上传和保存附件

您需要使用com.ibm.xsp.component.UIFileuploadEx.UploadedFile类在bean中创建getter和setter:

private UploadedFile uploadedFile;

public UploadedFile getFileUpload() {

return uploadedFile;

}

public void setFileUpload( UploadedFile to ) {

this.uploadedFile = to;

}

在处理bean数据的函数(例如保存函数)中,您可以通过检查对象是否为空来检查文件是否已上载.如果它不为null,则上载文件.

要处理上载的文件,首先使用getServerFile()方法获取com.ibm.xsp.http.IUploadedFile对象的实例.该对象具有getServerFile()方法,该方法返回上载文件的File对象.该对象的问题在于它具有一个神秘的名称(可能是为了处理多个人同时上传具有相同名称的文件).可以使用IUploadedFile类的getClientFileName()方法检索原始文件名.

我接下来要做的是将隐秘文件重命名为其原始文件名,处理它(将其嵌入富文本字段或使用它执行其他操作),然后将其重命名为其原始(隐藏)名称.最后一步很重要,因为只有在代码完成后才清理(删除)文件.

以下是上述步骤的示例代码:

import java.io.File;

import com.ibm.xsp.component.UIFileuploadEx.UploadedFile;

import com.ibm.xsp.http.IUploadedFile;

import lotus.domino.Database;

import lotus.domino.Document;

import lotus.domino.RichTextItem;

import com.ibm.xsp.extlib.util.ExtLibUtil; //only used here to get the current db

public void saveMyBean() {

if (uploadedFile != null ) {

//get the uploaded file

IUploadedFile iUploadedFile = uploadedFile.getUploadedFile();

//get the server file (with a cryptic filename)

File serverFile = iUploadedFile.getServerFile();

//get the original filename

String fileName = iUploadedFile.getClientFileName();

File correctedFile = new File( serverFile.getParentFile().getAbsolutePath() + File.separator + fileName );

//rename the file to its original name

boolean success = serverFile.renameTo(correctedFile);

if (success) {

//do whatever you want here with correctedFile

//example of how to embed it in a document:

Database dbCurrent = ExtLibUtil.getCurrentDatabase();

Document doc = dbCurrent.createDocument();

RichTextItem rtFiles = doc.createRichTextItem("files");

rtFiles.embedObject(lotus.domino.EmbeddedObject.EMBED_ATTACHMENT, "", correctedFile.getAbsolutePath(), null);

doc.save();

rtFiles.recycle();

doc.recycle();

//if we're done: rename it back to the original filename, so it gets cleaned up by the server

correctedFile.renameTo( iUploadedFile.getServerFile() );

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值