Guest User

Untitled

a guest
Nov 23rd, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.22 KB | None | 0 0
  1. public static String getPhotoFileExtension(int empKey){
  2. try{
  3. String[] types = {".jpg",".JPG",".png", ".PNG"};
  4. for(String t : types)
  5. {
  6. String path = "/"+Common.PHOTO_PATH + empKey + t;
  7. File f = new File(Sessions.getCurrent().getWebApp()
  8. .getRealPath(path));
  9. if(f.isFile())
  10. return t;
  11. }
  12. }catch (Exception e) {
  13. e.printStackTrace();
  14. }
  15. return "";
  16. }
  17.  
  18. // on Linux
  19. File f1 = new File("/testFolder/test.png");
  20. File f2 = new File("/testFolder/test.PNG");
  21. File f3 = new File("/testFolder/test.PnG");
  22. f1.exists(); // false
  23. f2.exists(); // false
  24. f3.exists(); // true
  25.  
  26. // on Windows
  27. File f1 = new File("c:\testFolder\test.png");
  28. File f2 = new File("c:\testFolder\test.PNG");
  29. File f3 = new File("c:\testFolder\test.PnG");
  30. f1.exists(); // true
  31. f2.exists(); // true
  32. f3.exists(); // true
  33.  
  34. File dir = new File("c://testFolder//");
  35. for(String fileName : dir.list())
  36. System.out.println(fileName);
  37. // OUTPUT: test.PnG
  38.  
  39. File dir = new File("c://testFolder//");
  40. for(File file : dir.listFiles())
  41. System.out.println(file.getName());
  42. // OUTPUT: test.PnG
  43.  
  44. public boolean exists(File dir, String filename){
  45. String[] files = dir.list();
  46. for(String file : files)
  47. if(file.equals(filename))
  48. return true;
  49. return false;
  50. }
  51.  
  52. File dir = new File("c:\testFolder\");
  53. exists(dir, "test.png"); // false
  54. exists(dir, "test.PNG"); // false
  55. exists(dir, "test.PnG"); // true
  56.  
  57. File f = new File("c://testFolder//test.png");
  58. System.out.println(f.getCanonicalPath());
  59. // OUTPUT: C:testFoldertest.PnG
  60.  
  61. public boolean checkExtensionCaseSensitive(File _file, String _extension) throws IOException{
  62. String canonicalPath = _file.getCanonicalPath();
  63. String extension = "";
  64. int i = canonicalPath.lastIndexOf('.');
  65. if (i > 0) {
  66. extension = canonicalPath.substring(i+1);
  67. if(extension.equals(_extension))
  68. return true;
  69. }
  70. return false;
  71. }
  72.  
  73. File f = new File("c://testFolder//test.png");
  74. checkExtensionCaseSensitive(f, "png"); // false
  75. checkExtensionCaseSensitive(f, "PNG"); // false
  76. checkExtensionCaseSensitive(f, "PnG"); // true
  77.  
  78. public static boolean fileExistsCaseSensitive(String path) {
  79. try {
  80. File file = new File(path);
  81. return file.exists() && file.getCanonicalFile().getName().equals(file.getName());
  82. } catch (IOException e) {
  83. return false;
  84. }
  85. }
  86.  
  87. import java.io.File;
  88. import java.util.Collection;
  89. import java.util.Optional;
  90.  
  91. import org.apache.commons.io.FileUtils;
  92. import org.apache.commons.io.filefilter.IOFileFilter;
  93.  
  94. public class CaseInsensitiveFileFinder {
  95.  
  96. /**
  97. * Attempts to find a file with the given <code>fileName</code> (irrespective of case) in the given
  98. * <code>absoluteDirPath</code>. Note that while this method is able to find <code>fileName</code> ignoring case, it
  99. * may not be able to do so if <code>absoluteDirPath</code> is in an incorrect case - that behavior is OS dependent.
  100. *
  101. * @param absoluteDirPath the absolute path of the parent directory of <code>fileName</code> (e.g. "/Users/me/foo")
  102. * @param fileName the name of the file including extension that may or may not be the correct case
  103. * (e.g. myfile.txt)
  104. * @return an optional reference to the file if found, {@link Optional#empty()} will be returned if the file is not
  105. * found
  106. */
  107. public Optional<File> findFileIgnoreCase(String absoluteDirPath, final String fileName) {
  108.  
  109. File directory = new File(absoluteDirPath);
  110. if (!directory.isDirectory()) {
  111. throw new IllegalArgumentException("Directory '" + absoluteDirPath + "' isn't a directory.");
  112. }
  113. IOFileFilter caseInsensitiveFileNameFilter = new IOFileFilter() {
  114. @Override
  115. public boolean accept(File dir, String name) {
  116. boolean isSameFile = fileName.equalsIgnoreCase(name);
  117. return isSameFile;
  118. }
  119.  
  120. @Override
  121. public boolean accept(File file) {
  122. String name = file.getName();
  123. boolean isSameFile = fileName.equalsIgnoreCase(name);
  124. return isSameFile;
  125. }
  126. };
  127. Collection<File> foundFiles = FileUtils.listFiles(directory, caseInsensitiveFileNameFilter, null);
  128. if (foundFiles == null || foundFiles.isEmpty()) {
  129. return Optional.empty();
  130. }
  131. if (foundFiles.size() > 1) {
  132. throw new IllegalStateException(
  133. "More requirements needed to determine what to do with more than one file. Pick the closest match maybe?");
  134. }
  135. // else exactly one file
  136. File foundFile = foundFiles.iterator().next();
  137. return Optional.of(foundFile);
  138. }
  139. }
  140.  
  141. import static org.junit.Assert.assertEquals;
  142. import static org.junit.Assert.assertFalse;
  143. import static org.junit.Assert.assertTrue;
  144.  
  145. import java.io.File;
  146. import java.io.IOException;
  147. import java.util.Optional;
  148.  
  149. import org.apache.commons.io.FileUtils;
  150. import org.apache.commons.lang.StringUtils;
  151. import org.junit.AfterClass;
  152. import org.junit.BeforeClass;
  153. import org.junit.Test;
  154.  
  155. import com.google.common.io.Files;
  156.  
  157. /**
  158. * Non-quite-unit tests for {@link CaseInsensitiveFileFinder} class.
  159. */
  160. public class CaseInsensitiveFileFinderTest {
  161.  
  162. private static String APPENDABLE_NEW_TMP_DIR_PATH;
  163.  
  164. /**
  165. * Create the files with different cases.
  166. * @throws IOException
  167. */
  168. @BeforeClass
  169. public static void setup() throws IOException {
  170. File newTmpDir = Files.createTempDir();
  171. String newTmpDirPath = newTmpDir.getCanonicalPath();
  172. final String appendableNewTmpDirPath;
  173. String fileSeparator = System.getProperty("file.separator");
  174. if (!newTmpDirPath.endsWith(fileSeparator)) {
  175. appendableNewTmpDirPath = newTmpDirPath + fileSeparator;
  176. }
  177. else {
  178. appendableNewTmpDirPath = newTmpDirPath;
  179. }
  180. CaseInsensitiveFileFinderTest.APPENDABLE_NEW_TMP_DIR_PATH = appendableNewTmpDirPath;
  181.  
  182. File foofileDotPng = new File(appendableNewTmpDirPath + "FOOFILE.PNG");
  183. Files.touch(foofileDotPng);
  184. assertTrue(foofileDotPng.isFile());
  185. File barfileDotJpg = new File(appendableNewTmpDirPath + "BARFILE.JPG");
  186. Files.touch(barfileDotJpg);
  187. assertTrue(barfileDotJpg.isFile());
  188. }
  189.  
  190. @AfterClass
  191. public static void teardown() throws IOException {
  192. File newTmpDir = new File(CaseInsensitiveFileFinderTest.APPENDABLE_NEW_TMP_DIR_PATH);
  193. assertTrue(newTmpDir.isDirectory());
  194. // delete even though directory isn't empty
  195. FileUtils.deleteDirectory(newTmpDir);
  196. }
  197.  
  198. @Test
  199. public void findFooFilePngUsingLowercase() throws IOException {
  200. CaseInsensitiveFileFinder fileFinder = new CaseInsensitiveFileFinder();
  201. Optional<File> optFoundFile = fileFinder.findFileIgnoreCase(APPENDABLE_NEW_TMP_DIR_PATH, "foofile.png");
  202. assertTrue(optFoundFile.isPresent());
  203. File foundFile = optFoundFile.get();
  204. assertTrue(foundFile.isFile());
  205. assertEquals(APPENDABLE_NEW_TMP_DIR_PATH + "FOOFILE.PNG", foundFile.getCanonicalPath());
  206. }
  207.  
  208. @Test
  209. public void findBarFileJpgUsingLowercase() throws IOException {
  210. CaseInsensitiveFileFinder fileFinder = new CaseInsensitiveFileFinder();
  211. Optional<File> optFoundFile = fileFinder.findFileIgnoreCase(APPENDABLE_NEW_TMP_DIR_PATH, "barfile.jpg");
  212. assertTrue(optFoundFile.isPresent());
  213. File foundFile = optFoundFile.get();
  214. assertTrue(foundFile.isFile());
  215. assertEquals(APPENDABLE_NEW_TMP_DIR_PATH + "BARFILE.JPG", foundFile.getCanonicalPath());
  216. }
  217.  
  218. @Test
  219. public void findFileThatDoesNotExist() {
  220. CaseInsensitiveFileFinder fileFinder = new CaseInsensitiveFileFinder();
  221. Optional<File> optFoundFile = fileFinder.findFileIgnoreCase(APPENDABLE_NEW_TMP_DIR_PATH, "dne.txt");
  222. assertFalse(optFoundFile.isPresent());
  223. }
  224.  
  225. @Test
  226. public void findFooFileUsingDirWithNoTrailingFileSeparator() throws IOException {
  227. CaseInsensitiveFileFinder fileFinder = new CaseInsensitiveFileFinder();
  228. String newDirPathWithNoTrailingFileSep = StringUtils.chop(APPENDABLE_NEW_TMP_DIR_PATH);
  229. Optional<File> optFoundFile = fileFinder.findFileIgnoreCase(newDirPathWithNoTrailingFileSep, "FOOFILE.PNG");
  230. assertTrue(optFoundFile.isPresent());
  231. File foundFile = optFoundFile.get();
  232. assertTrue(foundFile.isFile());
  233. assertEquals(APPENDABLE_NEW_TMP_DIR_PATH + "FOOFILE.PNG", foundFile.getCanonicalPath());
  234. }
  235. }
  236.  
  237. public static File getPhotoFileExtension(int empKey){
  238. try{
  239. String[] types = {".jpg",".JPG",".png", ".PNG"};
  240. for(String t : types)
  241. {
  242. String path = "/"+Common.PHOTO_PATH + empKey + t;
  243. File f = new File(Sessions.getCurrent().getWebApp()
  244. .getRealPath(path));
  245. if(f.isFile())
  246. return f;
  247. }
  248. }catch (Exception e) {
  249. e.printStackTrace();
  250. }
  251. return null;
  252. }
  253.  
  254. public static boolean fileExistsCaseSensitive(String path) {
  255. try {
  256. File file = new File(path);
  257. return file.exists() || !file.getCanonicalFile().getName().equals(file.getName());
  258. } catch (IOException e) {
  259. return false;
  260. }
  261. }
Add Comment
Please, Sign In to add comment