0% found this document useful (0 votes)
260 views3 pages

FTP

This document contains the code for a Java program that tests connecting to an FTP server. It imports FTP and HTTP tunnel connector libraries. It defines variables for the FTP server details, files, and directories. It connects to the server, logs in with credentials, changes to a directory, uploads a file, lists the files, disconnects, and catches any exceptions during the process.

Uploaded by

Dani Supriyadi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
260 views3 pages

FTP

This document contains the code for a Java program that tests connecting to an FTP server. It imports FTP and HTTP tunnel connector libraries. It defines variables for the FTP server details, files, and directories. It connects to the server, logs in with credentials, changes to a directory, uploads a file, lists the files, disconnects, and catches any exceptions during the process.

Uploaded by

Dani Supriyadi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

D:\Documents\TestFTP.

java Tuesday, November 02, 2010 15:52

import it.sauronsoftware.ftp4j.FTPClient;
import it.sauronsoftware.ftp4j.connectors.HTTPTunnelConnector;
import java.io.*;

public class TestFTP {

public static void main(String args[]) {

FTPClient test = new FTPClient();


//FileOutputStream out; // declare a file output object
//FileWriter p; // declare a print stream object

// int mode = Integer.parseInt(args[0]);


int ftpPort = 21;
//int proxyPort = Integer.parseInt(args[4]);

String ftpServer = "ftp.giga.net.tw";


//String proxyServer = args[3];
String username = "anonymous";
String password = "x";
String fileName = "C:\\Documents and Settings\\dani\\Desktop\\MANIFEST11.zip";
File f1 = new File(fileName);
if (f1.exists()) System.out.println("file ada");
else System.out.println("file tidak ada");
String directory = "uploads/gigagame";

File connecting = new File(fileName+"1");


File connected = new File(fileName+"2");
File login = new File(fileName+"3");
File uploading = new File(fileName+"4");
File uploaded = new File(fileName+"5");
File disconnecting = new File(fileName+"6");
File disconnected = new File(fileName+"7");

// if(mode == 1) {
// directory = args[8];
// }

System.out.println(ftpServer+" "+ftpPort+" "+username+" "+password);

HTTPTunnelConnector HTTPproxy = new HTTPTunnelConnector("10.98.10.138",8090);


//HTTPTunnelConnector HTTPproxy = new HTTPTunnelConnector(proxyServer,proxyPort);
test.setConnector(HTTPproxy);

/* try
{
// Create a new file output stream
// connected to "myfile.txt"
out = new FileOutputStream("myfile.txt");

// Connect print stream to the output stream


p = new PrintStream( out );

p.println ("This is written to a file");

p.close();
}
catch (Exception e)
{
System.err.println ("Error writing to file");
}*/

try {
if (!connecting.exists() ) {
connecting.createNewFile();
}
test.connect(ftpServer, ftpPort);
if (!connected.exists() ) {

-1-
D:\Documents\TestFTP.java Tuesday, November 02, 2010 15:52

//connecting.delete();
connected.createNewFile();
}

//System.out.println("current dir 1 = "+ test.currentDirectory());


}catch (Exception e) {
e.printStackTrace();

System.out.println("Tidak dapat terhubung dengan FTP server, koneksi gagal atau kesalahan kombinasi
username/password");
System.exit(0);
}

try {
if (!login.exists() ) {
//connected.delete();
login.createNewFile();
}
test.login(username,password);
}catch (Exception e) {
e.printStackTrace();
System.out.println("Tidak dapat terhubung dengan FTP server, koneksi gagal");
System.exit(0);
}

// if (mode ==1) {
try {
test.changeDirectory(directory);
System.out.println("current dir 2 = "+ test.currentDirectory());
} catch (Exception e){
e.printStackTrace();
System.out.println("Gagal untuk pindah direktori");
}
// }

try {
if(!uploading.exists()) {
//login.delete();
uploading.createNewFile();
}
//test.upload(new java.io.File(fileName));
test.upload(f1);
if(!uploaded.exists()) {
//uploading.delete();
uploaded.createNewFile();
}
String[] files = test.listNames();
for (int i=0 ; i<files.length ; i++) {
System.out.println("file "+(i+1)+" = "+files[i]);
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("Upload gagal");
try {
//test.disconnect(true);
}catch (Exception c) {
c.printStackTrace();
System.out.println("Gagal memutuskan koneksi");
}
System.exit(0);
}

System.out.println("Upload file berhasil!");

try {
if(!disconnecting.exists()) {
//uploaded.delete();

-2-
D:\Documents\TestFTP.java Tuesday, November 02, 2010 15:52

disconnecting.createNewFile();
}
test.disconnect(true);
if(!disconnected.exists()) {
//disconnecting.delete();
disconnected.createNewFile();
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("Gagal memutuskan koneksi");
}

System.exit(0);
}
}

-3-

You might also like