2012-11-08 38 views
2
+2

爲什麼你不希望使用圖書館?這是什麼用例?查看擴展通常不是一個好主意,因爲任何人和任何其他程序都可以更改擴展。不看文件就很難確定它是否真的是PDF。爲此,我建議你使用一個庫。 – peshkira

+0

相關/重複:http://stackoverflow.com/questions/1915317/howto-extract-mimetype-from-a-byte –

+0

嘗試看看http://stackoverflow.com/questions/51438/gettinga-a- files-mime-type-in​​-java – MadProgrammer

回答

11

那麼,根據wikipedia PDF文件幻數開始:"%PDF" (hex 25 50 44 46)所以也許你應該從文件檢查InputStream和檢查。

+0

剛剛在記事本++中打開了一個PDF,它的確的確如此。 +1 –

+0

是的,我有一個類似的用例,維基百科是非常有幫助的 – ElderMael

+0

但如果你創建一個文本文件,並開始與%PDF-1.4,只是爲了擰與操作 –

0

這可能聽起來有點太明顯,但請檢查文件名上的擴展名。

如果它是探險不夠好,應該是不夠好,你

+2

擴展不會說格式的任何內容。 – peshkira

+0

@peshkira好,它應該。很少你不能相信它。 –

+1

基於什麼理由,你是基於你的評論。你怎麼能說這很少?這取決於用例。你說這很少,因爲你可能不會這樣做,或者沒有遇到它,但這並不意味着它不會發生在真實的世界中。 – peshkira

1

嗯,善良的hackish的解決辦法是看完整的文件名,看看它是否在「.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); 
    } 
} 
1

如果檢查的文件擴展名是不盡如人意,你coudl通過讀取文件的幾個字節嘗試檢查文件magic number

PDF files start with "%PDF" (hex 25 50 44 46). 
0

結合了更輕URLCOnnection.guessContentTypeFromStream()這對一些MIMETYPES返回null,與較重的AutoDetectParser。

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); 
      } 
1

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()); 
    } 
相關問題