Custom XPage file upload handler


<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="" rendered="false">



Java class:

package eu.linqed;

import java.util.Map;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lotus.domino.*;

public class UploadHandler {

private static String FILE_PARAM = "uploadedFile"; // name of the multipart
                                                    // POST param that holds
                                                    // the uploaded file
private static String RT_ITEM_NAME_FILES = "file"; // name of the RT item
                                                    // that will hold the
                                                    // uploaded file

public UploadHandler() {

public static void process() {

    XspHttpServletResponse response = null;
    PrintWriter pw = null;

    UploadedFile uploadedFile = null;
    File correctedFile = null;

    RichTextItem rtFiles = null;
    Document doc = null;

    String fileName = "";

    FacesContext facesContext = FacesContext.getCurrentInstance();

    try {

        ExternalContext extCon = facesContext.getExternalContext();
        response = (XspHttpServletResponse) extCon.getResponse();
        pw = response.getWriter();

        //only HTTP POST is allowed
        HttpServletRequest request = (HttpServletRequest) extCon.getRequest();
        if (!request.getMethod().equalsIgnoreCase("post")) {
            throw (new Exception("only POST is allowed"));

        Database dbCurrent = (Database) resolveVariable("database");

        //set up output object
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", -1);

        //check if we have a file in the POST
        Map map = request.getParameterMap();

        if (!map.containsKey(FILE_PARAM)) {
            throw (new Exception("no file received"));

        //get the file from the request
        uploadedFile = (UploadedFile) map.get(FILE_PARAM);

        if (uploadedFile == null) {
            throw (new Exception("that's not a file!"));

        //store file in a document
        fileName = uploadedFile.getClientFileName();   //original name of the file

        File tempFile = uploadedFile.getServerFile(); // the uploaded file with a cryptic name

        //we rename the file to its original name, so we can attach it with that name
        correctedFile = new + + fileName);
        boolean renamed = tempFile.renameTo(correctedFile);

        if (renamed) {

            //create a document in the current db
            doc = dbCurrent.createDocument();
            doc.replaceItemValue("form", "fFile");

            //attach file to target document
            rtFiles = doc.createRichTextItem(RT_ITEM_NAME_FILES);
            rtFiles.embedObject(lotus.domino.EmbeddedObject.EMBED_ATTACHMENT, "", correctedFile.getAbsolutePath(), null);

            boolean saved =;


        pw.print("add code to return to the upload method here");


    } catch (Exception e) {


        pw.print("add code here to return an error");

        try {
        } catch (IOException e1) {

    } finally {


        try {

            if (rtFiles != null) {
            if (doc != null) {

            if (correctedFile != null) {
                // rename temporary file back to its original name so it's
                // automatically
                // deleted by the XPages engine
        } catch (Exception ee) {


private static Object resolveVariable(String variableName) {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    return facesContext.getApplication().getVariableResolver().resolveVariable(facesContext, variableName);

All code submitted to OpenNTF XSnippets, whether submitted as a "Snippet" or in the body of a Comment, is provided under the Apache License Version 2.0. See Terms of Use for full details.
2 comment(s)Login first to comment...
Christophe MAUDIER
(at 03:10 on 27.03.2020)
hello, i'm trying to use this XSnippet with hcl domino 11 and dropzone. I have these alert messages
line 46 : Unnecessary @SuppressWarnings("unchecked") -
line 82 : Map is a raw type. References to generic type Map<K,V> should be parameterized
line 115 : The value of the local variable saved is not used

Dropzone message : Server responded with 0 code

any idea ??
Steve Cochrane
(at 10:18 on 30.04.2018)
Thanks! For uploading multiple files into one rich text field, would you suggest using dropzone js, or using the back end java to collect them together? Dropzone has methods for processing multiple files, so that may be an option. Any suggestions?