我知道我可以通过这样做得到这个
String ext = FilenameUtils.getExtension("/path/to/file/foo.txt");
但是,如果有人尝试通过将扩展类型更改为 foo.doc 来尝试上传 foo.exe 文件,该怎么办?有没有办法在不阅读文件内容的情况下获得实际的扩展类型
答案 0 :(得分:2)
File file = new File("filename.asgdsag");
InputStream is = new BufferedInputStream(new FileInputStream(file));
String mimeType = URLConnection.guessContentTypeFromStream(is);
从这篇文章:Get real file extension -Java code
同样使用java7,你应该看看:
public static String probeContentType(Path path)
throws IOException