2015-01-21 95 views
0

我對https有問題。我有一個鏈接,我連接成功的鏈接,但無法獲取數據,爲什麼?Android https下載文件

這是我的代碼

/Create a KeyStore containing our trusted CAs 
KeyStore keyStore = KeyStore.getInstance("BKS"); 
InputStream caInput = new FileInputStream(R.raw.keystore); 
keyStore.load(caInput , mypas.toCharArray()); 

// Create a TrustManager that trusts the CAs in our KeyStore 
String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm(); 
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm); 
tmf.init(keyStore); 

// Create an SSLContext that uses our TrustManager 
SSLContext context = SSLContext.getInstance("TLS"); 
context.init(null, tmf.getTrustManagers(), null); 

// Tell the URLConnection to use a SocketFactory from our SSLContext 
URL url = new URL("https://certs.cac.washington.edu/CAtest/"); 
HttpsURLConnection urlConnection = 
    (HttpsURLConnection)url.openConnection(); 
urlConnection.setSSLSocketFactory(context.getSocketFactory()); 

urlConnection.connect(); 
status = urlConnection.getResPonseCode()  

文件SDCardRoot =新的文件( 「/ SD卡/」 + 「有些文件夾名稱/」);

//create a new file, specifying the path, and the filename 
//which we want to save the file as. 
File file = new File(SDCardRoot,"some file name"); 

//this will be used to write the downloaded data into the file we created 
FileOutputStream fileOutput = new FileOutputStream(file); 

//this will be used in reading the data from the internet 
InputStream inputStream = urlConnection.getInputStream(); 

//this is the total size of the file 
int totalSize = urlConnection.getContentLength(); 

//variable to store total downloaded bytes 
int downloadedSize = 0; 

//create a buffer... 
byte[] buffer = new byte[1024]; 

int bufferLength = 0; //used to store a temporary size of the buffer 

//now, read through the input buffer and write the contents to the file 
while ((bufferLength = inputStream.read(buffer)) > 0) 
{ 
    //add the data in the buffer to the file in the file output stream (the file on the sd card 
    fileOutput.write(buffer, 0, bufferLength); 

    //add up the size so we know how much is downloaded 
    downloadedSize += bufferLength; 

    int progress=(int)(downloadedSize*100/totalSize); 
} 

result int status = urlConnection.getResPonseCode()== 200,但我無法獲取數據。任何建議,謝謝!

+0

日誌貓任何異常? – Prateek 2015-01-21 09:51:21

回答

0

您可能想要檢查您是否有寫入權限。 (具體到SD卡)。

我發現這堆:

你說得對,SD卡目錄是/ SD卡,但你不應該 被硬編碼了。相反,請調用: Environment.getExternalStorageDirectory()//獲取目錄:

File sdDir = Environment.getExternalStorageDirectory(); 

如果您還沒有 這樣做的話,你需要給你的應用程序的正確權限 寫爲通過將線下到你的清單中的SD卡:

<uses-permission 
android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

來源: Permission to write to the SD card

+0

我在我的項目上安裝了該權限。 – 2015-01-21 09:54:34

0

試試這個例子

URL url = new URL; 
URLConnection urlConnection = url.openConnection(); 
InputStream in = urlConnection.getInputStream(); 
copyInputStreamToOutputStream(in, System.out); 

// Load CAs from an InputStream 
// (could be from a resource or ByteArrayInputStream or ...) 
CertificateFactory cf = CertificateFactory.getInstance("X.509"); 
// From https://www.washington.edu/itconnect/security/ca/load-der.crt 
InputStream caInput = new BufferedInputStream(new FileInputStream("load-der.crt")); 
Certificate ca; 
try { 
ca = cf.generateCertificate(caInput); 
System.out.println("ca=" + ((X509Certificate) ca).getSubjectDN()); 
} finally { 
caInput.close(); 
} 

// Create a KeyStore containing our trusted CAs 
String keyStoreType = KeyStore.getDefaultType(); 
KeyStore keyStore = KeyStore.getInstance(keyStoreType); 
keyStore.load(null, null); 
keyStore.setCertificateEntry("ca", ca); 

// Create a TrustManager that trusts the CAs in our KeyStore 
String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm(); 
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm); 
tmf.init(keyStore); 

// Create an SSLContext that uses our TrustManager 
SSLContext context = SSLContext.getInstance("TLS"); 
context.init(null, tmf.getTrustManagers(), null); 

// Tell the URLConnection to use a SocketFactory from our SSLContext 
URL url = new URL; 
HttpsURLConnection urlConnection = 
(HttpsURLConnection)url.openConnection(); 
urlConnection.setSSLSocketFactory(context.getSocketFactory()); 
InputStream in = urlConnection.getInputStream(); 
copyInputStreamToOutputStream(in, System.out); 

http://developer.android.com/training/articles/security-ssl.html