0

Following are the server-client programs,,,, in this client can send files and server have to save it,,,,,i want to add progressbar.....plz help me out!!!

client:-

import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class ftpclient extends JFrame implements ActionListener
{
     JButton b1,b2;
    JTextField t1;
 ftpclient()
 {
  b1 =new JButton("Select File");
  b2 = new JButton("Send File to Server");
  t1 = new JTextField(15);
  setLayout(new FlowLayout());
  add(b1);
  add(t1);
  add(b2);
  b1.addActionListener(this);
  b2.addActionListener(this);
 }
 public void actionPerformed(ActionEvent ae)
 {
   if(ae.getSource()==b1)
  {
   FileDialog fd;
   Frame f1=new Frame();
   fd=new FileDialog(f1,"Select File");
   fd.setSize(300,300); 
   fd.setVisible(true);
  t1.setText(fd.getDirectory()+fd.getFile());
   }


  if(ae.getSource()==b2)
  {
    String s=t1.getText();
  try
  {
 FileInputStream f1 = new FileInputStream(s);
   byte b[]=new byte[1024];
   DataInputStream dis1 = new DataInputStream(f1);

  Socket so = new Socket("127.0.0.1",20);
  OutputStream os=so.getOutputStream();
       int a=dis1.available();
       System.out.println(a);
     PrintWriter pw = new PrintWriter(so.getOutputStream(),true );
       pw.println(a);
       pw.flush();





     while((dis1.read(b)) !=-1)
     {

         os.write(b);



     }

     os.flush();
     os.close();
    so.close();

      }
catch(Exception e) { e.printStackTrace(); }
 }    
}
public static void main(String s[])
 {
   new ftpclient().setVisible(true);
 }
}

Server:-

import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class ftpserver extends JFrame implements Runnable,ActionListener
{
  TextArea t1; Thread t;
  Button b1;
  InputStream is;
  JProgressBar c;
  progressbar bro;
    int num=0;
 ftpserver()
 {
   t1=new TextArea();
  add(t1);
 b1=new Button("Save File");
  b1.addActionListener(this);
 add(b1,BorderLayout.SOUTH);
t=new Thread(this);
t.start();
}
 byte b[] = new byte[1024];
public void run()
{

try {
  ServerSocket ss = new ServerSocket(20);
    while(true)
    {
        Socket s = ss.accept();
        System.out.println(s);
       is = s.getInputStream();
      BufferedReader br =new BufferedReader(new InputStreamReader(s.getInputStream()));
       t1.setText(br.readLine());
       is.reset();

       is.read(b);

     String s1=new String(b);
     t1.setText(s1);


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

    }
public void actionPerformed(ActionEvent ae)
 {
   Frame f1=new Frame();
   FileDialog fd = new FileDialog(f1,"Save",FileDialog.SAVE);
 fd.setSize(300,300);
 fd.setVisible(true);
try


 {
  FileOutputStream f=new FileOutputStream(fd.getDirectory()+fd.getFile());


  int k=1;

while((is.read(b)) !=-1)
{  
    f.write(b);
}


 f.close();
 }catch(Exception e){ }
}



public static void main(String s[])
{
  new ftpserver().setVisible(true);
 }
}
2
Contributors
1
Reply
4
Views
4 Years
Discussion Span
Last Post by JamesCherrill
0

Right now it looks like your file transfers run from actionPerformed methods. These are called on the Swing Event Dispatch Thread, and no other Swing code will run until the actionPerformed returns. This includes any code to update the screen with progress bar changes.
So the first thing you have to do is to move your file transfers into a new Thread. Once you have doe that you will be able to update a progress bar as each block is transferred.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.