【问题标题】:How to upload and save an attachment via XPages Java Bean如何通过 XPages Java Bean 上传和保存附件
【发布时间】:2013-06-12 19:00:24
【问题描述】:

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

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

那看起来像什么?我希望能够将文件上传控件绑定到 bean。将附件保存到“任何”文档...无论是当前文档还是外部文档.. bean 应该能够处理该逻辑。

我想我不知道如何将该文件附件放入内存中的 bean 以便能够对它进行任何操作,例如保存到文档中。

任何建议将不胜感激。

更新:这是一个类似的问题:How to store uploaded file to local file system using xPages upload control?

但在那个问题中,用户想要保存到本地磁盘。我正在寻找保存到文档中。

谢谢!

【问题讨论】:

    标签: java xpages


    【解决方案1】:

    您需要使用 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 数据的函数(例如保存函数)中,您可以通过检查对象是否为空来检查文件是否已上传。如果不为空,则表示已上传文件。

    要处理上传的文件,首先使用 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() );
            }
    
    
        }
     }
    

    【讨论】:

    • 这是一种非常酷的方法。我使用相同的重复控件,我可以直接在我的 Java bean 中处理每个上传;-)
    • 我不太清楚如何将文件上传控件绑定到托管 bean?
    • 喜欢这个per.lausten.dk/blog/2012/02/…。使用我提到的类添加 getter 和 setter,并使用表达式语言将其绑定到文件上传控件:“#{beanName.propertyName}
    • 我有一个问题,我不确定这是多米诺骨牌 9.0.1 问题还是 windows,或者如果你愿意我可以提出一个新问题......当我这样做时, File.seperator 字符是一个分号“;”。当文档保存在文档中时,这会更改文件名。我也无法手动放置斜杠或反斜杠。有人知道解决方法吗?
    【解决方案2】:

    我有处理 Java 中上传文件的代码。该文件使用普通的 fileUpload 控件上传,然后我从一个按钮调用以下 Java 代码(执行完全刷新 - 以便保存包含上传文件的文档)。在 Java 代码中,您可以进行任何您想要的检查(文件名、文件大小等):

    public void importFile() {
    
        facesContext = FacesContext.getCurrentInstance();
        ExternalContext externalContext = facesContext.getExternalContext();
    
        // get a handle an the uploaded file
        HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
        String fileUploadID = JSFUtil.findComponent("uploadFile").getClientId(FacesContext.getCurrentInstance());
        UploadedFile uploadedFile = ((UploadedFile) request.getParameterMap().get(fileUploadID));
    
        if (uploadedFile == null) {
            facesContext.addMessage("messages1", new FacesMessage(FacesMessage.SEVERITY_ERROR, "No file uploaded. Use the file upload button to upload a file.", ""));
            return;
        }
    
        File file = uploadedFile.getServerFile();
    
        String fileName = uploadedFile.getClientFileName();
    
        // Check that filename ends with .txt
        if (!fileName.endsWith(".txt")) {
            facesContext.addMessage("messages1", new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error in uploaded file. The file must end with .txt", ""));
            return;
        }
    
        try {
            // Open the file
            BufferedReader br;
    
            br = new BufferedReader(new FileReader(file));
            String strLine;
    
            // Read File Line By Line
            while ((strLine = br.readLine()) != null) {
                // do stuff with the contents of the file
            }
    
            // Close the input stream
            br.close();
    
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            facesContext.addMessage("messages1", new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error in uploaded file. Please check format of file and try again", ""));
            return;
        }
    
        facesContext.addMessage("messages1", new FacesMessage(FacesMessage.SEVERITY_INFO, "File successfully uploaded", ""));
    }
    

    通过文件对象的句柄,您可以使用 embedObject 将文件存储在其他文档中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多