在android中使用https连接从url复制文件

时间:2016-03-30 09:33:02

标签: java android

我是初学者,我无法通过身份验证从https连接下的网址复制文件。我需要进行身份验证,并信任主机。 url中的这个文件是一个小文件。它复制了一块但不是文件中的所有内容,总是复制同一块。知道可能是什么?

我尝试从连接获取inputStream后复制文件。如下:

    boolean result=false;
    ....
    https.connect();
    InputStream in=https.getInputStream();
    File f=new File(context.getExternalFilesDir(null)+File.separator+"file.json");
    RandomAccessFile out=new RandomAccessFile(f,"rw");
    int numberOfBytesWritten=0;
    try {
      byte[] buffer = new byte[1000];
      int count;
      while ((count = in.read(buffer)) > 0) {
        out.write(buffer, 0, count);
        numberOfBytesWritten += count;
      }
      out.close();
      in.close();
    }catch (Exception e){e.printStackTrace();}
    if(bytes==0) result=false;
    else result=true;

    if(result) Toast.makeText(getApplicationContext(),"DOWNLOADED",Toast.LENGTH_LONG).show();
    else Toast.makeText(getApplicationContext(),"FAILED",Toast.LENGTH_LONG).show();

0 个答案:

没有答案