Created
June 10, 2013 05:02
-
-
Save tomoyamkung/5746650 to your computer and use it in GitHub Desktop.
[Java]画像ファイルを選別する FilenameFilter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 画像ファイルの許可する拡張子 | |
upload.file.extensions = .jpeg,.jpg,.png,.gif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.tomoyamkung; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Properties; | |
public class AppProperties { | |
private static AppProperties instance; | |
private static final String PROP_FILE_NAME = "app.properties"; | |
private Properties prop; | |
/** | |
* コンストラクタ。 | |
* | |
* シングルトン。 | |
* | |
* @throws IOException | |
*/ | |
private AppProperties() throws IOException { | |
prop = new Properties(); | |
prop.load(this.getClass().getClassLoader().getResourceAsStream(PROP_FILE_NAME)); | |
} | |
/** | |
* インスタンスを生成する。 | |
* | |
* @return AppProperties クラスのインスタンス | |
* @throws IOException インスタンスの取得に失敗した場合 | |
*/ | |
public static synchronized AppProperties getInstance() throws IOException { | |
if(instance == null) { | |
instance = new AppProperties(); | |
} | |
return instance; | |
} | |
public String get(String key) { | |
return prop.getProperty(key).trim(); | |
} | |
public List<String> getStringList(String key) { | |
String[] array = get(key).split(","); | |
List<String> values = new ArrayList<String>(array.length); | |
for(int i = 0; i < array.length; i++) { | |
values.add(array[i].trim()); | |
} | |
return values; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.tomoyamkung; | |
import java.io.File; | |
import java.io.FilenameFilter; | |
import java.io.IOException; | |
import java.util.List; | |
/** | |
* 画像ファイルを選別する FilenameFilter | |
* | |
* @author tomoyamkung | |
* | |
*/ | |
public class ImageFileFilter implements FilenameFilter { | |
/* (non-Javadoc) | |
* @see java.io.FilenameFilter#accept(java.io.File, java.lang.String) | |
*/ | |
@Override | |
public boolean accept(File dir, String fileName) { | |
try { | |
List<String> extensions = AppProperties.getInstance().getStringList("upload.file.image.extensions"); | |
for(String extension : extensions) { | |
if(fileName.endsWith(extension) || fileName.endsWith(extension.toUpperCase())) { | |
return true; | |
} | |
} | |
return false; | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment