java - Download function with overwrite -


i doing function download files android device, works fine want if downloaded file exists in device overwrite it. here code:

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 prevent cpu going off if user          // presses 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 here, length known, set indeterminate 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;         (i=0; i< surl.length; i++) {         try {             url url = new url(surl[i]);             connection = (httpurlconnection) url.openconnection();             connection.connect();              // expect http 200 ok, don't mistakenly save error report             // instead of file             if (connection.getresponsecode() != httpurlconnection.http_ok) {                 return "server returned http " + connection.getresponsecode()                         + " " + connection.getresponsemessage();             }              // useful display download percentage             // might -1: server did not report length             int filelength = connection.getcontentlength();              // download 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 button                 if (iscancelled()) {                     input.close();                     return null;                 }                 total += count;                 // publishing progress....                 if (filelength > 0) // if total length known                     publishprogress((int) (total * 100 / filelength));                 fout.write(data, 0, count);              }         } catch (exception e) {             return e.tostring();         } {             try {                 if (fout != null)                     fout.close();                  if (input != null)                     input.close();             } catch (ioexception ignored) {             }              if (connection != null)                 connection.disconnect();         }         }         return null;     }   } 

if file exist, delete first.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -