java.lang.IllegalStateException: getOutputStream() already called

Hi, Im created a service for upload file exactly the way i find in ElFinder.xml in moqui.
When i get file via this service

<service verb="get" noun="file">
        <in-parameters>
            <parameter name="fileId"/>
            <parameter name="form"/>
            <parameter name="name"/>
        </in-parameters>
        <actions>
            <if condition="!fileId">
                <then>
                    <entity-find-one entity-name="moqui.test.File" value-field="fileMp">
                        <field-map field-name="name" from="name"/>
                    </entity-find-one>
                    <if condition="!fileMp">
                        <entity-find-one entity-name="moqui.test.File" value-field="fileMp">
                            <field-map field-name="name" from="'-'+name"/>
                        </entity-find-one>
                    </if>
                </then>
                <else>
                    <entity-find-one entity-name="moqui.test.File" value-field="fileMp">
                        <field-map field-name="fileId" from="fileId"/>
                    </entity-find-one>
                </else>
            </if>
            <script>
                def fileLoc = fileMp.location
                if(fileLoc.indexOf("file://")!=-1){
                fileLoc = fileMp.location.split("file://")[1]
                }
                ec.web.sendResourceResponse(fileLoc)
                return;
            </script>
        </actions>
    </service>

I get above error. why this error happen and how should i fix this?
Thanks

Did you use the REST API (/rest/s1/…) to call the service get#file?

1 Like

Hi, Thanks for reply
Yeah,I used api to call service in forms!

When you use REST API to call a service, after the service run, sendJsonResponse method is called in handleServiceRestCall at WebFacadeImpl.groovy file. In your service, ec.web.sendResourceResponse(fileLoc) make HttpServletResponse send response to the client already, so sendJsonResponse to client again will throw error getOutputStream() already called

2 Likes

thanks.
so there is no need to call ec.web.sendResourceResponse in the service
is that what you mean?

if you call ec.web.sendResourceResponse in service, you must modify handleServiceRestCall method code to ignore call sendJsonResponse.

Thank you !!!

you do like this if you really want to transfer a file from rest api
add a return parameter then read the file, and then assign the content to that parameter.
normally, reading a file from server would be either put a file under the webroot, make it accessible through web server, or handle it in a screen transition, call sendResourceResponse