从一个https的url获取zip压缩包,压缩包里面是多个excel文件,excel文件包含两个不同的sheet,字段不一样,要解析成java对象。
excel文件是要密码的,代码中的url获取的压缩包中的exce密码是91440300555442072M。
目前能从url中获取流,但是转为zipInputStream后,总是为空。
URL url = new URL("https://pubapi.holytax.com/pre/xz/sstfpgj?uid=764a332f16d1291ad2fd1d6c08d177ff");
SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
// 创建SSLContext对象,并使用我们指定的信任管理器初始化
TrustManager[] tm = {new MyX509TrustManager()};
sslContext.init(null, tm, new java.security.SecureRandom());
HostnameVerifier ignoreHostnameVerifier = (s, sslsession) -> {
log.warn("Hostname is not matched for cert.");
return true;
};
HttpsURLConnection.setDefaultHostnameVerifier(ignoreHostnameVerifier);
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
httpsURLConnection.setConnectTimeout(6000);
httpsURLConnection.setReadTimeout(6000);
int code = httpsURLConnection.getResponseCode();
if (code != HttpURLConnection.HTTP_OK) {
throw new Exception("文件读取失败");
}
ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(httpsURLConnection.getInputStream()), Charset.forName("GBK"));
// 这里每次读取都是空???
while (zipInputStream.getNextEntry() != null) {
}
访问url直接返回的是一个zip的压缩文件?
感觉不是代码的问题了,你这个压缩包下载到本地测试都不行,我换了个压缩包又可以了。。。你可以看下这个
import net.lingala.zip4j.io.inputstream.ZipInputStream;
ZipInputS tream zipInputStream = new ZipInputStream(new BufferedInputStream(httpsURLConnection.getInputStream()), Charset.forName("GBK"), password);
// 这里每次读取都是空???
while (zipInputStream.getNextEntry() != null) {
}
public static void main(String[] args) {
try {
String url = "https://pubapi.holytax.com/pre/xz/sstfpgj?uid=764a332f16d1291ad2fd1d6c08d177ff";
//根据url读取文件
Connection.Response response = ConnUtil.jsonUtil(url);
byte[] bytes = response.bodyAsBytes();
System.out.println(bytes.length);
//转换数据流
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
File file = new File("D:\\Users\\20064\\idea\\demo5\\temp\\1.zip");
FileOutputStream fileOutputStream = new FileOutputStream(file);
IOUtils.copy(byteArrayInputStream, fileOutputStream);
//使用zip4j解压文件
ZipFile zipFile = new ZipFile(file);
System.out.println(zipFile.isEncrypted());
zipFile.setPassword("91440300555442072M".toCharArray());
zipFile.extractAll("temp");
//读取excel,解析成对象
} catch (Exception e) {
e.printStackTrace();
}
}