Struts2-多部分/相关-HttpServletRequest#getParts不起作用

时间:2018-10-08 06:35:42

标签: java struts2 struts multipart servlet-3.0

我有来自另一个应用程序'multipart/related'的请求。在多部分请求正文中,我正在审核image/jpegapplication/xml。我想,我应该遍历从getPaths对象中的HttpServletRequest方法获得的http部分,并读取每个部分上可用的流。当我尝试这样做时,出现以下错误:

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

}

0 个答案:

没有答案