So I am trying to write an FTP server and client. I believe that I almost have it, but I think that I am missing something with sending the file. If I try to send a file over FTP, it will pop up at the server side, but the file will be empty. I think that I am not doing my I/O right...if anyone has any suggestions or examples about how to get my file I/O right that would be very helpful.
FTP Server:
import java.io.*;
import java.net.*;
public class FtpServer {
public static void main(String[] args) {
int i = 1;
System.out.println("******************************************************************************");
System.out.println("********************************* FTP SERVER *********************************");
System.out.println("******************************************************************************");
try {
ServerSocket s = new ServerSocket(100);
System.out.println("Server Started...");
System.out.println("Waiting for connections...\n");
for (;;) {
Socket incoming = s.accept();
System.out.println("New Client Connected with id " + i + " from " + incoming.getInetAddress().getHostName() + "...");
Thread t = new ThreadedServer(incoming, i);
i++;
t.start();
}
} catch (Exception e) {
System.out.println("Error: " + e);
}
}
}
class ThreadedServer extends Thread {
int n;
String c, fn, fc;
String filenm;
Socket incoming;
int counter;
String dirn = "C:/FTP-ROOT";
public ThreadedServer(Socket i, int c) {
incoming = i;
counter = c;
}
public void run() {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
OutputStream output = incoming.getOutputStream();
fn = in.readLine();
c = fn.substring(0, 1);
if (c.equals("#")) {
n = fn.lastIndexOf("#");
filenm = fn.substring(1, n);
FileInputStream fis = null;
boolean filexists = true;
System.out.println("Request to download file \"" + filenm + "\" recieved from " + incoming.getInetAddress().getHostName() + "...");
try {
fis = new FileInputStream(filenm);
} catch (FileNotFoundException exc) {
filexists = false;
System.out.println("FileNotFoundException:<BR>" + exc.getMessage());
}
if (filexists) {
sendBytes(fis, output);
fis.close();
}
} else {
try {
boolean done = true;
System.out.println("Request to upload file \"" + fn + "\" recieved from " + incoming.getInetAddress().getHostName() + "...");
File dir = new File(dirn);
if (!dir.exists()) {
dir.mkdir();
} else {
}
File f = new File(dir, fn);
FileOutputStream fos = new FileOutputStream(f);
DataOutputStream dops = new DataOutputStream(fos);
while (done) {
fc = in.readLine();
if (fc == null) {
done = false;
} else {
dops.writeChars(fc);
}
}
fos.close();
} catch (Exception ecc) {
System.out.println(ecc.getMessage());
}
}
incoming.close();
} catch (Exception e) {
System.out.println("Error: " + e);
}
}
private static void sendBytes(FileInputStream f, OutputStream op)
throws Exception {
byte[] buffer = new byte[1024];
int bytes = 0;
while ((bytes = f.read(buffer)) != -1) {
op.write(buffer, 0, bytes);
}
}
}
FTP Client:
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
public class ftp_client {
public static void main(final String args[]) {
String server_ip;
String file;
String fn, filenm;
String fc;
String dirn = "c:/FTP CLIENT DIRECTORY";
Socket s;
InputStreamReader in;
OutputStream out = null;
BufferedReader br;
PrintWriter pw = null;
while (true) {
System.out.print("FTP Server Address: ");
server_ip = data_read();
try {
s = new Socket(server_ip, 100);
br = new BufferedReader(new InputStreamReader(s
.getInputStream()));
pw = new PrintWriter(s.getOutputStream(), true);
out = s.getOutputStream();
} catch (final Exception e) {
System.out.println("ERROR : Coult not connect to the server \"" + server_ip + "\"");
break;
}
System.out.print("File to upload: ");
file = data_read();
try {
filenm = file;
pw.println(filenm);
final FileInputStream fis = new FileInputStream(filenm);
final byte[] buffer = new byte[1024];
int bytes = 0;
while ((bytes = fis.read(buffer)) != -1) {
out.write(buffer, 0, bytes);
}
fis.close();
} catch (final Exception exx) {
System.out.println(exx.getMessage());
}
}
}
// Read in from the user
public static String data_read() {
String str = null;
BufferedReader br_key = new BufferedReader(new InputStreamReader(
System.in));
try {
str = br_key.readLine(); // save text that was typed to the
// string "key"
} catch (IOException ioe) {
System.exit(1); // program exits on error
}
return str;
}
}