简体   繁体   中英

Corrupted ZIP file from Resteasy download

Problem is, after the download of a zip file from a REST service I've got something like:

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

The zip file generated is correct (tried directly copying it from the server, the size is 1843 bytes), the fault lies in the download method (resteasy add the HTTP header to the file, so the final size is 1843 bytes + header part). Here is my resteasy implementation (I've removed non influential parts):

@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();
    }

I'm using resteasy 2.1.0 GA (and I cannot upgrade it). The method readFileToByteArray is taken from org.apache.commons.io.FileUtils . I've tried setting the content to text/plain or application/zip and passing a FileInputStream to the Response, but the problem still persist. Any tips?

Oh, I've also tried download via REST method with a simple text file... same problem, at the start of the downloaded file I've got the HTTP response header:

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

edit: integrated comment infos in the question.

No simple solution was found for this issue, as a workaround I've created a servlet to download the file (from the new URL, the zipped file wasn't corrupted), so I've changed my resteasy implementation to do a redirect:

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

where the check parameter is simple encoded token, containing a timestamp, used for simmetric security. It avoided me the full port of authentication/role logic in the servlet (if the servlet URL isn't called in less than 3 seconds from the REST method, the request will return a 400). Not an elegant solution, but at least now the file is no longer corrupted.

PS By the way, just as a note: WinRAR will handle the corrupted file just fine (also 7zip version 9.2), Windows Explorer and the last version of 7zip cannot open it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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