java对xml压缩

import java.util.*;
import java.util.zip.GZIPOutputStream;
import java.nio.charset.StandardCharsets;
import org.apache.commons.codec.binary.Base64;

/**
     * 模板压缩
     *
     * @param xml 模板xml
     * @return
     * @throws Exception
     */
    public static String businessData(String xml) throws Exception {
        if (StringUtils.isBlank(xml)) {
            return "";
        }
        // xml报文压缩
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try (GZIPOutputStream gzip = new GZIPOutputStream(out)) {
            gzip.write(xml.getBytes(StandardCharsets.UTF_8));
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 压缩后转base64字符串
        return Base64.encodeBase64String(out.toByteArray());
    }

你可能感兴趣的:(java,xml,开发语言)