简体   繁体   English

java MultipartEntityBuilder java.lang.LinkageError

[英]java MultipartEntityBuilder java.lang.LinkageError

I have a Java application running under Apache Tomcat 8.0.21.0 on a RHEL 7.8 server.我有一个 Java 应用程序在 RHEL 7.8 服务器上运行在 Apache Tomcat 8.0.21.0 下。 It takes input from a Browser and uploads a file to another Java application running on a remote server.它从浏览器获取输入并将文件上传到另一个在远程服务器上运行的 Java 应用程序。

The partial code is:部分代码为:

// FileItem "fileToUpload" is passed by a HTML input type "file" in a JSP page
private void doPost (HttpServletRequest request, HttpServletResponse response) 
    throws IOException, ServletException
{
    try
    {
        if (ServletFileUpload.isMultipartContent (request))
        {
            FileItemFactory factory = new DiskFileItemFactory ();
            ServletFileUpload upload = new ServletFileUpload (factory);
            List<FileItem> fileItems = upload.parseRequest (request);

            // findFileItem () and generateFileDetails () are local functions
            FileItem fileItemToUpload = findFileItem (fileItems, "fileToUpload");
            String fileDetails = generateFileDetails (fileItemToUpload);

            MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create ();
            entityBuilder.setMode (HttpMultipartMode.BROWSER_COMPATIBLE);
            entityBuilder.addTextBody ("fileDetails", fileDetails);

            InputStream is = fileItemToUpload.getInputStream ();
            entityBuilder.addBinaryBody ("is", is);

            HttpEntity entity = entityBuilder.build ();  // Exception thrown here

            String url = "[remote server URL]";
            CloseableHttpClient httpClient = HttpClients.createDefault ();
            HttpPost httpPost = new HttpPost (url);
            httpPost.setEntity (entity);

            CloseableHttpResponse response = httpClient.execute (httpPost);
            .
            .
            .
            response.close ();
            httpClient.close ();
        }
        else
        {
            /* handle non-multipart POSTs */
        }
    }
    catch (Exception e)
    {
        .
        .
        .
    }
}

The rather verbose Exception:相当冗长的异常:

java.lang.LinkageError: loader constraint violation: when resolving method "org.apache.http.entity.mime.MultipartEntityBuilder.build()Lorg/apache/http/HttpEntity;" java.lang.LinkageError:加载程序约束违规:解析方法时“org.apache.http.entity.mime.build//HttpEntityBuilder;” the class loader (instance of org/apache/catalina/loader/WebappClassLoader) of the current class, uri_test_main/HttpHdlr, and the class loader (instance of java/net/URLClassLoader) for the method's defining class, org/apache/http/entity/mime/MultipartEntityBuilder, have different Class objects for the type er.build()Lorg/apache/http/HttpEntity; the class loader (instance of org/apache/catalina/loader/WebappClassLoader) of the current class, uri_test_main/HttpHdlr, and the class loader (instance of java/net/URLClassLoader) for the method's defining class, org/apache/http/ entity/mime/MultipartEntityBuilder,对于 er.build()Lorg/apache/http/HttpEntity 类型有不同的 Class 对象; used in the signature在签名中使用

is thrown at the line: "HttpEntity entity = entityBuilder.build ();"在以下行抛出:“HttpEntity entity = entityBuilder.build();”

Note: the Exception is consistent across all browser products.注意:例外在所有浏览器产品中都是一致的。

Is there a quick or obvious fix?有快速或明显的解决方法吗? I have several workarounds available, but would prefer not to use the deprecated class MultipartEntity.我有几种可用的解决方法,但不希望使用已弃用的 class MultipartEntity。

This is an application developed under Eclipse IDE configured as a Maven project.这是在 Eclipse IDE 下开发的应用程序,配置为 Maven 项目。 The external JAR httpmime-4.5.6.jar needs to be added to the project's Java Build Path, and the dependency:外部JAR httpmime-4.5.6.jar需要添加到项目的Java Build Path中,依赖:

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpmime</artifactId>
    <version>4.5.6</version>
</dependency>

needs to be added to the dependencies in pom.xml.需要添加到 pom.xml 中的依赖项中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM