gpt4 book ai didi

java - jetty , "No multipart config for servlet"问题

转载 作者:行者123 更新时间:2023-11-30 02:02:05 26 4
gpt4 key购买 nike

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

问题是 getParts() 调用时出现“java.lang.IllegalStateException:No multipart config for 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++));
}
}
}

异常(exception):

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)

这些 jar 是:jetty-server-9.4.8 和 javax-servlet-api-3.10。

这里出了什么问题?

最佳答案

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

     private static final MultipartConfigElement MULTI_PART_CONFIG = new MultipartConfigElement("c:/temp");
...
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String contentType = request.getContentType();

if(contentType != null && contentType.startsWith("multipart/")){
request.setAttribute(Request.__MULTIPART_CONFIG_ELEMENT, MULTI_PART_CONFIG);
for(Part part: request.getParts()) { ... } ;
} else {
...
}
}

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

关于java - jetty , "No multipart config for servlet"问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52514462/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com