Java 使用 Restful 服务下载 zip 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23057566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 20:21:07  来源:igfitidea点击:

Download a zip file using Restful services

javarestdownloadzip

提问by

Im trying to create and download a zip file in java using restful service. But its not working for me. Please find the code below:

我正在尝试使用restful服务在java中创建和下载一个zip文件。但它对我不起作用。请在下面找到代码:

        @GET
    @Path("/exportZip")
    @Produces("application/zip")
    public Response download(@QueryParam("dim") final String dimId,
            @QueryParam("client") final String clientId,
            @QueryParam("type") final String type,
            @Context UriInfo ui) {
        System.out.println("Start");

        ResponseBuilder response = null ;

        String filetype = "";

        if(type.equalsIgnoreCase("u")){
            filetype = "UnMapped";

        }else if(type.equalsIgnoreCase("m")){
            filetype = "Mapped";

        }
        try {

            byte[] workbook = null;
            workbook = engineService.export(dim, client, type);

            InputStream is = new ByteArrayInputStream(workbook);

            FileOutputStream out = new FileOutputStream(filetype + "tmp.zip");

            int bufferSize = 1024;
            byte[] buf = new byte[bufferSize];
            int n = is.read(buf);
            while (n >= 0) {
                out.write(buf, 0, n);
              n = is.read(buf);
            }

            response = Response.ok((Object) out);

            response.header("Content-Disposition",
                    "attachment; filename=\"" + filetype + " - " + new Date().toString() + ".zip\"");

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


        catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("End");
        return response.build();
    }

This is giving me the following error: javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException: A message body writer for Java class java.io.FileOutputStream, and Java type class java.io.FileOutputStream, and MIME media type application/zip was not found

这给了我以下错误:javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException: A message body writer for Java class java.io.FileOutputStream, and Java type class java.io.FileOutputStream, and未找到 MIME 媒体类型应用程序/zip

采纳答案by Pramod S. Nikam

You haven't added MIME type of your response . Your browser will be confused while processing this response. It needs response content type. To set response content type for your response, add following code ,

您尚未添加响应的 MIME 类型。您的浏览器在处理此响应时会感到困惑。它需要响应内容类型。要为您的响应设置响应内容类型,请添加以下代码,

          response.setContentType("application/zip");

after

          response = Response.ok((Object) out);

thanks.

谢谢。