Java
MIME 타입 알아내는 방법
풍풍
2013. 10. 2. 10:37
File file = new File(path + fileName); file.toURL().openConnection().getContentType(); // 1 URLConnection.guessContentTypeFromName(orgFile.getName()); // 2 URLConnection.guessContentTypeFromStream(new BufferedInputStream(new FileInputStream(orgFile))); // 3 new MimetypesFileTypeMap().getContentType(orgFile); // 4 Files.probeContentType(Paths.get(orgFile.toURI())); // 5
위의 방법으로 엑셀 2007 형식의 파일을 분석했을때 오피스가 설치된 pc와 설치되지 않은 pc에서 각각 다른 반응을 보였다.
1. 설치된 pc
1 >>>>>>content/unknown
2 >>>>>>null
3 >>>>>>null
4 >>>>>>application/octet-stream
5 >>>>>>application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
2. 설치되지 않은 pc
1 >>>>>>content/unknown
2 >>>>>>null
3 >>>>>>null
4 >>>>>>application/octet-stream
5 >>>>>>null
5번이 가장 정확한 내용을 알려주지만 경우에 따라서 null이 나올 수도 있으므로 4번이 가장 무난한 선택일수도 있겠다.