java 我如何知道文件类型是否为 PDF?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13296939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I know if a File type is PDF?
提问by daydreamer
This answer How can I determine if a file is a PDF file?recommends to download another library, but my requirement is that I just need to check if a file is directory is of type PDF or not
Using complete library for this use looks like overkill
- Are there any ways to know that a Java File is of type PDF?
这个答案如何确定文件是否为 PDF 文件?建议下载另一个库,但我的要求是我只需要检查文件目录是否为 PDF 类型
为此使用完整的库看起来有点矫枉过正
- 有什么方法可以知道 Java 文件是 PDF 类型吗?
回答by ElderMael
回答by Abdull
SimpleMagicis a Java library for resolving content types:
SimpleMagic是一个用于解析内容类型的 Java 库:
<!-- pom.xml -->
<dependency>
<groupId>com.j256.simplemagic</groupId>
<artifactId>simplemagic</artifactId>
<version>1.8</version>
</dependency>
import com.j256.simplemagic.ContentInfo;
import com.j256.simplemagic.ContentInfoUtil;
import com.j256.simplemagic.ContentType;
// ...
public class SimpleMagicSmokeTest {
private final static Logger log = LoggerFactory.getLogger(SimpleMagicSmokeTest.class);
@Test
public void smokeTestSimpleMagic() throws IOException {
ContentInfoUtil util = new ContentInfoUtil();
File possiblePdfFile = new File("/path/to/possiblePdfFile.pdf");
ContentInfo info = util.findMatch(possiblePdfFile);
log.info( info.toString() );
assertEquals( ContentType.PDF, info.getContentType() );
}
回答by awolfe91
Well, kind of a hackish solution would be to look at the full file name and see if it ends in ".pdf". The following should help:
好吧,一种骇人听闻的解决方案是查看完整的文件名,看看它是否以“.pdf”结尾。以下应该有所帮助:
import javax.activation.*;
public class ShowMimeType
{
public static void main(String[] args) {
FileDataSource ds = new FileDataSource(args[0]);
String contentType = ds.getContentType();
System.out.println("The MIME type of the file " + args[0] + " is: " + contentType);
}
}
回答by case1352
If checking the file extension is not satisfactory, you coudl try checking the files magic numberby reading a few bytes of the file
如果检查文件扩展名不满意,您可以尝试通过读取文件的几个字节来检查文件幻数
PDF files start with "%PDF" (hex 25 50 44 46).
回答by Akin Okegbile
Combines lighter URLCOnnection.guessContentTypeFromStream() which returns null for some mimeTypes, with heavier AutoDetectParser.
将较轻的 URLCOnnection.guessContentTypeFromStream() 与较重的 AutoDetectParser 相结合,后者为某些 mimeTypes 返回 null。
if(currentImageType ==null){
ByteArrayInputStream is = new ByteArrayInputStream(image);
String mimeType = URLConnection.guessContentTypeFromStream(is);
if(mimeType == null){
AutoDetectParser parser = new AutoDetectParser();
Detector detector = parser.getDetector();
Metadata md = new Metadata();
mimeType = detector.detect(is,md).toString();
if (mimeType.contains("pdf")){
mimeType ="pdf";
}
else if(mimeType.contains("tif")||mimeType.contains("tiff")){
mimeType = "tif";
}
}
if(mimeType.contains("png")){
mimeType ="png";
}
else if( mimeType.contains("jpg")||mimeType.contains("jpeg")){
mimeType = "jpg";
}
else if (mimeType.contains("pdf")){
mimeType ="pdf";
}
else if(mimeType.contains("tif")||mimeType.contains("tiff")){
mimeType = "tif";
}
currentImageType = ImageType.fromValue(mimeType);
}
回答by andro-girl
Tried below code and it worked.
尝试了下面的代码,它奏效了。
public static boolean isSelectedFilePdf(Uri uri, ContentResolver contentResolver) {
if (uri != null) {
if (uri.getScheme().equals("content")) {
String type = contentResolver.getType(uri);
return type != null && type.startsWith("application/pdf");
} else {
String fileName = uri.getLastPathSegment();
String extension = fileName.substring(fileName.lastIndexOf("."));
return extension != null && extension.equalsIgnoreCase(".pdf");
}
}
}
回答by caot
The following solution is mentioned at Check whether a PDF-File is valid (Python)
检查 PDF 文件是否有效 (Python) 中提到了以下解决方案
In a project if mine I need to check for the mime type of some uploaded file. I simply use the file command like this:
在我的项目中,我需要检查某些上传文件的 MIME 类型。我只是像这样使用 file 命令:
from subprocess import Popen, PIPE
filetype = Popen("/usr/bin/file -b --mime -", shell=True, stdout=PIPE, stdin=PIPE).communicate(file.read(1024))[0].strip()
You of course might want to move the actual command into some configuration file as also command line options vary among operating systems (e.g. mac).
您当然可能希望将实际命令移动到某个配置文件中,因为命令行选项也因操作系统(例如 mac)而异。
If you just need to know whether it's a PDF or not and do not need to process it anyway I think the file command is a faster solution than a lib. Doing it by hand is of course also possible but the file command gives you maybe more flexibility if you want to check for different types.
如果您只需要知道它是否是 PDF 并且无论如何都不需要处理它,我认为 file 命令是比 lib 更快的解决方案。手动执行当然也是可能的,但是如果您想检查不同的类型,文件命令可能会给您更大的灵活性。
回答by Sam I am says Reinstate Monica
This might sound a little bit too obvious, but check the extension on the filename.
这听起来可能有点太明显了,但请检查文件名的扩展名。
If it's good enough for explorer, it should be good enough for you
如果它对探险家来说足够好,它应该对你足够好