File is not downloaded completely using Java code -


i'm using java download file, have used 3 different code snippets i'm going post here. result same. downloading file partially , result file can't opened not downloaded completely. how should fix problem.

my first code:

public class filedownloader {   final static int size=1024;  public static void main(string[] args){      fileurl("http://textfiles.com/holiday","holiday.tar.gz","c:\\users\\me\\downloads");   }      public static void  fileurl(string faddress, string localfilename, string destinationdir) {     outputstream outstream = null;     urlconnection  ucon = null;      inputstream = null;     try {         url url;         byte[] buf;         int byteread,bytewritten=0;         url= new url(faddress);         outstream = new bufferedoutputstream(new         fileoutputstream(destinationdir+"\\"+localfilename));          ucon = url.openconnection();         = ucon.getinputstream();         buf = new byte[size];         while ((byteread = is.read(buf)) != -1) {             outstream.write(buf, 0, byteread);             bytewritten += byteread;         }         system.out.println("downloaded successfully.");         system.out.println("file name:\""+localfilename+ "\"\nno ofbytes :" + bytewritten);     }catch (exception e) {         e.printstacktrace();         }     {             try {             is.close();             outstream.close();             }             catch (ioexception e) {         e.printstacktrace();             }         }  }       } 

second code used:

public class downloadfile {  public static void main(string[]args){     downloadfile dfs = new downloadfile();     dfs.downloadfileandstore("c:\\users\\me\\downloads","sign&validateversion2.docx");  }      /**      * download , save file      * @param fileurl: string containing url       * @param destinationdirectory : directory store downloaded file      * @param filename : filename without extension      */     public void downloadfileandstore(string destinationdirectory,string filename){     //  url url = null;         fileoutputstream fos = null;              //convert string url              try {         //      url = new url(fileurl);                   httpclient client = httpclientbuilder.create().build();                  httpget request = new httpget("https://mail.uvic.ca/owa/#path=/mail");                  httpresponse response = client.execute(request);                  if(response.getentity().getcontent().read()==-1){                      log.error("response empty");                  }                  else{                   bufferedreader rd = new bufferedreader(new inputstreamreader(response                              .getentity().getcontent()));                  stringbuffer result = new stringbuffer();                  string line = "";                  while ((line = rd.readline()) != null) {                      result.append(line);                  }                  fos = new fileoutputstream(destinationdirectory + "\\" + filename);                  fos.write(result.tostring().getbytes());                  fos.close();                 }             } catch (malformedurlexception e) {                 // todo auto-generated catch block                  log.error("pdf " + filename + " error: " + e.getmessage());             } catch (clientprotocolexception e) {                 // todo auto-generated catch block                  log.error("pdf " + filename + " error: " + e.getmessage());             } catch (unsupportedoperationexception e) {                 // todo auto-generated catch block                 log.error("pdf " + filename + " error: " + e.getmessage());             } catch (filenotfoundexception e) {                 // todo auto-generated catch block                 log.error("pdf " + filename + " error: " + e.getmessage());             } catch (ioexception e) {                 // todo auto-generated catch block                 log.error("pdf " + filename + " error: " + e.getmessage());             }  }  } 

the third code used:

public class download {  public static void main(string[] args) {     download dfs = new download();     dfs.downloadfile();  } public void downloadfile(){     try {         ioutils.copy(                 new url("https://archive.org/details/alanoakleysmalltestvideo").openstream(),                  new fileoutputstream("c:\\users\\me\\downloads\\spacetestsmall.wmv")             );     } catch (ioexception e) {         // todo auto-generated catch block         e.printstacktrace();     } }    } 

i have download video file, 6-7 mb, have used these 3 codes small text files , other types, didn't work. 1 have idea?

i tried first example , works me. guess misinterpreting doing. with

fileurl("http://textfiles.com/holiday","holiday.tar.gz","c:\\users\\me\\downloads"); 

you downloading html page of textfiles.com/holiday file called "holiday.tar.gz". actual tar archive has url archives.textfiles.com/holiday.tar.gz.

in 1 of comments want download video under archive.org/details/alanoakleysmalltestvideo, using url download html page (successfully) video embedded.

if html sources, can find actual video url, e.g., archive.org/download/alanoakleysmalltestvideo/spacetestsmall_512kb.mp4, , download existing code.


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -