Search code examples
javarestxpageslotus-domino

Receive files in Java bean in XPages


I am creating service that can receive files and attach them to document.

I have created HTML form that is used to submit files (example below).

TEST FORM

<html>
<body>
    <h1>Test-form to upload files (v.1.0.0) </h1>

    <form action="/file.xsp/upload" method="post" enctype="multipart/form-data">
       <p>
        Select a file : <input type="file" name="file" size="50" />
       </p>

       <input type="submit" value="Upload It" />
    </form>
</body>
</html>

Once form is submitted Java bean is triggered and below is example.

JAVA BEAN

public class TestFile extends CustomServiceBean {

    @Override
    public void renderService(CustomService service, RestServiceEngine engine) throws ServiceException {
        print ("START: Uploading of file...");

        HttpServletResponse res = engine.getHttpResponse();
        HttpServletRequest req = engine.getHttpRequest();

        ...

However I do not know how to initialize InputStream to file that is submitted to service. Can anybody suggest (or example) how to do that?

Thanks!


Solution

  • I'm not sure it applies to submit a file to a bean, but I used something similar to the following code to validate uploaded file properties like size.

    In my case the POST was made by an XPage which is probably not your case so maybe it won't apply.

    HttpServletRequest request= engine.getHttpRequest();
    
    @SuppressWarnings("unchecked")
    Map<String,Object> params=request.getParameterMap();
    
    for (Entry<String, Object> tmpEntry : params.entrySet()) {
        if (tmpEntry.getValue() instanceof UploadedFile) {
            UploadedFile myFile=(UploadedFile)tmpEntry.getValue();
            //Do something with the file
        }
    }