• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Need Expert help with FTP

 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have to connect by ftp to a server and download files(Text and Images). I have downloaded NetComponents from :
http://www.savarese.org/oro/downloads/index.html
and I used this java file below to connect:
<java>
import java.io.*;
import com.oroinc.net.ftp.*;
public final class ftp {
public static final String USAGE =
"Usage: ftp [-s] [-b] <hostname> <username> <password> <remote file> <local file>\n" +
"\nDefault behavior is to download a file and use ASCII transfer mode.\n" +
"\t-s store file on server (upload)\n" +
"\t-b use binary transfer mode\n";
public static final void main(String[] args) {
int base = 0;
boolean storeFile = false, binaryTransfer = false, error = false;
String server, username, password, remote, local;
FTPClient ftp;
for(base = 0; base < args.length; base++) {
if(args[base].startsWith("-s"))
storeFile = true;
else if(args[base].startsWith("-b"))
binaryTransfer = true;
else
break;
}
if((args.length - base) != 5) {
System.err.println(USAGE);
System.exit(1);
}
server = args[base++];
username = args[base++];
password = args[base++];
remote = args[base++];
local = args[base];
ftp = new FTPClient();
ftp.addProtocolCommandListener(new PrintCommandListener(
new PrintWriter(System.out)));
try {
int reply;
ftp.connect(server);
System.out.println("Connected to " + server + ".");
// After connection attempt, you should check the reply code to verify
// success.
reply = ftp.getReplyCode();
if(!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
System.err.println("FTP server refused connection.");
System.exit(1);
}
} catch(IOException e) {
if(ftp.isConnected()) {
try {
ftp.disconnect();
} catch(IOException f) {
// do nothing
}
}
System.err.println("Could not connect to server.");
e.printStackTrace();
System.exit(1);
}
__main:
try {
if(!ftp.login(username, password)) {
ftp.logout();
error = true;
break __main;
}
System.out.println("Remote system is " + ftp.getSystemName());
if(binaryTransfer)
ftp.setFileType(FTP.BINARY_FILE_TYPE);
if(storeFile) {
InputStream input;
input = new FileInputStream(local);
ftp.storeFile(remote, input);
} else {
OutputStream output;
output = new FileOutputStream(local);
ftp.retrieveFile(remote, output);
}
ftp.logout();
} catch(FTPConnectionClosedException e) {
error = true;
System.err.println("Server closed connection.");
e.printStackTrace();
} catch(IOException e) {
error = true;
e.printStackTrace();
} finally {
if(ftp.isConnected()) {
try {
ftp.disconnect();
} catch(IOException f) {
// do nothing
}
}
}
System.exit(error ? 1 : 0);
} // end main
}
</java>
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic