Results 1 to 3 of 3
  1. #1
    simontkk2005 is offline Member
    Join Date
    Oct 2009
    Posts
    6
    Rep Power
    0

    Default simple FTP server and FTP client

    hi to all expert from java-forum. i`m a new user,and currently a student here.

    well..i need help. i hv problem wiht my ftp transfer. i found my file crash after it deliver to server side. can anyone help me.

    can anyone help me to solve this problmes?? thx
    Last edited by simontkk2005; 11-17-2010 at 05:33 AM.

  2. #2
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    368
    Rep Power
    5

    Default

    i found my file crash after it deliver to server side.
    ?

    Please provide some details.

    Post some code and errors and exceptions if any.

  3. #3
    simontkk2005 is offline Member
    Join Date
    Oct 2009
    Posts
    6
    Rep Power
    0

    Default

    //server

    //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);
    }
    }
    }




    //client

    //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="";
    String dirn1="";
    //String dirn="c:/FTP CLIENT DIRECTORY";
    JPanel pnl;
    JLabel lbltle,lblud;
    Font fnt;
    JTextField txtfn;
    JButton btnu,btnd,btnselect;
    Socket s;
    InputStreamReader in;
    OutputStream out;
    BufferedReader br;
    PrintWriter pw;

    JTextField _fileNameTF = new JTextField(15);
    JTextField _wordCountTF = new JTextField(4);
    JFileChooser _fileChooser = new JFileChooser();


    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);

    btnselect = new JButton("Select");
    btnselect.setBounds(150,150,170,35);
    pnl.add(btnselect);


    btnu.addActionListener(this);
    btnd.addActionListener(this);
    btnselect.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()==btnselect)
    {

    System.out.println("haha");

    JFileChooser chooser = new JFileChooser();
    chooser.showOpenDialog(this);
    File file = chooser.getSelectedFile();
    System.out.println(file);


    dirn1= file.getName();
    System.out.println("first" + dirn1);

    dirn = file.getAbsolutePath(); ///get file path
    System.out.println("second" + dirn);

    txtfn.setText(dirn1);

    // _fileNameTF = file;
    // int retval = _fileChooser.showOpenDialog(CountWords.this);
    // if (retval == JFileChooser.APPROVE_OPTION) {
    //... The user selected a file, get it, use it.
    //File file = _fileChooser.getSelectedFile();
    //System.out.println(file);

    //... Update user interface.
    // _fileNameTF.setText(file.getName());
    // _wordCountTF.setText("" + countWordsInFile(file));
    // }

    }





    if(e.getSource()==btnu)
    {
    try
    {
    filenm=txtfn.getText();

    System.out.println(filenm);
    //filenm="testing.txt";
    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); //directory---need to upgrade it
    if(!dir.exists())
    {
    dir.mkdir();
    }
    else{}
    boolean done=true;
    filenm=txtfn.getText(); /// the name of the file---upgrade into imageview



    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();
    }
    }





    after i send the file from client to server, i found that the file was crash,and it couldnt be open.

Similar Threads

  1. Simple Client (2) - Server program
    By Reztem in forum New To Java
    Replies: 2
    Last Post: 01-09-2012, 02:05 AM
  2. Simple Socket program: Java Client- C server
    By pimmling in forum New To Java
    Replies: 1
    Last Post: 11-08-2010, 01:27 PM
  3. Simple server-client
    By DC200 in forum New To Java
    Replies: 6
    Last Post: 12-09-2009, 05:13 PM
  4. Simple server/client text problem
    By Singing Boyo in forum New To Java
    Replies: 5
    Last Post: 06-02-2009, 10:33 AM
  5. Simple example Client Server Application
    By ferosh in forum Networking
    Replies: 1
    Last Post: 04-01-2007, 10:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •