Jetty,“没有针对servlet的多部分配置”问题

时间:2018-09-26 09:31:37

标签: java multipartform-data jetty-9

我正在编写用于文件传输的处理程序。该请求是多部分HTTP消息。该消息是正确的-已在其他服务器上进行了测试。

问题是在getParts()调用上出现“ java.lang.IllegalStateException:servlet的无多部分配置”。

测试代码:

@SuppressWarnings("serial")
@MultipartConfig
@WebServlet(urlPatterns={"/upload"}, name="upload")
public class FilesServlet extends HttpServlet
{
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        resp.setContentType("text/plain");
        PrintWriter out = resp.getWriter();

        int i=0;
        for(Part part: req.getParts())
        {
            out.printf("Got part: name=%s, size=%d%n",part.getName(), part.getSize());
            part.write(String.format("part-%02d.dat",i++));
        }
    }
}

例外:

java.lang.IllegalStateException: No multipart config for servlet
    at org.eclipse.jetty.server.Request.getParts(Request.java:2327)
    at org.eclipse.jetty.server.Request.getParts(Request.java:2314)
    at com.rad.server.servlet.FilesServlet.doPost(FilesServlet.java:31)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:860)
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:535)
    at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:188)
    at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1595)
    at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:188)
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1253)
    at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:168)
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:473)
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1564)
    at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:166)
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1155)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
    at org.eclipse.jetty.server.Server.handle(Server.java:530)
    at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:347)
    at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:256)
    at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:279)
    at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:102)
    at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:124)
    at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:247)
    at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.produce(EatWhatYouKill.java:140)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:708)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:626)
    at java.lang.Thread.run(Thread.java:745)

罐子是:jetty-server-9.4.8和javax-servlet-api-3.10。

这是怎么了?

2 个答案:

答案 0 :(得分:1)

实际上,我没有发现为什么@MultipartConfig注释对我不起作用,但是我在i-net上发现了一种可以正常工作的解决方法:

NULL

我认为该解决方案可能对遇到此类问题的开发人员有用

答案 1 :(得分:0)

为什么在将servlet映射到路径时不进行配置?

handler.addServlet(UploadServlet.class, "/upload/*")
  .getRegistration().setMultipartConfig(
    new MultipartConfigElement("./tmp")
  );

向该servlet发送的每个请求都将使用该属性进行充实