Files.probeContentType的替代? [英] Alternative to Files.probeContentType?
问题描述
在一个Web项目中,用户上传他们的文件,但是当我在服务器上收到它们时,它们被存储为.tmp文件而不是原始文件扩展名(这也是我的首选行为)。
In a web project, users upload their files, but when I receive them on the server, they are being stored as .tmp files rather than their original file extension (this is my preferred behavior as well).
但是,这会导致 Files.probeContentType()
出现问题。虽然本地对我来说,在我的Linux开发机器上,当我将项目上传到生产服务器时, Files.probeContentType()
正常工作并确定正确的mime类型beanstalk),它似乎没有正确确定mime类型。
However, this is causing a problem with Files.probeContentType()
. While locally for me, on my Linux dev machine, Files.probeContentType()
works correctly and does determine the right mime type, when I upload my project to the production server (amazon beanstalk), it doesn't seem to correctly determine the mime type.
从阅读javadocs,似乎 Files.probeContentType的实现()
是不同的,我认为在生产服务器上,它正在读取文件扩展名,因此无法确定内容类型。
From reading the javadocs, it seems that the implementation of Files.probeContentType()
are different, and I think that on production server, it is reading the file extension and so, is unable to determine the content type.
什么是 Files.probeContentType()
的一个好的,快速的替代品,它将接受文件
参数并返回一个字符串如 image / png
作为生成的mime类型?
What's a good, and quick alternative to Files.probeContentType()
which will accept a File
argument and return a string like image/png
as the resulting mime type?
推荐答案
Take看一下 Apache Tika 。它可以很容易地确定一个mime类型:
Take a look at the Apache Tika. It can easily determine a mime type:
Tika tika = new Tika();
File file = ...
String mimeType = tika.detect(file);
这是一个最小的所需maven依赖:
Here's a minimal required maven dependency:
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-core</artifactId>
<version>1.12</version>
</dependency>
这篇关于Files.probeContentType的替代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!