Index: trunk/src/org/openstreetmap/josm/io/CachedFile.java
===================================================================
--- trunk/src/org/openstreetmap/josm/io/CachedFile.java	(revision 13730)
+++ trunk/src/org/openstreetmap/josm/io/CachedFile.java	(revision 13731)
@@ -10,4 +10,5 @@
 import java.io.IOException;
 import java.io.InputStream;
+import java.math.BigInteger;
 import java.net.HttpURLConnection;
 import java.net.MalformedURLException;
@@ -16,4 +17,6 @@
 import java.nio.file.Files;
 import java.nio.file.StandardCopyOption;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -439,5 +442,5 @@
             urlStr = urlStr.replaceAll("%<(.*)>", "");
         long age = 0L;
-        long maxAgeMillis = maxAge;
+        long maxAgeMillis = TimeUnit.SECONDS.toMillis(maxAge);
         Long ifModifiedSince = null;
         File localFile = null;
@@ -497,4 +500,5 @@
         String a = urlStr.replaceAll("[^A-Za-z0-9_.-]", "_");
         String localPath = "mirror_" + a;
+        localPath = truncatePath(destDir, localPath);
         destDirFile = new File(destDir, localPath + ".tmp");
         try {
@@ -546,4 +550,31 @@
     }
 
+    private static String truncatePath(String directory, String fileName) {
+        String ret = fileName;
+        if (directory.length() + fileName.length() > 255) {
+            // Windows doesn't support paths longer than 260, leave 5 chars as safe buffer, 4 will be used by ".tmp"
+            // TODO: what about filename size on other systems? 255?
+            if (directory.length() > 191 && Main.isPlatformWindows()) {
+                // digest length + name prefix == 64
+                // 255 - 64 = 191
+                // TODO: use this check only on Windows?
+                throw new IllegalArgumentException("Path " + directory + " too long to cached files");
+            }
+
+            MessageDigest md;
+            try {
+                md = MessageDigest.getInstance("SHA-256");
+                md.update(fileName.getBytes(StandardCharsets.UTF_8));
+                String digest = String.format("%064x", new BigInteger(1, md.digest()));
+                return fileName.substring(0, Math.min(fileName.length(), 32)) + digest.substring(0, 32);
+            } catch (NoSuchAlgorithmException e) {
+                Logging.error(e);
+                // TODO: what better can we do here?
+                throw new IllegalArgumentException("Missing digest algorithm SHA-256", e);
+            }
+        }
+        return fileName;
+    }
+
     /**
      * Attempts to disconnect an URL connection.
