Java 使用HTTP GET请求提取下载的Zip文件的内容
我必须从rest调用下载一个Zip文件并提取其内容(一些PDF文件和一个PNG文件) 我正在使用JavaSpringJava 使用HTTP GET请求提取下载的Zip文件的内容,java,get,zip,unzip,Java,Get,Zip,Unzip,我必须从rest调用下载一个Zip文件并提取其内容(一些PDF文件和一个PNG文件) 我正在使用JavaSpring 怎么做?请求正文应如下所示: @RequestMapping(value = "/push/{id}", method = RequestMethod.POST, consumes = MediaType.APPLICATION_OCTET_STREAM_VALUE, produces=MediaType.APPLICATION_JSON_VALUE) @ResponseStat
怎么做?请求正文应如下所示:
@RequestMapping(value = "/push/{id}", method = RequestMethod.POST, consumes = MediaType.APPLICATION_OCTET_STREAM_VALUE, produces=MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK)
public String pushTransactions(@PathVariable("id") String id, @RequestBody byte[] str) throws MessagingException, JsonParseException, JsonMappingException, IOException {
在控制器中,您可以执行以下操作:
GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(str));
ByteArrayOutputStream out = new ByteArrayOutputStream();
int len;
while ((len = gis.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
gis.close();
out.close();
您可以使用Spring的RestTemplate下载该文件
RestTemplate templ = new RestTemplate();
byte[] downloadedBytes = templ.getForObject(url, byte[].class);
使用标准java或第三方库提取内容
示例实用程序,根据此处改编:
然后使用此实用程序,如下所示:
ZipHelper.unzip(downloadedBytes, "/path/to/directory");
@RequestBody byte[]srt引发异常:此位置不允许批注。@FrancescoPerfetti再次检查我的示例。我已经编辑了谢谢,我需要解码回应?r认为使用了库?对不起,你说的“r认为”是什么意思?对不起,或者诸如此类的东西:充气减压器=新充气器();decompressor.setInput(文件);ByteArrayOutputStream bos=新的ByteArrayOutputStream(file.length);字节[]buf=新字节[1024];while(!decompressor.finished()){int count=decompressor.inflate(buf);bos.write(buf,0,count);}bos.close();字节[]解压缩数据=bos.toByteArray();我认为ZipInputStream是一种比充气机(zlib)更通用的算法。是否要将zip文件解压缩到磁盘上的目录中?@IgorGanapolsky没有区别。当我们说提取,我们的意思是解压缩。
ZipHelper.unzip(downloadedBytes, "/path/to/directory");