我有以下内容,无法像预期的那样打印字符串到控制台:
String url = "http://mis.ercot.com/misdownload/servlets/mirDownload?
mimic_duns=000000000&doclookupId=698819309";
URL obj = new URL(url.trim());
HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
conn.getResponseCode();
InputStream stream = conn.getInputStream();
ZipInputStream zis = new ZipInputStream(stream);
byte[] buffer = new byte[1024];
int i = zis.read(buffer);
String str = new String(buffer, StandardCharsets.UTF_8);
System.out.println(str);
URL上有一个zip文件,该文件包含一个XML文件,我最终需要解析该文件,但现在我很满意将其简单地打印到控制台,从而确认我已成功解压缩该文件。
类似以下内容的确会生成文件的正确名称:
ZipEntry ze = zis.getNextEntry();
System.out.println(ze);
因此,我知道我的工作是正确的,但是我不需要文件名,甚至不需要文件,我都不需要XML条目,因为我的最终目标是坚持使用SQL。
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
byte contents[] = new byte[4096];
int direct;
while ((direct = zis.read(contents, 0, contents.length)) >= 0) {
System.out.println(entry.getName() + ", " + direct + " bytes");
for (byte con : contents) {
System.out.print((char) con);
}
}
System.out.println();
}
改编自How to read file from ZIP using InputStream?