相关疑难解决方法(0)

Spring REST - 创建.zip文件并将其发送到客户端

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

现在,代码是这样的:(我知道我不应该在spring控制器中完成所有操作,但不关心它,它只是为了测试目的,找到让它工作的方法)

    @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 …
Run Code Online (Sandbox Code Playgroud)

java rest zip spring zipoutputstream

31
推荐指数
3
解决办法
5万
查看次数

如何从PHP服务器获取$ .ajax的zip

我正在尝试在jquery的$ .ajax请求时发送由php服务器生成的zip.

这是我的代码:

PHP:

        $file = tempnam($a_folder_path, "zip");

        $zip = new ZipArchive();
        $zip->open($file, ZipArchive::OVERWRITE);
        $zip->addFile($path_to_json, 'data.json');
        $zip->close();

        rename($file, $file . '.zip');

        $filename = basename($file . '.zip');
        $filepath = $file . '.zip';
        while (ob_get_level()) {
            ob_end_clean();
        }
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control: public, must-revalidate, post-check=0, pre-check=0");
        header("Content-Description: File Transfer");
        header("Content-type: application/octet-stream");
        header("Content-Disposition: attachment; filename=\"".$filename."\"");
        header("Content-Transfer-Encoding: binary");
        header("Content-Length: ".filesize($filepath));
        ob_end_flush();
        echo file_get_contents($filepath);
        //@readfile($filepath);
Run Code Online (Sandbox Code Playgroud)

JavaScript的:

$.ajax(
        {
            url:    myUrl,                
            type:   'POST',                
            data:   {
                "leData"     : "some_data"
            },
            context: document.body,
            cache:  false,
            success: function(data) …
Run Code Online (Sandbox Code Playgroud)

javascript php ajax jquery

4
推荐指数
1
解决办法
5456
查看次数

在没有任何插件的reactjs中下载zip文件

我收到来自rest api的响应,其Content-Typeistext/htmlContent-Encodingis gzip

我尝试使用 blob 下载它

const blob = new Blob([res], { type: 'application/gzip' });
const downloadUrl = URL.createObjectURL(blob);
const a = document.createElement('a'); 
a.href = downloadUrl;
document.body.appendChild(a);
a.click();
Run Code Online (Sandbox Code Playgroud)

但下载的 gz 文件无法打开(似乎已损坏)。有人可以帮我下载 .zip 文件吗reactjs

编辑:需要一个不使用任何外部插件的解决方案

zip gzip reactjs

1
推荐指数
1
解决办法
2万
查看次数

标签 统计

zip ×2

ajax ×1

gzip ×1

java ×1

javascript ×1

jquery ×1

php ×1

reactjs ×1

rest ×1

spring ×1

zipoutputstream ×1