2015-05-06 16 views
2

我正在做POSTrestlet並且需要返回一個zip文件。但是,雖然創建的文件是zip,但該方法返回亂碼。zip格式的Restlet POST響應

我試圖包裹FileRepresentation作爲建議here:

new org.restlet.engine.application.EncodeRepresentation(org.restlet.data.Encoding.ZIP, representation); 

而且還嘗試添加A產生的註釋是這樣的:

@Produces({"application/x-zip-compressed"}) 

但無論是作品。該表示形式返回爲亂碼字符串,並且Content-Type標題保持爲application/octet-stream。我錯過了什麼?

這些是請求標頭。注意Accept-Encoding: gzip, deflate

User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 
Origin: chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo 
Content-Type: application/json 
Accept: */* 
Accept-Encoding: gzip, deflate 
Accept-Language: en-US,en;q=0.8,he;q=0.6 

的響應頭:

Vary: Accept-Encoding 
Last-Modified: Wed, 06 May 2015 14:49:03 GMT 
Content-Disposition: attachment; filename=_backup_20150506.zip; size=162191 
Date: Wed, 06 May 2015 14:49:03 GMT 
Accept-Ranges: bytes 
Server: Restlet-Framework/2.2.1 
Vary: Accept-Charset, Accept-Encoding, Accept-Language, Accept 
Set-Cookie: JSESSIONID=5F10BBBDC58D5C3D6C0474FA12C44FB9; Path=/AppName/; Domain=localhost 
Content-Encoding: gzip 
Content-Type: application/octet-stream 
Transfer-Encoding: chunked 

編輯:

MediaType mt = MediaType.APPLICATION_ZIP; 
FileRepresentation fr = new FileRepresentation(file, mt); 

的響應內容類型更改爲Content-Type: application/zip:我也嘗試過創建表示在改變媒體類型但返回的值仍然是一個亂碼字符串。

+0

您好埃迪,你能不能準確,如果您使用的Restlet的JAX-RS擴展? –

+0

我嘗試使用Produces註解(javax.ws.rs.Produces),但沒有解決問題。 – Eddy

回答

1

做正確的做法是,你用什麼:

public class MyServerResource extends ServerResource { 
    @Post 
    public Representation test(Representation repr) { 
     FileRepresentation outputRepresentation 
      = new FileRepresentation(new File("(...)"), 
          MediaType.APPLICATION_ZIP); 
     return outputRepresentation; 
    } 
} 

所以這應該工作。

使用curl這樣的代碼,這裏是我有:

$ curl -X POST http://localhost:8182/test > mycontent.zip 
$ unzip mycontent.zip 
Archive: mycontent.zip 
extracting: test.txt 

另外,這裏是我有捲曲的詳細模式:

curl -X POST --verbose http://localhost:8182/test 
* Hostname was NOT found in DNS cache 
* Trying 127.0.0.1... 
* Connected to localhost (127.0.0.1) port 8182 (#0) 
> POST /test HTTP/1.1 
> User-Agent: curl/7.35.0 
> Host: localhost:8182 
> Accept: */* 
> 
< HTTP/1.1 200 OK 
< Content-type: application/zip 
< Last-modified: Thu, 07 May 2015 08:08:59 GMT 
< Content-length: 134 
* Server Restlet-Framework/2.3.1 is not blacklisted 
< Server: Restlet-Framework/2.3.1 
< Accept-ranges: bytes 
< Vary: Accept-Charset, Accept-Encoding, Accept-Language, Accept 
< Date: Thu, 07 May 2015 08:19:26 GMT 
< 

注意,您可以使用標題Disposition如果您想在瀏覽器的下載對話框中配置提示。

否則,「在Resltet上啓用JSON響應實體的GZIP壓縮」對應於由Restlet自動壓縮整個響應內容。瀏覽器支持這一點,並可以在顯示內容之前直接解壓縮內容。我不認爲這不是你所需要/期望的。如果是這種情況,您可以對此鏈接感興趣:https://templth.wordpress.com/2015/02/23/optimizing-restlet-server-applications/

希望它可以幫助你, 蒂埃裏

+0

它不起作用。似乎問題可能與自動發送的內容類型有關。不確定。 – Eddy

+0

真奇怪!會對m'y測試項目感興趣嗎?僅供參考我使用標準的Res​​tlet應用程序,而不是JAX-RS Restlet ...如果您使用JAX-RS,我可以嘗試使用此技術。 –