Nikhil P Kookkiri
2018-10-08 09:06:47 UTC
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;
   }
}
'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
Best regards,
Nikhil P kookkiri
Gtalk: nikhil.pk81
Skype: nikhil.pk81