Reputation: 19
I have a Java application running under Apache Tomcat 8.0.21.0 on a RHEL 7.8 server. It takes input from a Browser and uploads a file to another Java application running on a remote server.
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;" 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; used in the signature
is thrown at the line: "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.
Upvotes: 0
Views: 369
Reputation: 19
This is an application developed under Eclipse IDE configured as a Maven project. The external JAR httpmime-4.5.6.jar needs to be added to the project's Java Build Path, and the dependency:
<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.
Upvotes: 0