0
  1. 我能够从服务器很好地将数据库文件下载到 sdcard,但现在的问题是当下载中断时它不会恢复,因此下载的数据库文件不完整,从而导致我的应用程序崩溃。他们是我可以恢复下载或将下载文件的大小与原始文件大小进行比较的一种方式。
  2. 我如何获取服务器日期或网络运营商日期以进行其他比较

示例代码

  protected String doInBackground(String... f_url) {
        int count;
        try {
            URL url = new URL(f_url[0]);
            URLConnection conection = url.openConnection();
            conection.connect();
            // getting file length
           int lenghtOfFile = conection.getContentLength();

            // input stream to read file - with 8k buffer
            InputStream input = new BufferedInputStream(url.openStream(), 8192);

            // Output stream to write file
            String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
            File testDirectory = new File(baseDir +"/test");
             testDirectory.mkdirs();

            OutputStream output = new FileOutputStream(testDirectory + "/db");

            byte data[] = new byte[1024];

            long total = 0;

            while ((count = input.read(data)) != -1) {
                total += count;
                // publishing the progress....
                // After this onProgressUpdate will be called
                publishProgress(""+(int)((total*100)/lenghtOfFile));

                // writing data to file
                output.write(data, 0, count);
            }

            // flushing output
            output.flush();

            // closing streams
            output.close();
            input.close();

        } catch (Exception e) {
            Log.e("Error: ", e.getMessage());
        }

        return null;
    }

    /**
     * Updating progress bar
     * */
    protected void onProgressUpdate(String... progress) {
        // setting progress percentage
        pDialog.setProgress(Integer.parseInt(progress[0]));
   }

我只是在调用哪里 file_url 是下载链接

  new DownloadFileFromURL().execute(file_url);
4

0 回答 0