首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有覆盖功能的下载功能

具有覆盖功能的下载功能
EN

Stack Overflow用户
提问于 2014-08-18 06:13:05
回答 2查看 116关注 0票数 0

我正在做一个下载文件到android设备的功能,运行良好,但我想这样做,如果下载的文件已经存在于设备中将覆盖它。下面是我的代码:

代码语言:javascript
复制
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;
    }


}
EN

回答 2

Stack Overflow用户

发布于 2014-08-18 06:47:06

如果该文件存在,请先将其删除。

票数 1
EN

Stack Overflow用户

发布于 2014-08-18 07:26:01

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

文件myFile =新建文件(FileName);

if(myFile.exists()) myFile.delete();

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25354148

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档