Spring REST - 创建.zip文件并将其发送到客户端 [英] Spring REST - create .zip file and send it to the client

查看:131
本文介绍了Spring REST - 创建.zip文件并将其发送到客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建包含我从后端接收的压缩文件的.zip文件,然后将此文件发送给用户。 2天我一直在寻找答案,无法找到合适的解决方案,也许你可以帮助我:)

I want to create .zip file that contains my zipped files that I recieve from backend, and then send this file to the user. For 2 days I have been looking for the answer and cant find proper solution, maybe you can help me :)

现在,代码是这样的: (我知道我不应该在弹簧控制器中完成所有操作,但不关心它,它只是出于测试目的,找到使其工作的方式)

For now, the code is like this: (I know I shouldnt do it all in the spring controller, but dont care about that, it is just for testing purposes, to find the way to make it works)

    @RequestMapping(value = "/zip")
    public byte[] zipFiles(HttpServletResponse response) throws IOException{
        //setting headers
        response.setContentType("application/zip");
        response.setStatus(HttpServletResponse.SC_OK);
        response.addHeader("Content-Disposition", "attachment; filename=\"test.zip\"");

        //creating byteArray stream, make it bufforable and passing this buffor to ZipOutputStream
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(byteArrayOutputStream);
        ZipOutputStream zipOutputStream = new ZipOutputStream(bufferedOutputStream);

        //simple file list, just for tests
        ArrayList<File> files = new ArrayList<>(2);
        files.add(new File("README.md"));

        //packing files
        for (File file : files) {
            //new zip entry and copying inputstream with file to zipOutputStream, after all closing streams
            zipOutputStream.putNextEntry(new ZipEntry(file.getName()));
            FileInputStream fileInputStream = new FileInputStream(file);

            IOUtils.copy(fileInputStream, zipOutputStream);

            fileInputStream.close();
            zipOutputStream.closeEntry();
        }

        if (zipOutputStream != null) {
            zipOutputStream.finish();
            zipOutputStream.flush();
            IOUtils.closeQuietly(zipOutputStream);
        }
        IOUtils.closeQuietly(bufferedOutputStream);
        IOUtils.closeQuietly(byteArrayOutputStream);
        return byteArrayOutputStream.toByteArray();
    }

但问题是,当我输入URL时使用代码:localhost :8080 / zip我得到文件:test.zip.html而不是.zip文件

But the problem is, that using the code, when I enter URL: localhost:8080/zip I get file: test.zip.html instead of .zip file

我不知道我还能做什么。我也尝试用以下内容替换ByteArrayOuputStream:

I have no idea what else can I do. I was also trying replace ByteArrayOuputStream with something like:

OutputStream outputStream = response.getOutputStream();

并将方法设置为 void ,以便它不返回任何内容,但它创建.zip文件,它被损坏了吗?

and set the method to be void so it returns nothing, but It created .zip file which was.. damaged?

在解压缩 test.zip 后,在我的Macbook上,我得到了 test.zip.cpgz ,这又一次给了我测试.zip文件等等..

On my macbook after unpacking the test.zip I was getting test.zip.cpgz which was again giving me test.zip file and so on..

在Windows上,.zip文件被破坏,因为我说,甚至无法打开它。

On windows the .zip file was damaged as I said and couldn't even open it.

我还想,自动删除.html扩展名将是最佳选择,但如何?
希望它没有像看起来那么难:)谢谢

I also suppose, that removing .html extension automatically will be the best option, but how? Hope it is no as hard as It seems to be :) thanks

推荐答案

似乎已经解决了。我替换了:

seems to be solved. I replaced:

response.setContentType("application/zip");

with:

@RequestMapping(value = "/zip", produces="application/zip")

现在我变得清晰,美丽.zip文件:)

And now I get clear, beautiful .zip file :)

如果你们中的任何人有更好或更快的主张,或者只是想提出一些建议,那就去吧我很好奇。

If any of you have either better or faster proposition, or just want to give some advice, then go ahead, I am curious.

这篇关于Spring REST - 创建.zip文件并将其发送到客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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