Dmytro Pastovenskyi Dmytro Pastovenskyi - 11 months ago 71
reST (reStructuredText) Question

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).


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

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

<input type="submit" value="Upload It" />

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


public class TestFile extends CustomServiceBean {

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?


Answer Source

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

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