我在这里检查了答案,但首先他们将其保存到某个地方,然后读取它的流并尝试检查该文件的 Mime 类型
Get real file extension -Java code
但我什至想在将文件保存到硬盘或某个地方之前知道文件类型。
我知道我可以通过另外两种方式做到这一点
File savedFile = new File(fileHandler.createTodayFolder(fileLocation) + "/" + name);
Path path = Paths.get(file.get);
Tika tika = new Tika();
String checkType = tika.detect(path.toFile());
和
Path path = Paths.get(savedFile.getPath());
Files.probeContentType(path)
但是在这两种情况下,他们都在使用一些已经在硬盘中某个位置的文件。 我想要这样的代码
public @ResponseBody ResponseEntity<?> importResume(@RequestParam(value = "name", required = false) String name,
@RequestParam("file") MultipartFile file) {
if (file!=DESIRED_TYPE)
{
return Exception;
}
}
有什么办法吗???
最佳答案
对我来说,最干净的解决方案:
使用 org.apache.commons.io 的 FilenameUtils:
String extension = FilenameUtils.getExtension(file.getOriginalFilename());
关于java - 如何获取 Multipart 文件的真实扩展类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33214214/