Java file upload to remote pc

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #16
    Originally posted by crazystone82
    yes i get the same nullpointer exception error on the same line

    if i run the program it displays

    source :
    destination:
    when i entered d:/source.txt for source and

    http://ws111/share/destination.txt for destionation and
    click submit button. it fires the confirmation dialog box as success
    but after that it throws that null pointer exception

    Can you run this one and then post the line that is reported to have the exception


    Code:
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;
    import java.net.*;
    public class remotefileupload implements ActionListener {
     JTextField sourcefield,destinationfield;
     JLabel sourceLabel,destinationLabel;
     JButton submit;
     FileInputStream fin;
     FileOutputStream fout;
     URL destinationurl;
     URLConnection connection;
     OutputStreamWriter out;
     BufferedReader in;
     remotefileupload() {
     //WindowUtilities.setNativeLookAndFeel();
       JFrame f = new JFrame("This is a test");
       f.setSize(300,300);
       Container content = f.getContentPane();
       content.setBackground(Color.white);
       content.setLayout(null);
       sourceLabel=new JLabel("Source Path");
       sourceLabel.setBounds(10,20,70,20);
      sourcefield=new JTextField(30);
      sourcefield.setBounds(90,20,90,20);
      destinationLabel=new JLabel("Destination");
       destinationLabel.setBounds(10,50,70,20);
      destinationfield=new JTextField(30);
      destinationfield.setBounds(90,50,90,20);
       submit=new JButton("Submit");
      submit.setBounds(40,80,90,30);
      content.add(sourceLabel);
       content.add(sourcefield);
       content.add(destinationLabel);
       content.add(destinationfield);
      content.add(submit);
      submit.addActionListener(this);
      f.addWindowListener(new WindowAdapter() {
       public void windowClosing(WindowEvent e) {
       System.exit(0);
       }});
       f.setVisible(true);
       //f.addWindowListener(new ExitListener());
     }
     public void actionPerformed(ActionEvent ae) {
      int i;
      String str=ae.getActionCommand();
      if(str.equals("Submit")) {
       try {
    	fin = new FileInputStream(sourcefield.getText());
    	in = new BufferedReader(new InputStreamReader(fin));
    	//fout =new FileOutputStream(destinationfield.getText());
    	String destination=destinationfield.getText();
    	destinationurl = new URL(destination);
    	connection = destinationurl.openConnection();
    	connection.setDoOutput(true);
    	//connection.connect();
    	out = new OutputStreamWriter(connection.getOutputStream());
    	do {
    	 i = fin.read();
    	 if(i != -1)
    	  out.write(i);
    	}while(i != -1);
    	JOptionPane.showConfirmDialog(null,"success", "success", JOptionPane.YES_NO_OPTION);
    	fin.close();
    	out.close();
    	in.close();
       }
       catch(FileNotFoundException e) {
    	System.out.println("Input File not found");
    	return;
       }
       catch (MalformedURLException e) {
    	e.printStackTrace(); // new URL() failed
       }
       catch(ArrayIndexOutOfBoundsException e) {
    	System.out.println(" Copy file frm to");
    	return;
       }
       catch(IOException e) {
    	e.printStackTrace();
       }
      }
     }
     public static void main(String[] args)
     {
       new remotefileupload();
    
     }
    }

    Comment

    • crazystone82
      New Member
      • Dec 2006
      • 15

      #17
      hi,

      i execute the code it not throws any error but the file is not upload at the destination pc.please review the code for further modifications

      Code:
       
      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;
      import java.io.*;
      import java.net.*;
      public class remotefileupload implements ActionListener {
       JTextField sourcefield,destinationfield;
       JLabel sourceLabel,destinationLabel;
       JButton submit;
       FileInputStream fin;
       FileOutputStream fout;
       URL destinationurl;
       URLConnection connection;
       OutputStreamWriter out;
       BufferedReader in;
       remotefileupload() {
       //WindowUtilities.setNativeLookAndFeel();
         JFrame f = new JFrame("This is a test");
         f.setSize(300,300);
         Container content = f.getContentPane();
         content.setBackground(Color.white);
         content.setLayout(null);
         sourceLabel=new JLabel("Source Path");
         sourceLabel.setBounds(10,20,70,20);
        sourcefield=new JTextField(30);
        sourcefield.setBounds(90,20,90,20);
        destinationLabel=new JLabel("Destination");
         destinationLabel.setBounds(10,50,70,20);
        destinationfield=new JTextField(30);
        destinationfield.setBounds(90,50,90,20);
         submit=new JButton("Submit");
        submit.setBounds(40,80,90,30);
        content.add(sourceLabel);
         content.add(sourcefield);
         content.add(destinationLabel);
         content.add(destinationfield);
        content.add(submit);
        submit.addActionListener(this);
        f.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
         System.exit(0);
         }});
         f.setVisible(true);
         //f.addWindowListener(new ExitListener());
       }
       public void actionPerformed(ActionEvent ae) {
        int i;
        String str=ae.getActionCommand();
        if(str.equals("Submit")) {
         try {
      	fin = new FileInputStream(sourcefield.getText());
      	in = new BufferedReader(new InputStreamReader(fin));
      	//fout =new FileOutputStream(destinationfield.getText());
      	String destination=destinationfield.getText();
      	destinationurl = new URL(destination);
      	connection = destinationurl.openConnection();
      	connection.setDoOutput(true);
      	//connection.connect();
      	out = new OutputStreamWriter(connection.getOutputStream());
      	do {
      	 i = fin.read();
      	 if(i != -1)
      	  out.write(i);
      	}while(i != -1);
      	JOptionPane.showConfirmDialog(null,"success", "success", JOptionPane.YES_NO_OPTION);
      	fin.close();
      	out.close();
      	in.close();
         }
         catch(FileNotFoundException e) {
      	System.out.println("Input File not found");
      	return;
         }
         catch (MalformedURLException e) {
      	e.printStackTrace(); // new URL() failed
         }
         catch(ArrayIndexOutOfBoundsException e) {
      	System.out.println(" Copy file frm to");
      	return;
         }
         catch(IOException e) {
      	e.printStackTrace();
         }
        }
       }
       public static void main(String[] args)
       {
         new remotefileupload();
      
       }
      }

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #18
        Originally posted by crazystone82
        hi,

        i execute the code it not throws any error but the file is not upload at the destination pc.please review the code for further modifications

        Code:
         
        import java.awt.*;
        import java.awt.event.*;
        import javax.swing.*;
        import java.io.*;
        import java.net.*;
        public class remotefileupload implements ActionListener {
        JTextField sourcefield,destinationfield;
        JLabel sourceLabel,destinationLabel;
        JButton submit;
        FileInputStream fin;
        FileOutputStream fout;
        URL destinationurl;
        URLConnection connection;
        OutputStreamWriter out;
        BufferedReader in;
        remotefileupload() {
        //WindowUtilities.setNativeLookAndFeel();
        JFrame f = new JFrame("This is a test");
        f.setSize(300,300);
        Container content = f.getContentPane();
        content.setBackground(Color.white);
        content.setLayout(null);
        sourceLabel=new JLabel("Source Path");
        sourceLabel.setBounds(10,20,70,20);
        sourcefield=new JTextField(30);
        sourcefield.setBounds(90,20,90,20);
        destinationLabel=new JLabel("Destination");
        destinationLabel.setBounds(10,50,70,20);
        destinationfield=new JTextField(30);
        destinationfield.setBounds(90,50,90,20);
        submit=new JButton("Submit");
        submit.setBounds(40,80,90,30);
        content.add(sourceLabel);
        content.add(sourcefield);
        content.add(destinationLabel);
        content.add(destinationfield);
        content.add(submit);
        submit.addActionListener(this);
        f.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
        System.exit(0);
        }});
        f.setVisible(true);
        //f.addWindowListener(new ExitListener());
        }
        public void actionPerformed(ActionEvent ae) {
        int i;
        String str=ae.getActionCommand();
        if(str.equals("Submit")) {
        try {
        	fin = new FileInputStream(sourcefield.getText());
        	in = new BufferedReader(new InputStreamReader(fin));
        	//fout =new FileOutputStream(destinationfield.getText());
        	String destination=destinationfield.getText();
        	destinationurl = new URL(destination);
        	connection = destinationurl.openConnection();
        	connection.setDoOutput(true);
        	//connection.connect();
        	out = new OutputStreamWriter(connection.getOutputStream());
        	do {
        	 i = fin.read();
        	 if(i != -1)
        	 out.write(i);
        	}while(i != -1);
        	JOptionPane.showConfirmDialog(null,"success", "success", JOptionPane.YES_NO_OPTION);
        	fin.close();
        	out.close();
        	in.close();
        }
        catch(FileNotFoundException e) {
        	System.out.println("Input File not found");
        	return;
        }
        catch (MalformedURLException e) {
        	e.printStackTrace(); // new URL() failed
        }
        catch(ArrayIndexOutOfBoundsException e) {
        	System.out.println(" Copy file frm to");
        	return;
        }
        catch(IOException e) {
        	e.printStackTrace();
        }
        }
        }
        public static void main(String[] args)
        {
        new remotefileupload();
         
        }
        }
        Why are not you making use of the buffered reader in your code even if you declared it?

        First test to see if you are reading anything at all by




        Code:
         
        import java.awt.*;
        import java.awt.event.*;
        import javax.swing.*;
        import java.io.*;
        import java.net.*;
        public class remotefileupload implements ActionListener {
         JTextField sourcefield,destinationfield;
         JLabel sourceLabel,destinationLabel;
         JButton submit;
         FileInputStream fin;
         FileOutputStream fout;
         URL destinationurl;
         URLConnection connection;
         OutputStreamWriter out;
         BufferedReader in;
         remotefileupload() {
         //WindowUtilities.setNativeLookAndFeel();
           JFrame f = new JFrame("This is a test");
           f.setSize(300,300);
           Container content = f.getContentPane();
           content.setBackground(Color.white);
           content.setLayout(null);
           sourceLabel=new JLabel("Source Path");
           sourceLabel.setBounds(10,20,70,20);
          sourcefield=new JTextField(30);
          sourcefield.setBounds(90,20,90,20);
          destinationLabel=new JLabel("Destination");
           destinationLabel.setBounds(10,50,70,20);
          destinationfield=new JTextField(30);
          destinationfield.setBounds(90,50,90,20);
           submit=new JButton("Submit");
          submit.setBounds(40,80,90,30);
          content.add(sourceLabel);
           content.add(sourcefield);
           content.add(destinationLabel);
           content.add(destinationfield);
          content.add(submit);
          submit.addActionListener(this);
          f.addWindowListener(new WindowAdapter() {
           public void windowClosing(WindowEvent e) {
           System.exit(0);
           }});
           f.setVisible(true);
           //f.addWindowListener(new ExitListener());
         }
         public void actionPerformed(ActionEvent ae) {
          int i;
          String str=ae.getActionCommand();
          if(str.equals("Submit")) {
           try {
        	fin = new FileInputStream(sourcefield.getText());
        	in = new BufferedReader(new InputStreamReader(fin));
        	//fout =new FileOutputStream(destinationfield.getText());
        	String destination=destinationfield.getText();
        	destinationurl = new URL(destination);
        	connection = destinationurl.openConnection();
        	connection.setDoOutput(true);
        	//connection.connect();
        	out = new OutputStreamWriter(connection.getOutputStream());
        	String line;
        	while ((line = in.readLine()) != null) {
        	 System.out.println(line);//Just read the lines and print them.
        	}
        	//do {
        	// i = fin.read();
        	// if(i != -1)
        	//  out.write(i);
        	//}while(i != -1);
        	JOptionPane.showConfirmDialog(null,"success", "success", JOptionPane.YES_NO_OPTION);
        	fin.close();
        	out.close();
        	in.close();
           }
           catch(FileNotFoundException e) {
        	System.out.println("Input File not found");
        	return;
           }
           catch (MalformedURLException e) {
        	e.printStackTrace(); // new URL() failed
           }
           catch(ArrayIndexOutOfBoundsException e) {
        	System.out.println(" Copy file frm to");
        	return;
           }
           catch(IOException e) {
        	e.printStackTrace();
           }
          }
         }
         public static void main(String[] args)
         {
           new remotefileupload();
        
         }
        }

        Comment

        • crazystone82
          New Member
          • Dec 2006
          • 15

          #19
          i tested that program it reads the content of file and displays it on the system console of the source machine



          Code:
           
          import java.awt.*;
          import java.awt.event.*;
          import javax.swing.*;
          import java.io.*;
          import java.net.*;
          public class remotefileupload implements ActionListener {
           JTextField sourcefield,destinationfield;
           JLabel sourceLabel,destinationLabel;
           JButton submit;
           FileInputStream fin;
           FileOutputStream fout;
           URL destinationurl;
           URLConnection connection;
           OutputStreamWriter out;
           BufferedReader in;
           remotefileupload() {
           //WindowUtilities.setNativeLookAndFeel();
             JFrame f = new JFrame("This is a test");
             f.setSize(300,300);
             Container content = f.getContentPane();
             content.setBackground(Color.white);
             content.setLayout(null);
             sourceLabel=new JLabel("Source Path");
             sourceLabel.setBounds(10,20,70,20);
            sourcefield=new JTextField(30);
            sourcefield.setBounds(90,20,90,20);
            destinationLabel=new JLabel("Destination");
             destinationLabel.setBounds(10,50,70,20);
            destinationfield=new JTextField(30);
            destinationfield.setBounds(90,50,90,20);
             submit=new JButton("Submit");
            submit.setBounds(40,80,90,30);
            content.add(sourceLabel);
             content.add(sourcefield);
             content.add(destinationLabel);
             content.add(destinationfield);
            content.add(submit);
            submit.addActionListener(this);
            f.addWindowListener(new WindowAdapter() {
             public void windowClosing(WindowEvent e) {
             System.exit(0);
             }});
             f.setVisible(true);
             //f.addWindowListener(new ExitListener());
           }
           public void actionPerformed(ActionEvent ae) {
            int i;
            String str=ae.getActionCommand();
            if(str.equals("Submit")) {
             try {
          	fin = new FileInputStream(sourcefield.getText());
          	in = new BufferedReader(new InputStreamReader(fin));
          	//fout =new FileOutputStream(destinationfield.getText());
          	String destination=destinationfield.getText();
          	destinationurl = new URL(destination);
          	connection = destinationurl.openConnection();
          	connection.setDoOutput(true);
          	//connection.connect();
          	out = new OutputStreamWriter(connection.getOutputStream());
          	String line;
          	while ((line = in.readLine()) != null) {
          	 System.out.println(line);//Just read the lines and print them.
          	}
          	//do {
          	// i = fin.read();
          	// if(i != -1)
          	//  out.write(i);
          	//}while(i != -1);
          	JOptionPane.showConfirmDialog(null,"success", "success", JOptionPane.YES_NO_OPTION);
          	fin.close();
          	out.close();
          	in.close();
             }
             catch(FileNotFoundException e) {
          	System.out.println("Input File not found");
          	return;
             }
             catch (MalformedURLException e) {
          	e.printStackTrace(); // new URL() failed
             }
             catch(ArrayIndexOutOfBoundsException e) {
          	System.out.println(" Copy file frm to");
          	return;
             }
             catch(IOException e) {
          	e.printStackTrace();
             }
            }
           }
           public static void main(String[] args)
           {
             new remotefileupload();
          
           }
          }

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #20
            Originally posted by crazystone82
            i tested that program it reads the content of file and displays it on the system console of the source machine



            Code:
             
            import java.awt.*;
            import java.awt.event.*;
            import javax.swing.*;
            import java.io.*;
            import java.net.*;
            public class remotefileupload implements ActionListener {
            JTextField sourcefield,destinationfield;
            JLabel sourceLabel,destinationLabel;
            JButton submit;
            FileInputStream fin;
            FileOutputStream fout;
            URL destinationurl;
            URLConnection connection;
            OutputStreamWriter out;
            BufferedReader in;
            remotefileupload() {
            //WindowUtilities.setNativeLookAndFeel();
            JFrame f = new JFrame("This is a test");
            f.setSize(300,300);
            Container content = f.getContentPane();
            content.setBackground(Color.white);
            content.setLayout(null);
            sourceLabel=new JLabel("Source Path");
            sourceLabel.setBounds(10,20,70,20);
            sourcefield=new JTextField(30);
            sourcefield.setBounds(90,20,90,20);
            destinationLabel=new JLabel("Destination");
            destinationLabel.setBounds(10,50,70,20);
            destinationfield=new JTextField(30);
            destinationfield.setBounds(90,50,90,20);
            submit=new JButton("Submit");
            submit.setBounds(40,80,90,30);
            content.add(sourceLabel);
            content.add(sourcefield);
            content.add(destinationLabel);
            content.add(destinationfield);
            content.add(submit);
            submit.addActionListener(this);
            f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
            System.exit(0);
            }});
            f.setVisible(true);
            //f.addWindowListener(new ExitListener());
            }
            public void actionPerformed(ActionEvent ae) {
            int i;
            String str=ae.getActionCommand();
            if(str.equals("Submit")) {
            try {
            	fin = new FileInputStream(sourcefield.getText());
            	in = new BufferedReader(new InputStreamReader(fin));
            	//fout =new FileOutputStream(destinationfield.getText());
            	String destination=destinationfield.getText();
            	destinationurl = new URL(destination);
            	connection = destinationurl.openConnection();
            	connection.setDoOutput(true);
            	//connection.connect();
            	out = new OutputStreamWriter(connection.getOutputStream());
            	String line;
            	while ((line = in.readLine()) != null) {
            	 System.out.println(line);//Just read the lines and print them.
            	}
            	//do {
            	// i = fin.read();
            	// if(i != -1)
            	// out.write(i);
            	//}while(i != -1);
            	JOptionPane.showConfirmDialog(null,"success", "success", JOptionPane.YES_NO_OPTION);
            	fin.close();
            	out.close();
            	in.close();
            }
            catch(FileNotFoundException e) {
            	System.out.println("Input File not found");
            	return;
            }
            catch (MalformedURLException e) {
            	e.printStackTrace(); // new URL() failed
            }
            catch(ArrayIndexOutOfBoundsException e) {
            	System.out.println(" Copy file frm to");
            	return;
            }
            catch(IOException e) {
            	e.printStackTrace();
            }
            }
            }
            public static void main(String[] args)
            {
            new remotefileupload();
             
            }
            }
            Now you want to test if you can create the file on the remote machine. Try this

            Code:
             
            import java.awt.*;
            import java.awt.event.*;
            import javax.swing.*;
            import java.io.*;
            import java.net.*;
            public class remotefileupload implements ActionListener {
             JTextField sourcefield,destinationfield;
             JLabel sourceLabel,destinationLabel;
             JButton submit;
             FileInputStream fin;
             FileOutputStream fout;
             URL destinationurl;
             URLConnection connection;
             OutputStreamWriter out;
             BufferedReader in;
             remotefileupload() {
             //WindowUtilities.setNativeLookAndFeel();
               JFrame f = new JFrame("This is a test");
               f.setSize(300,300);
               Container content = f.getContentPane();
               content.setBackground(Color.white);
               content.setLayout(null);
               sourceLabel=new JLabel("Source Path");
               sourceLabel.setBounds(10,20,70,20);
              sourcefield=new JTextField(30);
              sourcefield.setBounds(90,20,90,20);
              destinationLabel=new JLabel("Destination");
               destinationLabel.setBounds(10,50,70,20);
              destinationfield=new JTextField(30);
              destinationfield.setBounds(90,50,90,20);
               submit=new JButton("Submit");
              submit.setBounds(40,80,90,30);
              content.add(sourceLabel);
               content.add(sourcefield);
               content.add(destinationLabel);
               content.add(destinationfield);
              content.add(submit);
              submit.addActionListener(this);
              f.addWindowListener(new WindowAdapter() {
               public void windowClosing(WindowEvent e) {
               System.exit(0);
               }});
               f.setVisible(true);
               //f.addWindowListener(new ExitListener());
             }
             public void actionPerformed(ActionEvent ae) {
              int i;
              String str=ae.getActionCommand();
              if(str.equals("Submit")) {
               try {
            	fin = new FileInputStream(sourcefield.getText());
            	in = new BufferedReader(new InputStreamReader(fin));
            	;
            	String destination=destinationfield.getText();
            	destinationurl = new URL(destination);
            	connection = destinationurl.openConnection();
            	connection.setDoOutput(true);
            	//connection.connect();
            	out = new OutputStreamWriter(connection.getOutputStream());
            	fout = new FileOutputStream(destinationfield.getText());
            	String line;
            	while ((line = in.readLine()) != null) {
            	 System.out.println(line);//Just read the lines and print them.
            	}
            	//do {
            	// i = fin.read();
            	// if(i != -1)
            	//  out.write(i);
            	//}while(i != -1);
            	JOptionPane.showConfirmDialog(null,"success", "success", JOptionPane.YES_NO_OPTION);
            	fin.close();
            	out.close();
            	in.close();
               }
               catch(FileNotFoundException e) {
            	System.out.println("Input File not found");
            	return;
               }
               catch (MalformedURLException e) {
            	e.printStackTrace(); // new URL() failed
               }
               catch(ArrayIndexOutOfBoundsException e) {
            	System.out.println(" Copy file frm to");
            	return;
               }
               catch(IOException e) {
            	e.printStackTrace();
               }
              }
             }
             public static void main(String[] args)
             {
               new remotefileupload();
            
             }
            }

            and see if the blank file is getting created

            Comment

            • crazystone82
              New Member
              • Dec 2006
              • 15

              #21
              if i run the program i got the Exception "input file not found"

              Code:
               
              import java.awt.*;
              import java.awt.event.*;
              import javax.swing.*;
              import java.io.*;
              import java.net.*;
              public class remotefileupload implements ActionListener {
               JTextField sourcefield,destinationfield;
               JLabel sourceLabel,destinationLabel;
               JButton submit;
               FileInputStream fin;
               FileOutputStream fout;
               URL destinationurl;
               URLConnection connection;
               OutputStreamWriter out;
               BufferedReader in;
               remotefileupload() {
               //WindowUtilities.setNativeLookAndFeel();
                 JFrame f = new JFrame("This is a test");
                 f.setSize(300,300);
                 Container content = f.getContentPane();
                 content.setBackground(Color.white);
                 content.setLayout(null);
                 sourceLabel=new JLabel("Source Path");
                 sourceLabel.setBounds(10,20,70,20);
                sourcefield=new JTextField(30);
                sourcefield.setBounds(90,20,90,20);
                destinationLabel=new JLabel("Destination");
                 destinationLabel.setBounds(10,50,70,20);
                destinationfield=new JTextField(30);
                destinationfield.setBounds(90,50,90,20);
                 submit=new JButton("Submit");
                submit.setBounds(40,80,90,30);
                content.add(sourceLabel);
                 content.add(sourcefield);
                 content.add(destinationLabel);
                 content.add(destinationfield);
                content.add(submit);
                submit.addActionListener(this);
                f.addWindowListener(new WindowAdapter() {
                 public void windowClosing(WindowEvent e) {
                 System.exit(0);
                 }});
                 f.setVisible(true);
                 //f.addWindowListener(new ExitListener());
               }
               public void actionPerformed(ActionEvent ae) {
                int i;
                String str=ae.getActionCommand();
                if(str.equals("Submit")) {
                 try {
              	fin = new FileInputStream(sourcefield.getText());
              	in = new BufferedReader(new InputStreamReader(fin));
              	;
              	String destination=destinationfield.getText();
              	destinationurl = new URL(destination);
              	connection = destinationurl.openConnection();
              	connection.setDoOutput(true);
              	//connection.connect();
              	out = new OutputStreamWriter(connection.getOutputStream());
              	fout = new FileOutputStream(destinationfield.getText());
              	String line;
              	while ((line = in.readLine()) != null) {
              	 System.out.println(line);//Just read the lines and print them.
              	}
              	//do {
              	// i = fin.read();
              	// if(i != -1)
              	//  out.write(i);
              	//}while(i != -1);
              	JOptionPane.showConfirmDialog(null,"success", "success", JOptionPane.YES_NO_OPTION);
              	fin.close();
              	out.close();
              	in.close();
                 }
                 catch(FileNotFoundException e) {
              	System.out.println("Input File not found");
              	return;
                 }
                 catch (MalformedURLException e) {
              	e.printStackTrace(); // new URL() failed
                 }
                 catch(ArrayIndexOutOfBoundsException e) {
              	System.out.println(" Copy file frm to");
              	return;
                 }
                 catch(IOException e) {
              	e.printStackTrace();
                 }
                }
               }
               public static void main(String[] args)
               {
                 new remotefileupload();
              
               }
              }

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #22
                Originally posted by crazystone82
                if i run the program i got the Exception "input file not found"

                Code:
                 
                import java.awt.*;
                import java.awt.event.*;
                import javax.swing.*;
                import java.io.*;
                import java.net.*;
                public class remotefileupload implements ActionListener {
                JTextField sourcefield,destinationfield;
                JLabel sourceLabel,destinationLabel;
                JButton submit;
                FileInputStream fin;
                FileOutputStream fout;
                URL destinationurl;
                URLConnection connection;
                OutputStreamWriter out;
                BufferedReader in;
                remotefileupload() {
                //WindowUtilities.setNativeLookAndFeel();
                JFrame f = new JFrame("This is a test");
                f.setSize(300,300);
                Container content = f.getContentPane();
                content.setBackground(Color.white);
                content.setLayout(null);
                sourceLabel=new JLabel("Source Path");
                sourceLabel.setBounds(10,20,70,20);
                sourcefield=new JTextField(30);
                sourcefield.setBounds(90,20,90,20);
                destinationLabel=new JLabel("Destination");
                destinationLabel.setBounds(10,50,70,20);
                destinationfield=new JTextField(30);
                destinationfield.setBounds(90,50,90,20);
                submit=new JButton("Submit");
                submit.setBounds(40,80,90,30);
                content.add(sourceLabel);
                content.add(sourcefield);
                content.add(destinationLabel);
                content.add(destinationfield);
                content.add(submit);
                submit.addActionListener(this);
                f.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                System.exit(0);
                }});
                f.setVisible(true);
                //f.addWindowListener(new ExitListener());
                }
                public void actionPerformed(ActionEvent ae) {
                int i;
                String str=ae.getActionCommand();
                if(str.equals("Submit")) {
                try {
                	fin = new FileInputStream(sourcefield.getText());
                	in = new BufferedReader(new InputStreamReader(fin));
                	;
                	String destination=destinationfield.getText();
                	destinationurl = new URL(destination);
                	connection = destinationurl.openConnection();
                	connection.setDoOutput(true);
                	//connection.connect();
                	out = new OutputStreamWriter(connection.getOutputStream());
                	fout = new FileOutputStream(destinationfield.getText());
                	String line;
                	while ((line = in.readLine()) != null) {
                	 System.out.println(line);//Just read the lines and print them.
                	}
                	//do {
                	// i = fin.read();
                	// if(i != -1)
                	// out.write(i);
                	//}while(i != -1);
                	JOptionPane.showConfirmDialog(null,"success", "success", JOptionPane.YES_NO_OPTION);
                	fin.close();
                	out.close();
                	in.close();
                }
                catch(FileNotFoundException e) {
                	System.out.println("Input File not found");
                	return;
                }
                catch (MalformedURLException e) {
                	e.printStackTrace(); // new URL() failed
                }
                catch(ArrayIndexOutOfBoundsException e) {
                	System.out.println(" Copy file frm to");
                	return;
                }
                catch(IOException e) {
                	e.printStackTrace();
                }
                }
                }
                public static void main(String[] args)
                {
                new remotefileupload();
                 
                }
                }
                What had you specified as the destination?
                Run this and check the stack trace to see which file was not found.

                Code:
                 
                import java.awt.*;
                import java.awt.event.*;
                import javax.swing.*;
                import java.io.*;
                import java.net.*;
                public class remotefileupload implements ActionListener {
                 JTextField sourcefield,destinationfield;
                 JLabel sourceLabel,destinationLabel;
                 JButton submit;
                 FileInputStream fin;
                 FileOutputStream fout;
                 URL destinationurl;
                 URLConnection connection;
                 OutputStreamWriter out;
                 BufferedReader in;
                 remotefileupload() {
                 //WindowUtilities.setNativeLookAndFeel();
                   JFrame f = new JFrame("This is a test");
                   f.setSize(300,300);
                   Container content = f.getContentPane();
                   content.setBackground(Color.white);
                   content.setLayout(null);
                   sourceLabel=new JLabel("Source Path");
                   sourceLabel.setBounds(10,20,70,20);
                  sourcefield=new JTextField(30);
                  sourcefield.setBounds(90,20,90,20);
                  destinationLabel=new JLabel("Destination");
                   destinationLabel.setBounds(10,50,70,20);
                  destinationfield=new JTextField(30);
                  destinationfield.setBounds(90,50,90,20);
                   submit=new JButton("Submit");
                  submit.setBounds(40,80,90,30);
                  content.add(sourceLabel);
                   content.add(sourcefield);
                   content.add(destinationLabel);
                   content.add(destinationfield);
                  content.add(submit);
                  submit.addActionListener(this);
                  f.addWindowListener(new WindowAdapter() {
                   public void windowClosing(WindowEvent e) {
                   System.exit(0);
                   }});
                   f.setVisible(true);
                   //f.addWindowListener(new ExitListener());
                 }
                 public void actionPerformed(ActionEvent ae) {
                  int i;
                  String str=ae.getActionCommand();
                  if(str.equals("Submit")) {
                   try {
                	fin = new FileInputStream(sourcefield.getText());
                	in = new BufferedReader(new InputStreamReader(fin));
                	;
                	String destination=destinationfield.getText();
                	destinationurl = new URL(destination);
                	connection = destinationurl.openConnection();
                	connection.setDoOutput(true);
                	//connection.connect();
                	out = new OutputStreamWriter(connection.getOutputStream());
                	fout = new FileOutputStream(destinationfield.getText());
                	String line;
                	while ((line = in.readLine()) != null) {
                	 System.out.println(line);//Just read the lines and print them.
                	}
                	//do {
                	// i = fin.read();
                	// if(i != -1)
                	//  out.write(i);
                	//}while(i != -1);
                	JOptionPane.showConfirmDialog(null,"success", "success", JOptionPane.YES_NO_OPTION);
                	fin.close();
                	out.close();
                	in.close();
                   }
                   catch(FileNotFoundException e) {
                	System.out.println("Input File not found");
                	e.printStackTrace();
                   }
                   catch (MalformedURLException e) {
                	e.printStackTrace(); // new URL() failed
                   }
                   catch(ArrayIndexOutOfBoundsException e) {
                	System.out.println(" Copy file frm to");
                	return;
                   }
                   catch(IOException e) {
                	e.printStackTrace();
                   }
                  }
                 }
                 public static void main(String[] args)
                 {
                   new remotefileupload();
                
                 }
                }

                I'm begginning to think we may need a Socket for this

                Comment

                • crazystone82
                  New Member
                  • Dec 2006
                  • 15

                  #23
                  i got the following error:


                  Input File not found
                  java.io.FileNot FoundException: http:\ws111\Sha re\source.txt (The filename, direc
                  tory name, or volume label syntax is incorrect)
                  at java.io.FileOut putStream.open( Native Method)
                  at java.io.FileOut putStream.<init >(FileOutputStr eam.java:179)
                  at java.io.FileOut putStream.<init >(FileOutputStr eam.java:70)
                  at remotefileuploa d.actionPerform ed(remotefileup load.java:60)
                  at javax.swing.Abs tractButton.fir eActionPerforme d(AbstractButto n.java:17
                  86)
                  at javax.swing.Abs tractButton$For wardActionEvent s.actionPerform ed(Abstra
                  ctButton.java:1 839)
                  at javax.swing.Def aultButtonModel .fireActionPerf ormed(DefaultBu ttonModel
                  .java:420)
                  at javax.swing.Def aultButtonModel .setPressed(Def aultButtonModel .java:258
                  )
                  at javax.swing.pla f.basic.BasicBu ttonListener.mo useReleased(Bas icButtonL
                  istener.java:24 5)
                  at java.awt.Compon ent.processMous eEvent(Componen t.java:5100)
                  at java.awt.Compon ent.processEven t(Component.jav a:4897)
                  at java.awt.Contai ner.processEven t(Container.jav a:1569)
                  at java.awt.Compon ent.dispatchEve ntImpl(Componen t.java:3615)
                  at java.awt.Contai ner.dispatchEve ntImpl(Containe r.java:1627)
                  at java.awt.Compon ent.dispatchEve nt(Component.ja va:3477)
                  at java.awt.Lightw eightDispatcher .retargetMouseE vent(Container. java:3483
                  )
                  at java.awt.Lightw eightDispatcher .processMouseEv ent(Container.j ava:3198)

                  at java.awt.Lightw eightDispatcher .dispatchEvent( Container.java: 3128)
                  at java.awt.Contai ner.dispatchEve ntImpl(Containe r.java:1613)
                  at java.awt.Window .dispatchEventI mpl(Window.java :1606)
                  at java.awt.Compon ent.dispatchEve nt(Component.ja va:3477)
                  at java.awt.EventQ ueue.dispatchEv ent(EventQueue. java:480)
                  at java.awt.EventD ispatchThread.p umpOneEventForH ierarchy(EventD ispatchTh
                  read.java:201)
                  at java.awt.EventD ispatchThread.p umpEventsForHie rarchy(EventDis patchThre
                  ad.java:151)
                  at java.awt.EventD ispatchThread.p umpEvents(Event DispatchThread. java:145)

                  at java.awt.EventD ispatchThread.p umpEvents(Event DispatchThread. java:137)

                  at java.awt.EventD ispatchThread.r un(EventDispatc hThread.java:10 0)



                  Actually we need to send the file to remote pc by specifing their path in destination textbox.if we use socket then we should open the socket from client side also and we install client program on all the client machine we not need copy the file in all client machine,so we need different solution other then sockets to accept datai.e dynamically the destination pc accept the data

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #24
                    Originally posted by crazystone82
                    i got the following error:


                    Input File not found
                    java.io.FileNot FoundException: http:\ws111\Sha re\source.txt (The filename, direc
                    tory name, or volume label syntax is incorrect)
                    at java.io.FileOut putStream.open( Native Method)
                    at java.io.FileOut putStream.<init >(FileOutputStr eam.java:179)
                    at java.io.FileOut putStream.<init >(FileOutputStr eam.java:70)
                    at remotefileuploa d.actionPerform ed(remotefileup load.java:60)
                    at javax.swing.Abs tractButton.fir eActionPerforme d(AbstractButto n.java:17
                    86)
                    at javax.swing.Abs tractButton$For wardActionEvent s.actionPerform ed(Abstra
                    ctButton.java:1 839)
                    at javax.swing.Def aultButtonModel .fireActionPerf ormed(DefaultBu ttonModel
                    .java:420)
                    at javax.swing.Def aultButtonModel .setPressed(Def aultButtonModel .java:258
                    )
                    at javax.swing.pla f.basic.BasicBu ttonListener.mo useReleased(Bas icButtonL
                    istener.java:24 5)
                    at java.awt.Compon ent.processMous eEvent(Componen t.java:5100)
                    at java.awt.Compon ent.processEven t(Component.jav a:4897)
                    at java.awt.Contai ner.processEven t(Container.jav a:1569)
                    at java.awt.Compon ent.dispatchEve ntImpl(Componen t.java:3615)
                    at java.awt.Contai ner.dispatchEve ntImpl(Containe r.java:1627)
                    at java.awt.Compon ent.dispatchEve nt(Component.ja va:3477)
                    at java.awt.Lightw eightDispatcher .retargetMouseE vent(Container. java:3483
                    )
                    at java.awt.Lightw eightDispatcher .processMouseEv ent(Container.j ava:3198)

                    at java.awt.Lightw eightDispatcher .dispatchEvent( Container.java: 3128)
                    at java.awt.Contai ner.dispatchEve ntImpl(Containe r.java:1613)
                    at java.awt.Window .dispatchEventI mpl(Window.java :1606)
                    at java.awt.Compon ent.dispatchEve nt(Component.ja va:3477)
                    at java.awt.EventQ ueue.dispatchEv ent(EventQueue. java:480)
                    at java.awt.EventD ispatchThread.p umpOneEventForH ierarchy(EventD ispatchTh
                    read.java:201)
                    at java.awt.EventD ispatchThread.p umpEventsForHie rarchy(EventDis patchThre
                    ad.java:151)
                    at java.awt.EventD ispatchThread.p umpEvents(Event DispatchThread. java:145)

                    at java.awt.EventD ispatchThread.p umpEvents(Event DispatchThread. java:137)

                    at java.awt.EventD ispatchThread.r un(EventDispatc hThread.java:10 0)



                    Actually we need to send the file to remote pc by specifing their path in destination textbox.if we use socket then we should open the socket from client side also and we install client program on all the client machine we not need copy the file in all client machine,so we need different solution other then sockets to accept datai.e dynamically the destination pc accept the data
                    I think this is the proper way to do it.
                    http://java.sun.com/docs/books/tutor...ngWriting.html

                    Try it and tell me how it goes

                    Comment

                    • crazystone82
                      New Member
                      • Dec 2006
                      • 15

                      #25
                      in that also it need one more program that should run on the server and program run on the client.but we not need such servlet implementation we need simple java program

                      Comment

                      Working...