springboot gzip 解压缩代码
时间: 2023-10-14 21:13:41 浏览: 156
可以使用以下代码进行Spring Boot的GZIP解压缩:
```
@GetMapping(value = "/gzip-decompress")
public String decompressGzip(@RequestParam("gzipString") String gzipString) {
try {
byte[] decodedBytes = Base64.getDecoder().decode(gzipString);
ByteArrayInputStream bis = new ByteArrayInputStream(decodedBytes);
GZIPInputStream gzipInputStream = new GZIPInputStream(bis);
InputStreamReader isr = new InputStreamReader(gzipInputStream, StandardCharsets.UTF_8);
BufferedReader br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
```
其中,对于接收到的gzipString,首先需要使用`Base64.getDecoder().decode()`解码为字节数组,然后通过`GZIPInputStream`进行GZIP解压缩,最后将结果封装到StringBuilder中即可。
阅读全文