使用Restful服务下载zip文件 [英] Download a zip file using Restful services

查看:713
本文介绍了使用Restful服务下载zip文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用restful service在java中创建和下载zip文件。但它不适合我。请找到以下代码:

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:

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

这给了我以下错误:
javax.ws。 rs.WebApplicationException:com.sun.jersey.api.MessageException:Java类java.io.FileOutputStream的消息体编写器,Java类型类java.io.FileOutputStream,未找到MIME媒体类型application / zip

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

推荐答案

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

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 ,

          response.setContentType("application/zip");

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

谢谢。

这篇关于使用Restful服务下载zip文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆