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">
            <parameter name="fileId"/>
            <parameter name="form"/>
            <parameter name="name"/>
            <if condition="!fileId">
                    <entity-find-one entity-name="moqui.test.File" value-field="fileMp">
                        <field-map field-name="name" from="name"/>
                    <if condition="!fileMp">
                        <entity-find-one entity-name="moqui.test.File" value-field="fileMp">
                            <field-map field-name="name" from="'-'+name"/>
                    <entity-find-one entity-name="moqui.test.File" value-field="fileMp">
                        <field-map field-name="fileId" from="fileId"/>
                def fileLoc = fileMp.location
                fileLoc = fileMp.location.split("file://")[1]

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

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


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