Anda di halaman 1dari 6

//ftpserver.

java
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("******************************************************
**************************");
system.out.println("server started...");
system.out.println("waiting for connections...");
system.out.println("
");
try
{

serversocket s = new serversocket(100);


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 server directory";
public threadedserver(socket i,int c)
{
incoming=i;
counter=c;
}
public void run()
{
try
{

bufferedreader in =new bufferedreader(new


inputstreamreader(incoming.getinputstream()));
printwriter out = new
printwriter(incoming.getoutputstream(), true);
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:
"+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);
// system.out.println(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);
}
}
}

//ftpclient.java

import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.*;
class ftpclient extends jframe implements actionlistener
{
string fn,filenm;
string fc;
string dirn="c:/ftp client directory";
jpanel pnl;
jlabel lbltle,lblud;
font fnt;
jtextfield txtfn;
jbutton btnu,btnd;
socket s;
inputstreamreader in;
outputstream out;
bufferedreader br;
printwriter pw;
public ftpclient()
{
super("ftp client");

pnl=new jpanel(null);

fnt=new font("times new roman",font.bold,25);

lbltle=new jlabel("ftp client");


lbltle.setfont(fnt);
lbltle.setbounds(225,35,200,30);
pnl.add(lbltle);

lblud=new jlabel("enter file-name :");


lblud.setbounds(100,100,150,35);
pnl.add(lblud);

txtfn=new jtextfield();
txtfn.setbounds(300,100,200,25);
pnl.add(txtfn);

btnu=new jbutton("upload");
btnu.setbounds(150,200,120,35);
pnl.add(btnu);

btnd=new jbutton("download");
btnd.setbounds(320,200,120,35);

pnl.add(btnd);

btnu.addactionlistener(this);
btnd.addactionlistener(this);
getcontentpane().add(pnl);

try
{
s=new socket("localhost",100);
br=new bufferedreader(new inputstreamreader(s.getinputstream()));
pw=new printwriter(s.getoutputstream(),true);
out=s.getoutputstream();
}
catch(exception e)
{
system.out.println("exception :"+e.getmessage());
}
}
public void actionperformed(actionevent e)
{
if(e.getsource()==btnu)
{
try
{
filenm=txtfn.gettext();
pw.println(filenm);
fileinputstream fis=new fileinputstream(filenm);
byte[] buffer=new byte[1024];
int bytes=0;

while((bytes=fis.read(buffer))!=-1)
{
out.write(buffer,0,bytes);
}
fis.close();
}
catch(exception exx)
{
system.out.println(exx.getmessage());
}
}

if(e.getsource()==btnd)
{
try
{
file dir=new file(dirn);
if(!dir.exists())
{
dir.mkdir();
}
else{}
boolean done=true;
filenm=txtfn.gettext();
fn=new string("#"+filenm+"#");
//system.out.println(filenm);
pw.println(fn);
file f=new file(dir,filenm);
fileoutputstream fos=new fileoutputstream(f);
dataoutputstream dops=new dataoutputstream(fos);
while(done)
{
fc=br.readline();
if(fc==null)
{
done=false;
}
else
{
dops.writechars(fc);
// system.out.println(fc);

}
}
fos.close();
}

catch(exception exx)
{}

}
}
public static void main(string args[])
{
ftpclient ftpc=new ftpclient();
ftpc.setsize(600,300);
ftpc.show();
}
}

Anda mungkin juga menyukai