无法在Weblogic服务器上使用Fileupload Multipart

问题描述

在docker上请求Weblogic 12.2.1.4-dev中的servlet部件时遇到阻塞异常(请注意,这在Wildfly服务器上有效)

我正在使用的Java代码是:

import javax.servlet.http.*;

protected void doPut(HttpServletRequest request,HttpServletResponse response) throws IOException {
    ...
    Collection<Part> parts = request.getParts();
    ...
}

server.log中出现错误

<Oct 2,2020 9:17:59,302 AM GMT> <Warning> <HTTP> <BEA-101394> <The exception "The request content-type is not a multipart/form-data" occurred when processing getParameter or getParameterValues from a multipart value of a ServletRequest.> 
javax.servlet.servletexception: The request content-type is not a multipart/form-data
    at weblogic.servlet.utils.fileupload.Multipart.getParts(Multipart.java:158)
    at weblogic.servlet.internal.ServletRequestImpl$RequestParameters.getParts(ServletRequestImpl.java:2497)
    at weblogic.servlet.internal.ServletRequestImpl$RequestParameters.access$3000(ServletRequestImpl.java:2181)
    at weblogic.servlet.internal.ServletRequestImpl.getParts(ServletRequestImpl.java:3652)
    at javax.servlet.http.HttpServletRequestWrapper.getParts(HttpServletRequestWrapper.java:375)

以及google chrome上的Http请求详细信息:

// GENERAL
Request URL: http://172.21.1.1:8310/backoffice/service
Request Method: PUT
Status Code: 400 Bad Request
Remote Address: 172.1.1.1:8310
Referrer Policy: strict-origin-when-cross-origin
// REQUEST HEADERS
Accept: */*
Accept-Encoding: gzip,deflate
Accept-Language: en-BE,en;q=0.9,fr-FR;q=0.8,fr;q=0.7,en-US;q=0.6,es;q=0.5,it;q=0.4
Cache-Control: no-cache
Connection: keep-alive
Content-Length: 647482
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryoCBB96YNI9S3vXob
Cookie: JSESSIONID=XIjomkiRAVxEtEn0qwlILe46arjsphNNibL00t2dHEhj75oc167A!-481127499
Host: 172.1.1.1:8310
Origin: http://172.1.1.1:8310
Pragma: no-cache
Referer: http://172.1.1.1:8310/backoffice/products/edit?id=MA01
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/85.0.4183.121 Safari/537.36
X-Requested-With: XMLHttpRequest
// FORM DATA
subside-velo-pedelec25.pdf: (binary)
actionId: 98c6e900-0289-4bb6-8e98-2b967b2ee363 
windowId: YbVZr0XiCFHxU7K4hUlmBJmLoXwzY6

解决方法

我遇到了同样的问题。

来自 WebLogic 核心库的 FileUpload 的 servlet 实现只允许在“POST”方法下的多部分请求:

  private boolean isMultipart() {
    if (!this.request.getMethod().toLowerCase().equals("post"))
      return false; 
    String contentType = this.request.getContentType();
    if (contentType == null)
      return false; 
    if (contentType.toLowerCase().startsWith("multipart/form-data"))
      return true; 
    return false;
  }

如果这个验证失败,它会抛出一个异常,包括一条与实际错误没有任何关系的消息:

    if (!isMultipart())
      throw new ServletException("The request content-type is not a multipart/form-data"); 

所以我将我的方法从 PUT 切换到 POST。