Android:从网站下载的文件与原始文件不同

时间:2012-01-31 16:00:33

标签: android file download

我的初衷是从我的网站下载我的Android应用程序(myApp.apk)的新版本并更新设备上正在运行的应用程序... 有一个问题,因为我意识到下载的文件与网站中的文件不同,我做了二进制比较,并且有很多不同的区域。有人可以给出一个线索吗?

这是我用来下载文件的代码:

    URLConnection ucon = null;

    try 
    {
       URL url = new URL("http://www.mySite.com/folder/myApp.apk");
       HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
       urlConnection.setRequestMethod("GET");
       urlConnection.setDoInput(true);
       urlConnection.connect();
       File SDCardRoot = Environment.getExternalStorageDirectory();
       File file = new File(SDCardRoot,"FileDownloaded11.apk");
       FileOutputStream fileOutput = new FileOutputStream(file);
       InputStream inputStream = urlConnection.getInputStream();
       int totalSize = urlConnection.getContentLength();
       int downloadedSize = 0;
       byte[] buffer = new byte[8192];
       int bufferLength = 0; //used to store a temporary size of the buffer
       while ( (bufferLength = inputStream.read(buffer)) > 0 ) 
       {
            fileOutput.write(buffer, 0, bufferLength);
            downloadedSize += bufferLength;

        }
        fileOutput.close();
    } 
    catch (MalformedURLException e) 
    {
        e.printStackTrace();
    } 
    catch (IOException e) 
    {
        e.printStackTrace();
    }

0 个答案:

没有答案