Discussion:
Struts2 - multipart/related - HttpServletRequest#getParts not working
Nikhil P Kookkiri
2018-10-08 09:06:47 UTC
Permalink
I have request coming from another application which is
'multipart/related'. In the mutlipart request body, I am receiving
image/jpeg and application/xml. I suppose, I should be iterating over
the http part obtained from getPaths method in the HttpServletRequest
object and read the stream available on each of the parts. When I try
doing that, I am getting the following error:

java.lang.IllegalStateException: UT010057: multipart config was not
present on Servlet
ERROR [stderr] (default task-2)     at
io.undertow.servlet//io.undertow.servlet.spec.HttpServletRequestImpl.verifyMultipartServlet(HttpServletRequestImpl.java:523)
ERROR [stderr] (default task-2)     at
io.undertow.servlet//io.undertow.servlet.spec.HttpServletRequestImpl.getParts(HttpServletRequestImpl.java:512)
ERROR [stderr] (default task-2)     at
javax.servlet.api//javax.servlet.http.HttpServletRequestWrapper.getParts(HttpServletRequestWrapper.java:375)

Please let me know the best possible solution for this.

Here is what I am doing in the code:

    public class TestMultiPart extends ActionSupport implements
ServletRequestAware
    {
        private InputStream inputStream;
        private HttpServletRequest request;

    public String execute()
    {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try
        {
            Collection<Part> parts = request.getParts();
            for(Part part : parts)
            {
                String contentType = part.getContentType();
                System.out.println("Content type is: " + contentType);
                File file = getFileToDownload(getContentType(contentType));
                if(file != null)
                {
                    bis = new
BufferedInputStream(request.getInputStream());
                    bos = new BufferedOutputStream(new
FileOutputStream(file));
                    byte[] bytes = new byte[bis.available()];
                    while(bis.read(bytes) > 0)
                    {
                        bos.write(bytes);
                        bos.flush();
                        bytes = new byte[bis.available()];
                    }
                }
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ServletException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if(bis != null)
            {
                try {
                    bis.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(bos != null)
            {
                try {
                    bos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

        inputStream = new
ByteArrayInputStream("200".getBytes(StandardCharsets.UTF_8));
        return SUCCESS;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    @Override
    public void setServletRequest(HttpServletRequest requestObject)
    {
        this.request = requestObject;
    }

}
--
Best regards,
Nikhil P kookkiri

Gtalk: nikhil.pk81
Skype: nikhil.pk81
Yasser Zamani
2018-10-09 05:32:36 UTC
Permalink
This isn't Struts issue but your underlying servlet message. Anyway, what about trying Struts itself file upload feature. Then ask that another app to http post the file to a file upload action.

Regards.
-----Original Message-----
Sent: Monday, October 8, 2018 12:37 PM
Subject: Struts2 - multipart/related - HttpServletRequest#getParts not working
I have request coming from another application which is 'multipart/related'. In
the mutlipart request body, I am receiving image/jpeg and application/xml. I
suppose, I should be iterating over the http part obtained from getPaths method
in the HttpServletRequest object and read the stream available on each of the
java.lang.IllegalStateException: UT010057: multipart config was not present on
Servlet ERROR [stderr] (default task-2)     at
io.undertow.servlet//io.undertow.servlet.spec.HttpServletRequestImpl.verifyMul
tipartServlet(HttpServletRequestImpl.java:523)
ERROR [stderr] (default task-2)     at
io.undertow.servlet//io.undertow.servlet.spec.HttpServletRequestImpl.getParts(
HttpServletRequestImpl.java:512)
ERROR [stderr] (default task-2)     at
javax.servlet.api//javax.servlet.http.HttpServletRequestWrapper.getParts(HttpSer
vletRequestWrapper.java:375)
Please let me know the best possible solution for this.
    public class TestMultiPart extends ActionSupport implements
ServletRequestAware
    {
        private InputStream inputStream;
        private HttpServletRequest request;
    public String execute()
    {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try
        {
            Collection<Part> parts = request.getParts();
            for(Part part : parts)
            {
                String contentType = part.getContentType();
                System.out.println("Content type is: " + contentType);
                File file = getFileToDownload(getContentType(contentType));
                if(file != null)
                {
                    bis = new
BufferedInputStream(request.getInputStream());
                    bos = new BufferedOutputStream(new FileOutputStream(file));
                    byte[] bytes = new byte[bis.available()];
                    while(bis.read(bytes) > 0)
                    {
                        bos.write(bytes);
                        bos.flush();
                        bytes = new byte[bis.available()];
                    }
                }
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ServletException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if(bis != null)
            {
                try {
                    bis.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(bos != null)
            {
                try {
                    bos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        inputStream = new
ByteArrayInputStream("200".getBytes(StandardCharsets.UTF_8));
        return SUCCESS;
    }
    public InputStream getInputStream() {
        return inputStream;
    }
    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }
    public void setServletRequest(HttpServletRequest requestObject)
    {
        this.request = requestObject;
    }
}
--
Best regards,
Nikhil P kookkiri
Gtalk: nikhil.pk81
Skype: nikhil.pk81
---------------------------------------------------------------------
To unsubscribe, e-mail: user-***@struts.apache.org
For additional commands, e-ma
Nikhil P Kookkiri
2018-10-09 06:35:11 UTC
Permalink
Hi Yesser, thanks for your response. Yes, it is the issue with the
servlet. I am suspecting that MutiPartConfig works only for
multipart/form-data and does not work with multipart/related. I passed
the request directly to HttpServlet instead of Sturts mapped Action with
@MultiPartConfig configured and I got the exception:
javax.servlet.ServletException: UT010016: Not a multi part request.
Almost all documentation I saw talks only about multipart/form-data.

I am assuming using Struts rest plugin also will not help in this case.
Kindly let me know.

Best regards,
Nikhil P kookkiri
Post by Yasser Zamani
This isn't Struts issue but your underlying servlet message. Anyway, what about trying Struts itself file upload feature. Then ask that another app to http post the file to a file upload action.
Regards.
-----Original Message-----
Sent: Monday, October 8, 2018 12:37 PM
Subject: Struts2 - multipart/related - HttpServletRequest#getParts not working
I have request coming from another application which is 'multipart/related'. In
the mutlipart request body, I am receiving image/jpeg and application/xml. I
suppose, I should be iterating over the http part obtained from getPaths method
in the HttpServletRequest object and read the stream available on each of the
java.lang.IllegalStateException: UT010057: multipart config was not present on
Servlet ERROR [stderr] (default task-2)     at
io.undertow.servlet//io.undertow.servlet.spec.HttpServletRequestImpl.verifyMul
tipartServlet(HttpServletRequestImpl.java:523)
ERROR [stderr] (default task-2)     at
io.undertow.servlet//io.undertow.servlet.spec.HttpServletRequestImpl.getParts(
HttpServletRequestImpl.java:512)
ERROR [stderr] (default task-2)     at
javax.servlet.api//javax.servlet.http.HttpServletRequestWrapper.getParts(HttpSer
vletRequestWrapper.java:375)
Please let me know the best possible solution for this.
    public class TestMultiPart extends ActionSupport implements
ServletRequestAware
    {
        private InputStream inputStream;
        private HttpServletRequest request;
    public String execute()
    {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try
        {
            Collection<Part> parts = request.getParts();
            for(Part part : parts)
            {
                String contentType = part.getContentType();
                System.out.println("Content type is: " + contentType);
                File file = getFileToDownload(getContentType(contentType));
                if(file != null)
                {
                    bis = new
BufferedInputStream(request.getInputStream());
                    bos = new BufferedOutputStream(new FileOutputStream(file));
                    byte[] bytes = new byte[bis.available()];
                    while(bis.read(bytes) > 0)
                    {
                        bos.write(bytes);
                        bos.flush();
                        bytes = new byte[bis.available()];
                    }
                }
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ServletException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if(bis != null)
            {
                try {
                    bis.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(bos != null)
            {
                try {
                    bos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        inputStream = new
ByteArrayInputStream("200".getBytes(StandardCharsets.UTF_8));
        return SUCCESS;
    }
    public InputStream getInputStream() {
        return inputStream;
    }
    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }
    public void setServletRequest(HttpServletRequest requestObject)
    {
        this.request = requestObject;
    }
}
--
Best regards,
Nikhil P kookkiri
Gtalk: nikhil.pk81
Skype: nikhil.pk81
---------------------------------------------------------------------
Yasser Zamani
2018-10-09 08:47:21 UTC
Permalink
I see Struts REST plugin has a handler for multipart request but it and even the whole Struts framework also talks only about multipart/form-data ;) Unfortunately it seems Struts doesn't know such content type at all. Anyway, some tips can be found in net via googling "UT010016: Not a multi part request".

Regards.
-----Original Message-----
Sent: Tuesday, October 9, 2018 10:05 AM
Subject: Re: Struts2 - multipart/related - HttpServletRequest#getParts not
working
Hi Yesser, thanks for your response. Yes, it is the issue with the servlet. I am
suspecting that MutiPartConfig works only for multipart/form-data and does not
work with multipart/related. I passed the request directly to HttpServlet instead
javax.servlet.ServletException: UT010016: Not a multi part request.
Almost all documentation I saw talks only about multipart/form-data.
I am assuming using Struts rest plugin also will not help in this case.
Kindly let me know.
Best regards,
Nikhil P kookkiri
Post by Yasser Zamani
This isn't Struts issue but your underlying servlet message. Anyway, what about
trying Struts itself file upload feature. Then ask that another app to http post the
file to a file upload action.
Post by Yasser Zamani
Regards.
-----Original Message-----
Sent: Monday, October 8, 2018 12:37 PM
Subject: Struts2 - multipart/related - HttpServletRequest#getParts not working
I have request coming from another application which is
'multipart/related'. In the mutlipart request body, I am receiving
image/jpeg and application/xml. I suppose, I should be iterating over
the http part obtained from getPaths method in the HttpServletRequest
object and read the stream available on each of the parts. When I try doing
java.lang.IllegalStateException: UT010057: multipart config was not
present on Servlet ERROR [stderr] (default task-2)     at
io.undertow.servlet//io.undertow.servlet.spec.HttpServletRequestImpl.
verifyMul
tipartServlet(HttpServletRequestImpl.java:523)
ERROR [stderr] (default task-2)     at
io.undertow.servlet//io.undertow.servlet.spec.HttpServletRequestImpl.
getParts(
HttpServletRequestImpl.java:512)
ERROR [stderr] (default task-2)     at
javax.servlet.api//javax.servlet.http.HttpServletRequestWrapper.getPa
rts(HttpSer
vletRequestWrapper.java:375)
Please let me know the best possible solution for this.
    public class TestMultiPart extends ActionSupport implements
ServletRequestAware
    {
        private InputStream inputStream;
        private HttpServletRequest request;
    public String execute()
    {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try
        {
            Collection<Part> parts = request.getParts();
            for(Part part : parts)
            {
                String contentType = part.getContentType();
                System.out.println("Content type is: " + contentType);
                File file =
getFileToDownload(getContentType(contentType));
                if(file != null)
                {
                    bis = new
BufferedInputStream(request.getInputStream());
                    bos = new BufferedOutputStream(new
FileOutputStream(file));
                    byte[] bytes = new byte[bis.available()];
                    while(bis.read(bytes) > 0)
                    {
                        bos.write(bytes);
                        bos.flush();
                        bytes = new byte[bis.available()];
                    }
                }
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ServletException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if(bis != null)
            {
                try {
                    bis.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(bos != null)
            {
                try {
                    bos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        inputStream = new
ByteArrayInputStream("200".getBytes(StandardCharsets.UTF_8));
        return SUCCESS;
    }
    public InputStream getInputStream() {
        return inputStream;
    }
    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }
    public void setServletRequest(HttpServletRequest requestObject)
    {
        this.request = requestObject;
    }
}
--
Best regards,
Nikhil P kookkiri
Gtalk: nikhil.pk81
Skype: nikhil.pk81
---------------------------------------------------------------------
Т���������������������������������������������������������������������ХF�V�7V'67&�&R�R���âW6W"�V�7V'67&�&T7G'WG2�6�R��&pФf�"FF�F����6����G2�R�
Loading...