2015-06-11 97 views
0

我寫下面的代碼來實現讀取.crt文件,但它不起作用。如何在Android中讀取.crt文件

AssetManager assetManager = getAssets(); 

InputStream in = null; 

in = assetManager.open("client.crt"); 

CertificateFactory cf = CertificateFactory.getInstance("X.509"); 

X509Certificate ca = (X509Certificate) cf.generateCertificate(in); 

最後一行不執行。有人請幫助我。謝謝!

回答

0

試試這個代碼片段

CertificateFactory cf = CertificateFactory.getInstance("X.509"); 
    // From https://www.washington.edu/itconnect/security/ca/load-der.crt 
    InputStream is = context.getResources().getAssets().openAsset("somefolder/somecertificate.crt"); // path should be your files path 
    InputStream caInput = new BufferedInputStream(is); 
    Certificate ca; 
    try { 
     ca = cf.generateCertificate(caInput); 
     // System.out.println("ca=" + ((X509Certificate) ca).getSubjectDN()); 
    } finally { 
     caInput.close(); 
    } 
+0

謝謝,但它並不能工作。 – David

+0

您是否收到任何錯誤或警告? –

+0

編號我發現android通過丟失一些內容來讀取.crt文件有一些問題。 – David