gzip压缩返回体

/**
	 * 条件查询配置项列表,只返回配置项id,编号,名称
	 * @return
	 */
	public void show() throws IOException {
		HttpServletResponse response = ServletActionContext.getResponse();
		// 取得用户的组织机构
		String deptId = super.getLoginDeptId();
		PageDto pageDto = configService.getConfigList(cid,this.getPageInfo(),
				sysCode,projectConfig, prjId,enable,deptId);
		response.setHeader("Content-Encoding","gzip");
		response.setHeader("Content-Type","application/json;charset=UTF-8");
		ServletOutputStream outputStream = response.getOutputStream();
		PageAction pageAction = new PageAction();
		pageAction.setStatus(0);
		pageAction.setTotal(pageDto.getTotalRecord());
		pageAction.setDataList(pageDto.getRecordList());
		outputStream.write(compress(JSON.toJSONString(pageAction)));
	}


	private byte[] compress(String primStr) {
		if (primStr == null || primStr.length() == 0) {
			return null;
		}
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		GZIPOutputStream gzip = null;
		try {
			byte[] bytes = primStr.getBytes(StandardCharsets.UTF_8);
			LOGGER.info("----压缩前数据大小:{}",bytes.length);
			System.out.println("----压缩前数据大小:"+bytes.length);
			gzip = new GZIPOutputStream(out);
			gzip.write(bytes);
			LOGGER.info("----压缩前数据大小:{}",out.size());
			System.out.println("----压缩前数据大小:{}"+out.size());
			System.out.println("压缩比:"+bytes.length/out.size());
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (gzip != null) {
				try {
					gzip.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return out.toByteArray();
	}
在Java中,如果你遇到一个响应压缩文件的情况,比如gzip或deflate格式,通常需要解压后才能读取内容。这里是一个基本的步骤: 1. **添加依赖**:首先确保你的项目中有支持HTTP请求和数据解压缩的库,如`java.net.HttpURLConnection`或者第三方库如Apache HttpClient或OkHttp,并且有对应的解码库,如Guava的`com.google.common.io.BaseEncoding`。 2. **获取连接**:创建一个HTTP连接并设置适当的请求来识别压缩内容,例如`Accept-Encoding: gzip, deflate`。 ```java URL url = new URL("http://example.com/compressedfile.zip"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty("Accept-Encoding", "gzip, deflate"); ``` 3. **检查响应**:检查返回的响应码,如果状态码是200(成功),并且部包含`Content-Encoding`,说明内容是压缩的。 4. **解压缩数据**:根据`Content-Encoding`的值(通常是`gzip`或`deflate`),读取响应输入流并解压缩到临时字节数组。 - 对于gzip,你可以使用`InflaterInputStream`: ```java if ("gzip".equals(connection.getHeaderField("Content-Encoding"))) { InputStream is = connection.getInputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); Inflater decompressor = new Inflater(); byte[] buffer = new byte[8192]; while (true) { int read = is.read(buffer); if (read == -1) break; decompressor.setInput(buffer, 0, read); byte[] outBuffer = new byte[decompressor.getRemaining()]; decompressor.finish(); // 否则数据可能会丢失 decompressor.copyTo(outBuffer); baos.write(outBuffer); } decompressor.end(); // 确保清理资源 is.close(); responseBody = baos.toByteArray(); } ``` 5. **处理解压后的数据**:将解压后的字节数组转换为你想要的数据格式,比如`InputStream`、`String`等。 6. **关闭连接**:记得关闭所有打开的连接资源,释放内存。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值