以zip格式的Restlet POST响应

时间:2015-05-06 15:53:09

标签: java zip content-type restlet representation

我正在对POST执行restlet并需要返回一个zip文件。但是虽然创建的文件是zip,但该方法返回乱码。

我尝试按照建议here:

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

还尝试添加这样的Produces注释:

@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,但返回的值仍然是一个乱码字符串。

1 个答案:

答案 0 :(得分:1)

正确使用的方法是:

req

所以这应该有用。

使用curl和这样的代码,这就是我所拥有的:

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

另外,以下是我对卷曲的详细模式的看法:

$ 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 <

否则,&#34;在Resltet&#34;上启用JSON响应实体的GZIP压缩对应于Restlet对整个响应内容的自动压缩。浏览器支持此功能,可以在显示内容之前直接解压缩内容。我不认为这不是你需要/期望的。如果是这种情况,您可能会对以下链接感兴趣:grunt-notify

希望它可以帮到你, 亨利