如何通过XPage Java Bean上传和保存附件 [英] How to upload and save an attachment via XPages Java Bean

查看:112
本文介绍了如何通过XPage Java Bean上传和保存附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解如何使用表达式语言将XPages控件绑定到Java Bean。然后它会自动访问setter和getter。

I get how you can use Expression Language to bind XPages controls to a Java Bean. Then it accesses the setters and getters automatically.

但是你如何处理文件附件?

But how do you handle a file attachment?

这是什么样的?我希望能够将文件上传控件绑定到bean。将附件保存到无论文档中......无论是当前文档还是外部文档...... bean应该能够处理该逻辑。

What does that look like? I'd like to be able to I guess bind the file upload control to the bean. Save the attachment to "whatever" doc... whether it's the current or external document.. the bean should be able to handle that logic.

我想我不知道知道如何将文件附件放入内存bean中,以便能够像保存到文档那样对它做任何事情。

I guess I don't know how to get that file attachment into the in memory bean to be able to do anything with it like saving to a document.

任何建议都将受到赞赏。

any advice would be appreciated.

更新:这是一个类似的问题:如何使用xPages上传控件将上传的文件存储到本地文件系统?

Update: This is a similar question to this: How to store uploaded file to local file system using xPages upload control?

但在该问题中,用户想要保存到本地光盘。我希望保存到文档。

But in that question the user wants to save to local disc. I'm looking to save to a document.

谢谢!

推荐答案

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

You need to create a getter and setter in the bean using the com.ibm.xsp.component.UIFileuploadEx.UploadedFile class:

private UploadedFile uploadedFile;

public UploadedFile getFileUpload() {
    return uploadedFile;
}
public void setFileUpload( UploadedFile to ) {
    this.uploadedFile = to;
}

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

In the function that processes the bean data (e.g. a save function) you can check if a file was uploaded by checking if the object is null. If it's not null, a file was uploaded.

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

To process that uploaded file, first get an instance of a com.ibm.xsp.http.IUploadedFile object using the getServerFile() method. That object has a getServerFile() method that returns a File object for the uploaded file. The problem with that object is that it has a cryptic name (probably to deal with multiple people uploading files with the same name at the same time). The original file name can be retrieved using the getClientFileName() method of the IUploadedFile class.

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

What I then tend to do is to rename the cryptic file to its original file name, process it (embed it in a rich text field or do something else with it) and then rename it back to its original (cryptic) name. This last step is important because only then the file is cleaned up (deleted) after the code is finished.

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

Here's the sample code for the steps above:

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() );
        }


    }
 }

这篇关于如何通过XPage Java Bean上传和保存附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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