File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes Downloading a file from an FTP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Downloading a file from an FTP" Watch "Downloading a file from an FTP" New topic
Author

Downloading a file from an FTP

Tom Hill
Ranch Hand

Joined: Aug 24, 2003
Posts: 115
Hi guys!

Im trying to open a connection with a server, and download a jar file and reconstruct it on the client system running the application. When the file downloads the jar is corrupt. Im assumed it was because I have downloaded in ascii format but Im using input and output streams for the process. Ive checked the jar file on the server and it is intact.

Any Ideas?

try{
URL fileurl = new URL("ftp://SERVERNAME/FILE.jar");
URLConnection urlC = fileurl.openConnection();
InputStream is = fileurl.openStream();
FileOutputStream fos= new FileOutputStream("C:\\SOMEDIR\\FILE.jar");

int count = 0;
int numb;
while( (numb=is.read())!=-1){
count++;
fos.write(numb);
}
is.close();
fos.close();
System.out.println(count +" byte(s) copied");
}catch(Exception ftpe){
System.out.println(ftpe);
}
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8836
    
    7

Try changing your url to:
"ftp://SERVERNAME/FILE.jar;type=i"
That will tell the FTP client to handle the data as binary.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Tom Hill
Ranch Hand

Joined: Aug 24, 2003
Posts: 115
thanks that semms to have made it work!
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8836
    
    7

Don't thank me. Thank the RFC1738 - Uniform Resource Locators document.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Downloading a file from an FTP
 
Similar Threads
probem with FTPing the file using commons-net
Problem with InputStream using org.apache.commons.net
ftp using java
FTP of a document to server using JAVA.
creating jar using java.util.jar