I use this function to detect if my file exists or not. While I have some image stored as .jpg, .JPG, .png, and .PNG. But it always return .jpg or .png as true even if the real file has extension .JPG or .PNG.
After I render it to my webpage it throws an error "Failed to load resource: the server responded with a status of 404 (Not Found)".
public static String getPhotoFileExtension(int empKey){ try{ String[] types = {".jpg",".JPG",".png", ".PNG"}; for(String t : types) { String path = "/"+Common.PHOTO_PATH + empKey + t; File f = new File(Sessions.getCurrent().getWebApp() .getRealPath(path)); if(f.isFile()) return t; } }catch (Exception e) { e.printStackTrace(); } return ""; }
Java is a case-sensitive language, which means that the upper or lower case of letters in your Java programs matter.
The equalsIgnoreCase() method compares two strings, ignoring lower case and upper case differences. This method returns true if the strings are equal, and false if not. Tip: Use the compareToIgnoreCase() method to compare two strings lexicographically, ignoring case differences.
Filenames are Case Sensitive on NTFS Volumes Even though NTFS and the POSIX subsystem each handle case-sensitivity well, 16-bit Windows-based, MS-DOS-based, OS/2-based, and Win32-based applications do not.
Java is case-sensitive because it uses a C-style syntax. In most programming languages, case sensitivity is the norm. Case-sensitive is useful because it lets you infer what a name means based on its case. In Java code, upper letters and lower letters both are represented differently at the lowest level.
So you want to get the real case sensitive names of files stored in your filesystem. Lets imaging we have the following paths:
/testFolder/test.PnG
c:\testFolder\test.PnG
Now lets create some Java File
Objects to each Image File.
// on Linux File f1 = new File("/testFolder/test.png"); File f2 = new File("/testFolder/test.PNG"); File f3 = new File("/testFolder/test.PnG"); f1.exists(); // false f2.exists(); // false f3.exists(); // true // on Windows File f1 = new File("c:\\testFolder\\test.png"); File f2 = new File("c:\\testFolder\\test.PNG"); File f3 = new File("c:\\testFolder\\test.PnG"); f1.exists(); // true f2.exists(); // true f3.exists(); // true
Your problem is that all calls of File
like File.exists
are redirected to the java.io.FileSystem
class that represents real Operating System calls of your File System by the JVM
. So you cannot distinguish on Windows Machines between test.PNG
and test.png
. Neither do Windows itself.
But even on Windows each File has a defined name in the File System that could be for example: test.PnG
. You will see this in your Windows Explorer
or in Command Line if you type dir c:\testFolder
.
So what you can do in Java is use the File.list
method on the parent directory
that results in the Operating System list call for all files in this directory with their real names.
File dir = new File("c://testFolder//"); for(String fileName : dir.list()) System.out.println(fileName); // OUTPUT: test.PnG
or if you prefer File
Objects
File dir = new File("c://testFolder//"); for(File file : dir.listFiles()) System.out.println(file.getName()); // OUTPUT: test.PnG
You can use this to write your own exists
Method that is case sensitive on all operating systems
public boolean exists(File dir, String filename){ String[] files = dir.list(); for(String file : files) if(file.equals(filename)) return true; return false; }
Use it like this:
File dir = new File("c:\\testFolder\\"); exists(dir, "test.png"); // false exists(dir, "test.PNG"); // false exists(dir, "test.PnG"); // true
EDIT: I have to admit that I was wrong. There is a way to get the real name of a File. I always overlooked the method File.getCanonicalPath
.
Again our example: We have that File c:\testFolder\test.PnG
.
File f = new File("c://testFolder//test.png"); System.out.println(f.getCanonicalPath()); // OUTPUT: C:\testFolder\test.PnG
With that knowledge you can write a simple test method for the case sensitive extension without iterating all files.
public boolean checkExtensionCaseSensitive(File _file, String _extension) throws IOException{ String canonicalPath = _file.getCanonicalPath(); String extension = ""; int i = canonicalPath.lastIndexOf('.'); if (i > 0) { extension = canonicalPath.substring(i+1); if(extension.equals(_extension)) return true; } return false; }
Use it like this:
File f = new File("c://testFolder//test.png"); checkExtensionCaseSensitive(f, "png"); // false checkExtensionCaseSensitive(f, "PNG"); // false checkExtensionCaseSensitive(f, "PnG"); // true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With