当前位置: 首页 > 知识库问答 >
问题:

java MultipartEntityBuilder java.lang.LinkageError

慕容昊焜
2023-03-14

我有一个在RHEL 7.8服务器上的Apache Tomcat8.0.21.0下运行的Java应用程序。它从浏览器获取输入并将文件上传到远程服务器上运行的另一个Java应用程序。

部分代码为:

// 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)
    {
        .
        .
        .
    }
}

相当冗长的异常:

java.lang.LinkageError:加载器约束冲突:解析方法" org . Apache . http . entity . mime . multipartentitybuilder . build()Lorg/Apache/http/http entity;时当前类uri_test_main/HttpHdlr的类加载器(org/Apache/catalina/loader/WebappClassLoader的实例)和方法的定义类org/Apache/http/entity/mime/MultipartEntityBuilder的类加载器(java/net/URLClassLoader的实例)具有不同的er . build()Lorg/Apache/http/http entity类型的类对象;在签名中使用

在以下行引发:" HttpEntity entity = entity builder . build();"

注意:例外情况在所有浏览器产品中都是一致的。

有没有快速或明显的解决方法?我有几个可用的解决方法,但是我不喜欢使用不推荐使用的类MultipartEntity。

共有1个答案

刘奇
2023-03-14

这是在配置为Maven项目的Eclipse IDE下开发的应用程序。需要将外部JARhttpmime-4.5.6.jar添加到项目的JavaBuild Path中,并且依赖项:

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

需要添加到pom.xml.中的依赖项

 类似资料:

相关问答

相关文章

相关阅读