2017-09-04 81 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());