Android https下载文件

时间:2015-01-21 09:08:02

标签: android https android-sdcard android-permissions

我遇到了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()    

File SDCardRoot = new file(“/ sdcard /”+“Some Folder Name /”);

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

结果int status = urlConnection.getResPonseCode()== 200,但我无法获取数据。任何建议,谢谢!

2 个答案:

答案 0 :(得分:0)

您可能想要检查您是否具有写入权限。 (特别是SD卡)。

我在堆栈中发现了这个:

  

你说SD卡目录是/ SD卡,但你不应该这样做   要硬编码。相反,拨打电话:       Environment.getExternalStorageDirectory()//获取目录:

File sdDir = Environment.getExternalStorageDirectory(); 
     

如果你还没有   这样做了,你需要给你的应用程序正确的权限   通过将以下行添加到您的Manifest来写入SD卡:

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

来源: Permission to write to the SD card

答案 1 :(得分: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