報錯信息:fetch of http://szs.mof.gov.cn/zhengwuxinxi/zhengcefabu/201402/t20140224_1046354.html failed with: java.io.IOException: unzipBestEffort returned nullhtml
完整的報錯信息爲:java
2014-03-12 16:48:38,031 ERROR http.Http - Failed to get protocol output java.io.IOException: unzipBestEffort returned null at org.apache.nutch.protocol.http.api.HttpBase.processGzipEncoded(HttpBase.java:317) at org.apache.nutch.protocol.http.HttpResponse.<init>(HttpResponse.java:164) at org.apache.nutch.protocol.http.Http.getResponse(Http.java:64) at org.apache.nutch.protocol.http.api.HttpBase.getProtocolOutput(HttpBase.java:140) at org.apache.nutch.fetcher.Fetcher$FetcherThread.run(Fetcher.java:703) 2014-03-12 16:48:38,031 INFO fetcher.Fetcher - fetch of http://szs.mof.gov.cn/zhengwuxinxi/zhengcefabu/201402/t20140224_1046354.html failed with: java.io.IOException: unzipBestEffort returned null 2014-03-12 16:48:38,031 INFO fetcher.Fetcher - -finishing thread FetcherThread, activeThreads=0
由此可知拋出異常的代碼位於src/plugin/lib-http/src/java/org/apache/nutch/protocol/http/api/HttpBase.java(lib-http插件)類的processGzipEncoded方法的317行:git
byte[] content; if (getMaxContent() >= 0) { content = GZIPUtils.unzipBestEffort(compressed, getMaxContent()); } else { content = GZIPUtils.unzipBestEffort(compressed); } if (content == null) throw new IOException("unzipBestEffort returned null");
nutch1.7\src\plugin\protocol-http\src\java\org\apache\nutch\protocol\http\HttpResponse.java(protocol-http插件)的164行調用了processGzipEncoded方法: github
readPlainContent(in); String contentEncoding = getHeader(Response.CONTENT_ENCODING); if ("gzip".equals(contentEncoding) || "x-gzip".equals(contentEncoding)) { content = http.processGzipEncoded(content, url); } else if ("deflate".equals(contentEncoding)) { content = http.processDeflateEncoded(content, url); } else { if (Http.LOG.isTraceEnabled()) { Http.LOG.trace("fetched " + content.length + " bytes from " + url); } }
經過Firefox的Firebug工具可查看該URL的響應頭爲Content-Encoding:gzip,Transfer-Encoding:chunked。apache
解決方法以下:api
一、修改文件nutch1.7\src\java\org\apache\nutch\metadata\HttpHeaders.java,增長一個field:工具
public final static String TRANSFER_ENCODING = "Transfer-Encoding";
二、修改文件nutch1.7\src\plugin\protocol-http\src\java\org\apache\nutch\protocol\http\HttpResponse.java,替換第160行代碼readPlainContent(in);爲以下代碼fetch
String transferEncoding = getHeader(Response.TRANSFER_ENCODING); if(transferEncoding != null && "chunked".equalsIgnoreCase(transferEncoding.trim())){ readChunkedContent(in, line); }else{ readPlainContent(in); }
三、http內容長度限制不能使用負值,只能使用一個大整數:ui
<property> <name>http.content.limit</name> <value>655360000</value> </property>
四、由於修改了核心代碼和插件代碼,因此須要從新編譯打包發佈,執行nutch1.7\build.xml的默認target:runtime url
cd nutch1.7 ant
提交BUG: