0

What i'm doing is just unzip and upload a zip file ,which can be download from a website, on to a hdfs. And here's the code:

String src="http://corpus.byu.edu/wikitext-samples/text.zip";
String dst = "hdfs://cshadoop1/user/hxy162130/assignment1";
InputStream a = new URL(src).openStream();
System.out.println(a == null);
ZipInputStream in = new ZipInputStream(a);
System.out.println(in == null);
ZipEntry zE = in.getNextEntry();        
System.out.println(zE == null);

As you see, I used openStream method to change the url into inputstream, and then use the inputstream as a parameter of the ZipInputStream.Finally i get an entry from the zipinputStream. But the problem is the getNextEntry method returns a null value, which means the output of my code is false,false,true. And i just can't find where the problem is.

4

1 回答 1

0

对http://corpus.byu.edu/wikitext-samples/text.zip的 HTTP 请求会生成一个301 Moved Permanently新的Location: https://corpus.byu.edu/wikitext-samples/text.zip. 因此,不再有可用的ZIP资源使用 this URL

要遵循重定向,您可以执行以下操作:

import java.net.URL;
import java.net.URLConnection;
import java.io.InputStream;
import java.util.zip.*;


class ReadZipInputStream {

 public static void main(String[] args) throws Exception {

  String src="http://corpus.byu.edu/wikitext-samples/text.zip";
  //301 Moved Permanently: Location:https://corpus.byu.edu/wikitext-samples/text.zip

  URL url = new URL(src);
  URLConnection connection = url.openConnection();
  String redirect = connection.getHeaderField("Location");
  if (redirect != null){
   connection = new URL(redirect).openConnection();
  }

  InputStream a = connection.getInputStream();
  System.out.println(a);

  ZipInputStream in = new ZipInputStream(a);
  System.out.println(in);

  ZipEntry zE = in.getNextEntry();        
  System.out.println(zE);

 }
}
于 2017-09-09T04:25:39.007 回答