First I'll provide a tiny code snippet here:
String path = "".equals(url.getPath()) ? "/" : url.getPath();
java.io.File f = new java.io.File(path);
if (!f.exists()) {
this.code = 404; // http Not Found
return;
}
Here, the URL's type is java.net.URL
,and its value has this format:
file:///directory1/directory2.../filename
the above code works 90% of the time when it's processing general URLs, But it fails when the file name has special characters in it. For example:
/tmp/Marissafolli/Receptionist/Administrative Assistant/Marissa's Resume.txt.txt
URLs like this will report a "404" code, even if they exist. The following version will work for those special cases:
java.io.File f = new java.io.File(url.toURI());
But url.toURI()
is only in j2SE 1.5.0. So I need to use the first version. How can I make it work?