java - 执行 HTTP.PUT 的正确方法

标签 java maven http curl apache-httpclient-4.x

我正在尝试从我的 Maven 插件到目标服务器执行 HTTP.PUT:

private void uploadFile(Wagon wagon, String fileName, Artifact artifact) throws MojoExecutionException {
    if (artifact != null) {
        try {
            //org.apache.maven.wagon
            wagon.put(artifact.getFile(), fileName);

        } catch (TransferFailedException | ResourceDoesNotExistException | AuthorizationException e) {
            throw new MojoExecutionException("failed", e);
        }
    }
}

请求

PUT /somepath/myfile.bla HTTP/1.1
Cache-control: no-cache
Cache-store: no-store
Pragma: no-cache
Expires: 0
Accept-Encoding: gzip
Content-Type: application/json
Authorization: Bearer xxx
Content-Length: 123
Host: targethost
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.3.5 (java 1.5)

回应

HTTP/1.1 404 Not Found
Server: nginx/1.12.1
Date: Thu, 31 Aug 2017 11:45:18 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 69
Connection: keep-alive

这对我来说似乎完全合法,但如您所见,它因 404 而失败。 targethost 的维护者告诉我文件名不能包含在路径中,事实上,如果我用 cURL 这样做:

curl -v -X PUT -T myfile.bla https://targethost/somepath -H "Authorization: Bearer .." --Capath /path/to -k

结果是:

> PUT /somepath HTTP/1.1
> User-Agent: curl/7.38.0
> Host: targethost
> Accept: */*
> Authorization: Bearer ...
> Content-Length: 123
> Expect: 100-continue

我未能按照 targethost 维护者的要求执行 PUT。我也试过

File file = artifact.getFile();
((HttpWagon) wagon).putFromStream(new FileInputStream(file), fileName, file.length(), file.lastModified());

但结果是一样的。有什么提示吗?

最佳答案

我所要做的就是从 putFromStream 调用中删除目标:

((HttpWagon) wagon).putFromStream(new FileInputStream(file), "", file.length(), file.lastModified());

关于java - 执行 HTTP.PUT 的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46035732/

相关文章:

java - Maven EAR 插件,重命名应用程序

java - 由于无法识别选项 : --module-path,在 Netbeans 11 中运行示例 JavaFX Maven 时出错

javascript - 在 Axios OPTIONS 响应中处理状态(例如 503)

python - 如何将这些 header 添加到我的 python urllib opener?

api - 获取或创建的最佳 HTTP 方法

java - Spring Boot 不识别其余存储库

java - ActionBarCompact 内的微调器

java - ArrayList 到 ArrayList<类型>

java - Git Eclipse 将源文件夹导入到项目中

maven - 添加 java 源代码(.java 文件)以在 Maven 中测试 jar