You are on page 1of 3

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, fc, path="C:\\Program Files\\Java\\jdk1.6.0\\bin";
JFrame jf; JPanel jp;
JLabel lb1,lb2;
Font fnt;
TextField txtfn;
JButton btnu, btnd, browse;
Socket s;
InputStreamReader in;
OutputStream out;
BufferedReader br;
PrintWriter pw;

public ftpclient()
{
super("FTP Client");
jp=new JPanel(null);
fnt=new Font("Times New Roman",Font.BOLD,15);
lb1=new JLabel("FTP Client");
lb1.setFont(fnt);
lb1.setBounds(225,35,200,30);
jp.add(lb1);
lb2=new JLabel("Enter Filename");
lb2.setFont(fnt);
lb2.setBounds(100,100,150,33);
jp.add(lb2);
txtfn=new TextField();
txtfn.setBounds(200,100,200,25);
jp.add(txtfn);
browse=new JButton("Browse");
browse.setBounds(400,100,150,25);
jp.add(browse);

btnu=new JButton("Upload");
btnu.setBounds(150,200,120,35);
jp.add(btnu);

btnd=new JButton("Download");
btnd.setBounds(320,200,120,35);
jp.add(btnd);
browse.addActionListener(this);
btnu.addActionListener(this);
btnd.addActionListener(this);
getContentPane().add(jp);
try
{
s=new Socket("10.0.17.10",4083);
br=new BufferedReader(new InputStreamReader(s.getInputStream()));
pw=new PrintWriter(s.getOutputStream(),true);
out=s.getOutputStream();
}
catch(Exception e) { System.out.println("Error"+e.getMessage()); }
}

public void actionPerformed(ActionEvent ae)


{
if(ae.getSource()==btnu)
{
try
{
File dir=new File(path);
filenm=txtfn.getText();
pw.println(filenm);
File f=new File(dir,filenm);
FileInputStream fis=new FileInputStream(f);
byte[] buff=new byte[1024];
int bytes=0;
while((bytes=fis.read(buff))!=-1)
{ out.write(buff,0,bytes); }
fis.close();
} catch(Exception ex)
{ System.out.println(ex.getMessage()); }
this.setVisible(false);
JOptionPane.showMessageDialog(this,"the file was successfully
uploaded","Upload",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}

if(ae.getSource()==btnd)
{
try
{ FileDialog fd= new FileDialog(this, "Save as", FileDialog.SAVE);
fd.setVisible(true);
String path=fd.getDirectory();
File dir=new File(path);
boolean done=true;
filenm=txtfn.getText();
fn=new String("#"+filenm+"#");
pw.println(fn);
File f= new File(dir,fd.getFile());
FileOutputStream fos=new FileOutputStream(f);
DataOutputStream dops = new DataOutputStream(fos);
while(done)
{
fc=br.readLine();
if(fc==null) { done=false; }
else
{ dops.writeChars(fc); }
}
fos.close();
} catch(Exception ex) { }
this.setVisible(false);
JOptionPane.showMessageDialog(this,"The File was
downloaded","Download",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}

if(ae.getSource()==browse)
{
FileDialog fd=new FileDialog(this,"open");
fd.setVisible(true);
path=fd.getDirectory();
txtfn.setText(fd.getFile());
}

public static void main(String args[])


{
ftpclient ftpc=new ftpclient();
ftpc.setSize(600,300);
ftpc.show();
}
}

You might also like