2017-09-04 108 views
1

我试图做HTTP.PUT从我的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 -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()); 

但结果是一样的。任何提示?

+1

你尝试'wagon.put(artifact.getFile(),文件名);',但而不是'文件名== targethost/somepath/myfile.bla'使用'filename == targethost/somepath /'? – diginoise

+0

感谢您的评论,我只是从putToStream调用中删除了目的地,它现在按预期工作。 –

回答

0

我所要做的就是从putFromStream通话中移除目标:

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