Struts2 - multipart / related - HttpServletRequest # getParts не работает - PullRequest
0 голосов
/ 08 октября 2018

У меня есть запрос от другого приложения 'multipart/related'.В теле запроса Mutlipart я обновляю image/jpeg и application/xml.Полагаю, мне следует перебрать часть http, полученную из метода getPaths в объекте HttpServletRequest, и прочитать поток, доступный в каждой из частей.Когда я пытаюсь это сделать, я получаю следующую ошибку:

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)

Пожалуйста, дайте мне знать наилучшее из возможных решений для этого.

Вот что я делаю в коде:

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;
}   

}
...