HttpClient - 使用 Tomcat Webdav 上传 JAR 文件导致 "invalid or corrupt jarfile"

HttpClient - Upload JAR file using Tomcat Webdav leads to "invalid or corrupt jarfile"

我无法使用 Webdav 和 Apache HTTPClient 上传 JAR 文件,但在我尝试启动它时不会导致 "invalid or corrupt jarfile"。

这是我的设置:

构建 JAR 后使用自定义 Maven 插件(内部使用 HTTP 客户端)上传文件。

如果我尝试使用 HTTP 客户端将文件上传到远程服务器,它会导致损坏。但是如果我使用 curl 命令发送完全相同的文件,我可以毫无问题地启动 Jar

curl -u <user>:<pass> -T <myjar>.jar http://<remotehost>/<myjar>.jar

以下是使用 HTTP 客户端的示例代码:

class FileSender {
    public static void main(String[] args) {
        // [...]
        RequestConfig.Builder cfg = RequestConfig.copy(RequestConfig.DEFAULT);
        cfg = cfg.setConnectTimeout(timeout)
                .setConnectionRequestTimeout(timeout)
                .setSocketTimeout(timeout);

        CredentialsProvider credentialsProvider = authentication.credentials();
        HttpClientBuilder builder = HttpClientBuilder.create()
                .setDefaultRequestConfig(cfg.build())
                .setDefaultCredentialsProvider(credentialsProvider);
        try(CloseableHttpClient client = builder.build()) {
            HttpPut httpPut = new HttpPut("http://<remote>/<myJar>.jar");
            httpPut.setEntity(MultipartEntityBuilder.create()
                    .addBinaryBody("file", new File("path/to/<myJar>.jar"))
                    .build());
            try (CloseableHttpResponse response = client.execute(httpPut)) {
                // Check response HTTP status
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

你有什么可能导致我的问题吗?

编辑:如果我使用 HTTP 客户端和 CURL,MD5 哈希似乎不同,但 CURL 和 FTP 副本共享相同的哈希。

这是使用 HttpClient 上传任何文件的方式:

CloseableHttpClient httpClient = HttpClientBuilder.create().build();

HttpEntity requestEntity = MultipartEntityBuilder.create().addBinaryBody("file", new File("myfile")).build();

HttpPost post = new HttpPost("http://...");
post.setEntity(requestEntity);

try (CloseableHttpResponse response = httpClient.execute(post)) {
    System.out.print(response.getStatusLine());
}

一般使用POST方式上传表单或文件内容

我改这个就解决了

MultipartEntityBuilder.create()
           .addBinaryBody("file", new File("path/to/<myJar>.jar"))
           .build()

由此

new InputStreamEntity(new FileInputStream(new File("path/to/<myJar>.jar)))