It's not a secret anymore!
The moose likes Java in General and the fly likes FTP Client question Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "FTP Client question" Watch "FTP Client question" New topic
Author

FTP Client question

Dirk Coppieters
Greenhorn

Joined: Jul 09, 2004
Posts: 4
Hello,
I've found an example somewhere of an ftp client and re-used it myself.
However, I found that it doesn't work all the time. I mean, if I redownload the same file over and over again and compare the actual bytes received there is a 50 % chance the file is correctly received, but also a 50 % chance that the file is incompletely downloaded.
Could someone have a look at the example and tell me what I should add or what I'm doing wrong ?
Thanks for your help,
Dirk


// Directory.java
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.Logger;
import org.apache.log4j.Priority;
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.net.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;

public class Ftp
{
static Logger logger = Logger.getLogger(Ftp.class.getName());
static int progress_counter = 0;

//Constructor
public static void Ftp()
{ PropertyConfigurator.configure("log4j_configuration.ini");
}

public boolean download(String remote_file_name, String local_file_name) throws IOException
{
logger.debug("MIn");
File file = new File(local_file_name);

BufferedInputStream bis = null;
BufferedOutputStream bos = null;

URL url = new URL(remote_file_name);

bis = new BufferedInputStream(url.openStream());

FileOutputStream foss=new FileOutputStream(file);
bos = new BufferedOutputStream(foss);

byte[] buff = new byte[1024];
int bytesRead;
int totalbytesRead=0;
while(-1 != (bytesRead = bis.read(buff, 0, buff.length)))
{ totalbytesRead= totalbytesRead + bytesRead;
bos.write(buff, 0, bytesRead);
logger.debug("total bytes read: "+totalbytesRead);
progress_counter++;
logger.debug("progress_counter: "+progress_counter);
if ((progress_counter%1000) == 0)
{ logger.debug("MBytes read: "+(progress_counter*1000));
//gui.change_progress_bar_value(counter/1000);
}
}
foss.close();
logger.debug("MBytes read: "+(progress_counter*1000));
logger.debug("MOut");
return true;

}

public int get_progress_counter()
{ return progress_counter/1000;
}

} ///:~


Dirk Coppieters<br />Belgium
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24054
    
  13

You don't close the BufferedOutputStream, and Java isn't going to close it at shutdown: only the underlying file will be closed. Therefore, any data in the BufferedOutputStream's buffer may not actually ever be sent to the file. I suspect that if you explicitly close the BufferedOutputStream, the program will work fine.


[Jess in Action][AskingGoodQuestions]
Dirk Coppieters
Greenhorn

Joined: Jul 09, 2004
Posts: 4
Correct, Ernest !
Thanks for your help!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: FTP Client question
 
Similar Threads
Display PDF in IE from byte array using Servlet
How to Return a File using Servlet
Sending A File From An Applet To A Servlet
Corrupted Downloaded Files
Writing a new line character