这个答案如何确定文件是否是PDF文件?建议下载另一个库,但我的要求是我只需要检查文件目录是否为PDF类型
使用完整的库用于此用途看起来像矫枉过正
SimpleMagic是用于解析内容类型的Java库:
<!-- pom.xml -->
<dependency>
<groupId>com.j256.simplemagic</groupId>
<artifactId>simplemagic</artifactId>
<version>1.8</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
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() );
}
Run Code Online (Sandbox Code Playgroud)