티스토리 뷰
확장자만 구하기
String 클래스 이용
int pos = fileName.lastIndexOf( "." );
String result = fileName.substring( pos + 1 );
FilenameUtils.getExtension 이용
String extension = FilenameUtils.getExtension(file.getOriginalFilename());
org.apache.commons.io.FilenameUtils 의 getExtension() 메서드를 쓸 수 있습니다.
ex) example.txt 일 경우 "txt"가 리턴
확장자를 제외한 파일명만 구하기
String 클래스 이용
String fileName = file.getName();
int pos = fileName.lastIndexOf(".");
String result = fileName.substring(0, pos);
FilenameUtils.getBaseName 이용
String fileName = FilenameUtils.getBaseName(file.getOriginalFilename());
ex) example.txt 일 경우 "example" 만 리턴 "a/b/c.txt" 일 경우 c만 리턴합니다.
reference
'프로그래밍 언어 > Java Programming' 카테고리의 다른 글
JAVA Checked Exception, Runtime Exception (0) | 2021.02.25 |
---|---|
객체지향 프로그래밍 OOP 5대원칙 SOLID (0) | 2021.02.25 |
JDBC객체 및 DB 연결 (0) | 2016.12.24 |
JAVA 실행시간 측정 방법 (0) | 2016.11.19 |
JAVA 직렬화 (Serializable) (0) | 2016.11.16 |
댓글