Advertisement
Guest User

Untitled

a guest
Feb 3rd, 2010
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.16 KB | None | 0 0
  1.  try {
  2.  
  3.             FileOutputStream out1 = new FileOutputStream(new File(localFile));
  4.             ftp.connect(host, port);
  5.             ftp.login(username, password);
  6.             FTPFile f = new FTPFile();
  7.  
  8.             //ftp.retrieveFile(file, out);
  9.             InputStream in = ftp.retrieveFileStream(file);
  10.             ByteArrayOutputStream out = new ByteArrayOutputStream();
  11.  
  12.             byte[] buffer = new byte[1024];
  13.             int bytesRead = 0;
  14.             int current = 0;
  15.  
  16.             while ((bytesRead = in.read(buffer)) != -1) //keep filling the buffer until we get to the end of the file
  17.             {
  18.                 out.write(buffer, current, bytesRead); //write the buffer to the file offset = current, length = bytesRead
  19.                 current += bytesRead; //we've progressed a little so update current
  20.                 setProgress((int)(current/size)*100); //tell progress how far we are
  21.             }
  22.  
  23.             out.writeTo(out1);
  24.             out.close();
  25.             out1.close();
  26.             in.close();
  27.  
  28.         } catch (Exception ex) {
  29.             Logger.getLogger(DownloadThread.class.getName()).log(Level.SEVERE, null, ex);
  30.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement