java - zip 格式的 ReSTLet POST 响应

标签 java zip content-type restlet representation

我正在对 reSTLet 进行 POST 操作,并且需要返回一个 zip 文件。但是,尽管创建的文件是 zip,该方法却返回乱码。

我尝试按照建议包装 FileRepresentation here:

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

还尝试添加一个 Produces 注释,如下所示:

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

但这两者都不起作用。表示形式返回为乱码字符串,并且 Content-Type header 仍为 application/octet-stream。我缺少什么?

这些是请求 header 。请注意 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

响应 header :

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,但返回值仍然是乱码。

最佳答案

正确的方法是您使用的方法:

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 详细模式的了解:

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/ .

希望对你有帮助 蒂埃里

关于java - zip 格式的 ReSTLet POST 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30081733/

相关文章:

java - 动态饼图

html - 音频内容类型 aacp

c# - 如何拦截来自 WCF 客户端的 header ...?

python - 比较 Pandas 系列中的前一个值和下一个值

php - ZipArchive() 向存档添加空字节的奇怪行为

ios - 找不到 minizip/unzip.h 文件

java - 如何强制 Firefox 使用 java web start 打开页面?

java - 不同人的 jar 尺寸不同

java - 按钮 onClick 调用 Java 方法直到出现特定结果

java - jsp中如何将对象类型转换为int