繁体   English   中英

Resteasy下载损坏的ZIP文件

[英]Corrupted ZIP file from Resteasy download

问题是,从REST服务下载zip文件后,我有类似的东西:

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Disposition: attachment; filename=Report_request_2681.zip
Content-Type: application/octet-stream
Content-Length: 1843
Date: Tue, 24 May 2016 15:24:39 GMT

PK etc etc... my real zip file bytes

生成的zip文件是正确的(尝试直接从服务器复制它,大小为1843字节),故障在于下载方法(resteasy将HTTP头添加到文件中,因此最终大小为1843字节+标题部分) 。 这是我的resteasy实现(我删除了没有影响的部分):

@Produces(MediaType.APPLICATION_OCTET_STREAM)
@Path("/download/{fileName}")
Response getRequestedFile(
        @Context HttpServletRequest httpRequest,
        @PathParam("fileName") String fileName
);

    //... bla bla bla method and authentication stuff

    //Prepare a file object with file to return
    File file = new File(myPath);
    if (!file.exists()) {
        return Response.status(Response.Status.NOT_FOUND).build();
    }

    try {
        return Response.ok(FileUtils.readFileToByteArray(file), MediaType.APPLICATION_OCTET_STREAM_TYPE).header("Content-Disposition", "attachment; filename=\"" + cleanFileName + "\"").build();
    } catch (IOException ex) {
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
    }

我正在使用resteasy 2.1.0 GA (我无法升级它)。 readFileToByteArray方法取自org.apache.commons.io.FileUtils 我已经尝试将内容设置为text / plain或application / zip并将FileInputStream传递给Response,但问题仍然存在。 有小费吗?

哦,我也试过通过REST方法下载一个简单的文本文件...同样的问题,在下载的文件的开头,我有HTTP响应头:

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Disposition: attachment; filename="Report_request_2681"
Content-Type: application/octet-stream
Content-Length: 16550
Date: Wed, 25 May 2016 07:03:20 GMT

...rest of my txt file

编辑:问题中的综合评论信息。

没有找到针对此问题的简单解决方案,因为我已经创建了一个servlet来下载文件(从新URL,压缩文件没有损坏),所以我改变了我的resteasy实现来进行重定向:

....
String redirectPath = StringUtils.substringBefore(httpRequest.getRequestURL().toString(), "restws") + "download?fileName=" + fileName + "&actionId=" + actionRequestId + "&check=" + encodedString;
return Response.seeOther(URI.create(redirectPath)).build();

其中check参数是简单编码标记,包含用于simmetric安全性的时间戳。 它避免了servlet中的完整身份验证/角色逻辑端口(如果在REST方法的不到3秒内没有调用servlet URL,请求将返回400)。 不是一个优雅的解决方案,但至少现在文件不再被破坏。

PS顺便说一句,就像一个注释: WinRAR将处理损坏的文件就好(也就是7zip版本9.2), Windows Explorer7zip的最后一个版本无法打开它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM