如何阅读RESTFul webservice响应(zip文件作为MediaType)?

时间:2015-12-09 09:20:18

标签: java web-services rest jersey zipfile

当我点击服务URL时,它会给我一个ZIP文件,需要将其移动到某个位置。

请让我知道如何阅读Response中的zip文件。以下是我的代码。

@Path("/folder")
public class FileDownloadService {

@GET
@Path("zipFile")
@Produces("application/zip")
public Response getFile() {
    File f = new File("/home/mpasala/Documents/Example.zip");

    if (!f.exists()) {
        throw new WebApplicationException(404);
    } else {
        Boolean success = moveFile();

    }

    return Response
            .ok(f)
            .header("Content-Disposition",
                    "attachment; filename=server.zip").build();
}

1 个答案:

答案 0 :(得分:0)

InputStream response = webResource.type(MediaType.MULTIPART_FORM_DATA_TYPE).post(InputStream.class,
                    formData);

            OutputStream out = null;
            int read = 0;
            byte[] bytes = new byte[1024];
            File file = new File("D:/Today/");
            if (!file.exists()) {
                file.mkdir();
                out = new FileOutputStream(new File(file + ".zip"));
                while ((read = response.read(bytes)) != -1) {
                    out.write(bytes, 0, read);
                }
                // new File(file +".zip").createNewFile();
            } else {
                out = new FileOutputStream(new File(file+ ".zip"));
                while ((read = response.read(bytes)) != -1) {
                    out.write(bytes, 0, read);
                }
            }

            out.flush();
            out.close();