Apache Nutch is an extensible and scalable web crawler
Fix for NUTCH-1950 File name too long contributed by xzjh <[email protected]> and Chong Li. This closes #9.
Chris Mattmann8650bd842b35
diff --git a/CHANGES.txt b/CHANGES.txtindex 39ba3155f..d7d6b3c46 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -2,6 +2,8 @@ Nutch Change LogNutch Current Development 1.10-SNAPSHOT+* NUTCH-1950 File name too long (Jiaheng Zhang, Chong Li via mattmann)+* NUTCH-1921 Optionally disable HTTP if-modified-since header (markus)* NUTCH-1933 nutch-selenium plugin (Mo Omer, Mohammad Al-Moshin, lewismc)diff --git a/src/java/org/apache/nutch/tools/FileDumper.java b/src/java/org/apache/nutch/tools/FileDumper.javaindex cb719c90f..314a8d28b 100644--- a/src/java/org/apache/nutch/tools/FileDumper.java+++ b/src/java/org/apache/nutch/tools/FileDumper.java@@ -26,6 +26,7 @@ import java.io.ByteArrayInputStream;import java.util.Arrays;import java.util.HashMap;import java.util.Map;+import java.security.MessageDigest;import org.apache.commons.cli.CommandLine;import org.apache.commons.cli.CommandLineParser;@@ -206,9 +207,32 @@ public class FileDumper {File outputFile = new File(outputFullPath);if (!outputFile.exists()) {LOG.info("Writing: [" + outputFullPath + "]");- FileOutputStream output = new FileOutputStream(outputFile);- IOUtils.write(content.getContent(), output);- fileCount++;+ try {+ FileOutputStream output = new FileOutputStream(outputFile);+ IOUtils.write(content.getContent(), output);+ fileCount++;++ } catch (Exception e) {+ // if the file name is too long, we get the first 32 chars of the original name and append its MD5+ // after the first 32 chars as the new file name+ MessageDigest md = MessageDigest.getInstance("MD5");+ md.update(outputFullPath.getBytes());+ byte[] digest = md.digest();+ StringBuffer sb = new StringBuffer();+ for (byte b : digest) {+ sb.append(String.format("%02x", b & 0xff));+ }+ outputFullPath = outputFullPath.substring(0, 32) + "_" + sb.toString();++ File newOutPutFile = new File(outputFullPath);+ FileOutputStream output = new FileOutputStream(newOutPutFile);+ IOUtils.write(content.getContent(), output);+ fileCount++;+ LOG.info("File name is too long. Truncated and MD5 appended.");++ //e.printStackTrace();+ }+} else {LOG.info("Skipping writing: [" + outputFullPath+ "]: file already exists");