对象转成json,由于数据量过大压缩成.json.zip格式

2024-01-07 20:45:54
import cn.hutool.core.util.URLUtil;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletResponse;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;


private void downloadCompressedJsonData(WaveFormVo myObject, HttpServletResponse response) {

        // 创建 ObjectMapper 实例用于转换对象为 JSON 字符串
        ObjectMapper objectMapper = new ObjectMapper();

        // 将对象转换为 JSON 字符串
        String json = null;
        try {
            json = objectMapper.writeValueAsString(myObject);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }

        // 设置响应头信息,告知浏览器这是一个下载的文件
        response.setContentType("application/zip");
        response.setHeader("Content-Disposition", "attachment; filename="+getDownloadFileName("数据.json.zip"));

        // 创建 ByteArrayOutputStream 和 ZipOutputStream 来写入压缩数据
        try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
             ZipOutputStream zipOutputStream = new ZipOutputStream(byteArrayOutputStream)) {

            // 创建一个新的 ZipEntry,这里的文件名是压缩文件内部的文件名
            ZipEntry zipEntry = new ZipEntry("data.json");
            zipOutputStream.putNextEntry(zipEntry);

            // 将 JSON 字符串写入 ZipEntry
            zipOutputStream.write(json.getBytes());

            // 完成 ZipEntry 的添加
            zipOutputStream.closeEntry();

            // 完成所有的 ZIP 文件添加
            zipOutputStream.finish();

            // 将 ByteArrayOutputStream 写入到 HttpServletResponse 的输出流
            response.getOutputStream().write(byteArrayOutputStream.toByteArray());
        } catch (IOException  e) {
            throw new RuntimeException(e);
        }

    }
    /**
     * @doc 获取文件下载名字符串(解决乱码问题)
     * @param fileName
     * @return
     */
    public static String getDownloadFileName(String fileName) {
        String finalFileName = fileName;
        String userAgent = getUserAgent();
        if(StringUtils.contains(userAgent, "MSIE")){//IE浏览器
            finalFileName = URLUtil.encode(fileName, Charset.forName("UTF-8"));
        }else if(StringUtils.contains(userAgent, "Mozilla")){//google,火狐浏览器
            finalFileName = URLUtil.encode(fileName,  Charset.forName("UTF-8")); // StrUtil.str(fileName.getBytes(), "ISO8859-1");
        }else{
            finalFileName = URLUtil.encode(fileName, Charset.forName("UTF-8"));//其他浏览器
        }
        return finalFileName;
    }

    /**
     * 获取UserAgent
     */
    public static String getUserAgent() {
        return getRequestAttributes().getRequest().getHeader("USER-AGENT");
    }
    public static ServletRequestAttributes getRequestAttributes() {
        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        return (ServletRequestAttributes) attributes;
    }

文章来源:https://blog.csdn.net/Mr_Dracy/article/details/135402026
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。