正确的http.put方法

0ejtzxu1  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(364)

我正在努力 HTTP.PUT 从maven插件到目标服务器:

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());

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

iyfjxgzm

iyfjxgzm1#

我所要做的就是把目的地从 putFromStream 电话:

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

相关问题