简体   繁体   中英

Android https download file

I have a problem with https. I have a link, I connect to the link succeed, but can not get data, why ?

This my code

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

result int status = urlConnection.getResPonseCode() == 200, but I can not get Data. Any thing advice, thank !

You might want to check if you have writing permissions. (Specifically to the SD card).

I've found this in stack:

You're right that the SD Card directory is /sdcard but you shouldn't be hard coding it. Instead, make a call to: Environment.getExternalStorageDirectory() //to get the directory:

 File sdDir = Environment.getExternalStorageDirectory(); 

If you haven't done so already, you will need to give your app the correct permission to write to the SD Card by adding the line below to your Manifest:

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

Source: Permission to write to the SD card

Try this example

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 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM