下载覆盖功能

时间:2014-08-17 22:13:05

标签: java android

我正在做一个将文件下载到Android设备的功能,工作正常,但我想这样做,如果设备中已存在已下载的文件将覆盖它。这是我的代码:

class DownloadTask extends AsyncTask<String, Integer, String> {

    private Context context;
    private PowerManager.WakeLock mWakeLock;

    public DownloadTask(Context context) {
        this.context = context;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // take CPU lock to prevent CPU from going off if the user 
        // presses the power button during download
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
             getClass().getName());
        mWakeLock.acquire();
        mProgressDialog.show();
    }

    @Override
    protected void onProgressUpdate(Integer... progress) {
        super.onProgressUpdate(progress);
        // if we get here, length is known, now set indeterminate to false
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.setMax(100);
        mProgressDialog.setProgress(progress[0]);
    }

    @Override
    protected void onPostExecute(String result) {
        mWakeLock.release();
        mProgressDialog.dismiss();
        if (result != null){

            this.finish();
            }
        else
        {

            Descargar.this.finish();
        }
    }

   @Override
    protected String doInBackground(String... sUrl) {
        InputStream input = null;

        HttpURLConnection connection = null;
        for (i=0; i< sUrl.length; i++) {
        try {
            URL url = new URL(sUrl[i]);
            connection = (HttpURLConnection) url.openConnection();
            connection.connect();

            // expect HTTP 200 OK, so we don't mistakenly save error report
            // instead of the file
            if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
                return "Server returned HTTP " + connection.getResponseCode()
                        + " " + connection.getResponseMessage();
            }

            // this will be useful to display download percentage
            // might be -1: server did not report the length
            int fileLength = connection.getContentLength();

            // download the file
            input = connection.getInputStream();

            fOut = openFileOutput(i+".json",MODE_PRIVATE);



            //fOut = new FileOutputStream("/aste/tiempobilbao.json");

            byte data[] = new byte[4096];
            long total = 0;
            int count;
            while ((count = input.read(data)) != -1) {
                // allow canceling with back button
                if (isCancelled()) {
                    input.close();
                    return null;
                }
                total += count;
                // publishing the progress....
                if (fileLength > 0) // only if total length is known
                    publishProgress((int) (total * 100 / fileLength));
                fOut.write(data, 0, count);

            }
        } catch (Exception e) {
            return e.toString();
        } finally {
            try {
                if (fOut != null)
                    fOut.close();

                if (input != null)
                    input.close();
            } catch (IOException ignored) {
            }

            if (connection != null)
                connection.disconnect();
        }
        }
        return null;
    }


}

2 个答案:

答案 0 :(得分:0)

如果文件存在,请先删除它。

答案 1 :(得分:0)

您可以在开始下载之前使用以下代码删除文件:

  

文件myFile = new File(fileName);

     

如果(myFile.exists())       myFile.delete();