Java file upload to remote pc

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • crazystone82
    New Member
    • Dec 2006
    • 15

    #1

    Java file upload to remote pc

    Hi all,

    please send me a source code to transfer a file to a server
    running on another pc in the LAN...using JAVA .By getting the source path and destination path through textfield implemented in java swings
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by crazystone82
    Hi all,

    please send me a source code to transfer a file to a server
    running on another pc in the LAN...using JAVA .By getting the source path and destination path through textfield implemented in java swings
    Perhaps you should have read the posting guidelines first.
    You should read a tutorial on how to use the java.net package and the try to write your own code and post it and we can then help from there.

    Comment

    • crazystone82
      New Member
      • Dec 2006
      • 15

      #3
      Java File transfer

      hi all,

      Have written a app to transfer a text file to a server
      running on another pc in the LAN...but not working please correct the code to became it work.th code is

      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
      {
      try
      {
       
      fin = new FileInputStream(sourcefield.getText());
       
      }
      catch(FileNotFoundException e)
      {
      	System.out.println("Input File not found");
      	return;
      }
      try
      {
      //fout =new FileOutputStream(destinationfield.getText());
      String destination=destinationfield.getText();
      destinationurl = new URL("destination");
      connection = destinationurl.openConnection();
      connection.setDoOutput(true);
      //connection.connect();
       
      }
      catch (MalformedURLException e) 
      {
      e.printStackTrace(); // new URL() failed
       
      } 
      catch (IOException e) 
      {
      System.out.println("unable to open");// openConnection() failed
       
      }
       
      /*catch(FileNotFoundException e)
       
      {
      System.out.println("Output File not found");
      return;
      }*/
      }
      catch(ArrayIndexOutOfBoundsException e)
       
      {
      System.out.println(" Copy file frm to");
      return;
      }
       
      try
       
      {
       
      out = new OutputStreamWriter(connection.getOutputStream());
      //in = new BufferedReader(new InputStreamReader(connection.getInputStream(fin)));
      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(IOException e)
      {
      e.printStackTrace();
      }
       
       
      }
       
       
       
      } 
       
       
      public static void main(String[] args) 
      {
      	 new remotefileupload();
       
       
      }
      }

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by crazystone82
        hi all,

        Have written a app to transfer a text file to a server
        running on another pc in the LAN...but not working please correct the code to became it work.th code is

        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
        {
        try
        {
         
        fin = new FileInputStream(sourcefield.getText());
         
        }
        catch(FileNotFoundException e)
        {
        	System.out.println("Input File not found");
        	return;
        }
        try
        {
        //fout =new FileOutputStream(destinationfield.getText());
        String destination=destinationfield.getText();
        destinationurl = new URL("destination");
        connection = destinationurl.openConnection();
        connection.setDoOutput(true);
        //connection.connect();
         
        }
        catch (MalformedURLException e) 
        {
        e.printStackTrace(); // new URL() failed
         
        } 
        catch (IOException e) 
        {
        System.out.println("unable to open");// openConnection() failed
         
        }
         
        /*catch(FileNotFoundException e)
         
        {
        System.out.println("Output File not found");
        return;
        }*/
        }
        catch(ArrayIndexOutOfBoundsException e)
         
        {
        System.out.println(" Copy file frm to");
        return;
        }
         
        try
         
        {
         
        out = new OutputStreamWriter(connection.getOutputStream());
        //in = new BufferedReader(new InputStreamReader(connection.getInputStream(fin)));
        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(IOException e)
        {
        e.printStackTrace();
        }
         
         
        }
         
         
         
        } 
         
         
        public static void main(String[] args) 
        {
        	 new remotefileupload();
         
         
        }
        }
        That's better. You left out the code tags which I've already added for you. What you should also add is an explanation of what problem it is giving. You simply said that it is not working. Where/when is it not working? Explain the problem more precisely.

        Comment

        • crazystone82
          New Member
          • Dec 2006
          • 15

          #5
          if we give the destination as http://ws111/share/copy.txt at run time
          it shows the following error

          java.net.Malfor medURLException : no protocol: destination
          at java.net.URL.<i nit>(URL.java:5 37)
          at java.net.URL.<i nit>(URL.java:4 34)
          at java.net.URL.<i nit>(URL.java:3 83)
          at remotefileuploa d.actionPerform ed(remotefileup load.java:89)
          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)
          java.lang.NullP ointerException
          at remotefileuploa d.actionPerform ed(remotefileup load.java:124)
          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)



          please give me the solution

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by crazystone82
            if we give the destination as http://ws111/share/copy.txt at run time
            it shows the following error

            java.net.Malfor medURLException : no protocol: destination
            at java.net.URL.<i nit>(URL.java:5 37)
            at java.net.URL.<i nit>(URL.java:4 34)
            at java.net.URL.<i nit>(URL.java:3 83)
            at remotefileuploa d.actionPerform ed(remotefileup load.java:89)
            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)
            java.lang.NullP ointerException
            at remotefileuploa d.actionPerform ed(remotefileup load.java:124)
            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)



            please give me the solution
            Well it says your URL is Malformed doesn't it.

            Comment

            • horace1
              Recognized Expert Top Contributor
              • Nov 2006
              • 1510

              #7
              Originally posted by crazystone82
              if we give the destination as http://ws111/share/copy.txt at run time
              it shows the following error

              java.net.Malfor medURLException : no protocol: destination


              please give me the solution
              you get this error because you have " around destination in
              Code:
              destinationurl = new URL("destination");
              it should be?
              Code:
              destinationurl = new URL(destination);

              Comment

              • crazystone82
                New Member
                • Dec 2006
                • 15

                #8
                dear friends,

                i had changed that as per ur corrrection but still there is an saying that

                java.lang.NullP ointerException
                at remotefileuploa d.actionPerform ed(remotefileup load.java:137)
                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)

                please correct that code in order to run correct .its urgent

                Comment

                • crazystone82
                  New Member
                  • Dec 2006
                  • 15

                  #9
                  Java file upload to remote pc

                  hi friends ,

                  i need to upload a file to remote machine.i had use textbox to get source and destination path and when i click submit button it should be uploaded.when i run the program it throws an runtime error.the code is below

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

                  public class remotefileuploa d implements ActionListener
                  {
                  JTextField sourcefield,des tinationfield;
                  JLabel sourceLabel,des tinationLabel;
                  JButton submit;
                  FileInputStream fin;
                  FileOutputStrea m fout;
                  URL destinationurl;
                  URLConnection connection;
                  OutputStreamWri ter out;
                  BufferedReader in;

                  remotefileuploa d()
                  {
                  //WindowUtilities .setNativeLookA ndFeel();
                  JFrame f = new JFrame("This is a test");
                  f.setSize(300,3 00);

                  Container content = f.getContentPan e();
                  content.setBack ground(Color.wh ite);
                  content.setLayo ut(null);

                  sourceLabel=new JLabel("Source Path");
                  sourceLabel.set Bounds(10,20,70 ,20);

                  sourcefield=new JTextField(30);
                  sourcefield.set Bounds(90,20,90 ,20);

                  destinationLabe l=new JLabel("Destina tion");
                  destinationLabe l.setBounds(10, 50,70,20);

                  destinationfiel d=new JTextField(30);
                  destinationfiel d.setBounds(90, 50,90,20);

                  submit=new JButton("Submit ");
                  submit.setBound s(40,80,90,30);

                  content.add(sou rceLabel);
                  content.add(sou rcefield);
                  content.add(des tinationLabel);
                  content.add(des tinationfield);
                  content.add(sub mit);

                  submit.addActio nListener(this) ;

                  f.addWindowList ener(new WindowAdapter() {

                  public void windowClosing(W indowEvent e) {
                  System.exit(0);
                  }
                  });

                  f.setVisible(tr ue);
                  //f.addWindowList ener(new ExitListener()) ;

                  }

                  public void actionPerformed (ActionEvent ae)
                  {
                  int i;

                  String str=ae.getActio nCommand();

                  if(str.equals(" Submit"))
                  {
                  try
                  {
                  try
                  {

                  fin = new FileInputStream (sourcefield.ge tText());

                  }
                  catch(FileNotFo undException e)
                  {
                  System.out.prin tln("Input File not found");
                  return;
                  }
                  try
                  {
                  //fout =new FileOutputStrea m(destinationfi eld.getText());
                  String destination=des tinationfield.g etText();
                  destinationurl = new URL(destination );
                  connection = destinationurl. openConnection( );
                  connection.setD oOutput(true);
                  //connection.conn ect();

                  }
                  catch (MalformedURLEx ception e)
                  {
                  e.printStackTra ce(); // new URL() failed

                  }
                  catch (IOException e)
                  {
                  System.out.prin tln("unable to open");// openConnection( ) failed

                  }

                  /*catch(FileNotF oundException e)

                  {
                  System.out.prin tln("Output File not found");
                  return;
                  }*/
                  }
                  catch(ArrayInde xOutOfBoundsExc eption e)

                  {
                  System.out.prin tln(" Copy file frm to");
                  return;
                  }

                  try

                  {

                  out = new OutputStreamWri ter(connection. getOutputStream ());
                  //in = new BufferedReader( new InputStreamRead er(connection.g etInputStream(f in))) ;
                  do
                  {
                  i = fin.read();
                  if(i != -1)
                  out.write(i);
                  }while(i != -1);
                  JOptionPane.sho wConfirmDialog( null,"success", "success", JOptionPane.YES _NO_OPTION);


                  fin.close();
                  out.close();
                  in.close();
                  }
                  catch(IOExcepti on e)
                  {
                  e.printStackTra ce();
                  }


                  }



                  }


                  public static void main(String[] args)
                  {
                  new remotefileuploa d();


                  }
                  }
                  please modify the code to upload the file correctly to remote url.please its very urgent

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by crazystone82
                    hi friends ,

                    i need to upload a file to remote machine.i had use textbox to get source and destination path and when i click submit button it should be uploaded.when i run the program it throws an runtime error.the code is below

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

                    public class remotefileuploa d implements ActionListener
                    {
                    JTextField sourcefield,des tinationfield;
                    JLabel sourceLabel,des tinationLabel;
                    JButton submit;
                    FileInputStream fin;
                    FileOutputStrea m fout;
                    URL destinationurl;
                    URLConnection connection;
                    OutputStreamWri ter out;
                    BufferedReader in;

                    remotefileuploa d()
                    {
                    //WindowUtilities .setNativeLookA ndFeel();
                    JFrame f = new JFrame("This is a test");
                    f.setSize(300,3 00);

                    Container content = f.getContentPan e();
                    content.setBack ground(Color.wh ite);
                    content.setLayo ut(null);

                    sourceLabel=new JLabel("Source Path");
                    sourceLabel.set Bounds(10,20,70 ,20);

                    sourcefield=new JTextField(30);
                    sourcefield.set Bounds(90,20,90 ,20);

                    destinationLabe l=new JLabel("Destina tion");
                    destinationLabe l.setBounds(10, 50,70,20);

                    destinationfiel d=new JTextField(30);
                    destinationfiel d.setBounds(90, 50,90,20);

                    submit=new JButton("Submit ");
                    submit.setBound s(40,80,90,30);

                    content.add(sou rceLabel);
                    content.add(sou rcefield);
                    content.add(des tinationLabel);
                    content.add(des tinationfield);
                    content.add(sub mit);

                    submit.addActio nListener(this) ;

                    f.addWindowList ener(new WindowAdapter() {

                    public void windowClosing(W indowEvent e) {
                    System.exit(0);
                    }
                    });

                    f.setVisible(tr ue);
                    //f.addWindowList ener(new ExitListener()) ;

                    }

                    public void actionPerformed (ActionEvent ae)
                    {
                    int i;

                    String str=ae.getActio nCommand();

                    if(str.equals(" Submit"))
                    {
                    try
                    {
                    try
                    {

                    fin = new FileInputStream (sourcefield.ge tText());

                    }
                    catch(FileNotFo undException e)
                    {
                    System.out.prin tln("Input File not found");
                    return;
                    }
                    try
                    {
                    //fout =new FileOutputStrea m(destinationfi eld.getText());
                    String destination=des tinationfield.g etText();
                    destinationurl = new URL(destination );
                    connection = destinationurl. openConnection( );
                    connection.setD oOutput(true);
                    //connection.conn ect();

                    }
                    catch (MalformedURLEx ception e)
                    {
                    e.printStackTra ce(); // new URL() failed

                    }
                    catch (IOException e)
                    {
                    System.out.prin tln("unable to open");// openConnection( ) failed

                    }

                    /*catch(FileNotF oundException e)

                    {
                    System.out.prin tln("Output File not found");
                    return;
                    }*/
                    }
                    catch(ArrayInde xOutOfBoundsExc eption e)

                    {
                    System.out.prin tln(" Copy file frm to");
                    return;
                    }

                    try

                    {

                    out = new OutputStreamWri ter(connection. getOutputStream ());
                    //in = new BufferedReader( new InputStreamRead er(connection.g etInputStream(f in))) ;
                    do
                    {
                    i = fin.read();
                    if(i != -1)
                    out.write(i);
                    }while(i != -1);
                    JOptionPane.sho wConfirmDialog( null,"success", "success", JOptionPane.YES _NO_OPTION);


                    fin.close();
                    out.close();
                    in.close();
                    }
                    catch(IOExcepti on e)
                    {
                    e.printStackTra ce();
                    }


                    }



                    }


                    public static void main(String[] args)
                    {
                    new remotefileuploa d();


                    }
                    }
                    please modify the code to upload the file correctly to remote url.please its very urgent
                    DO NOT DOUBLE POST

                    Your last post said you had a nullpointer exception on line 137. Can you post which one is line 137 in your code.

                    Comment

                    • crazystone82
                      New Member
                      • Dec 2006
                      • 15

                      #11
                      sorry for double post

                      the line 137 is


                      in.close();

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        Originally posted by crazystone82
                        sorry for double post

                        the line 137 is


                        in.close();
                        What happens if you run it like 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 {
                        	try {
                        	 fin = new FileInputStream(sourcefield.getText());
                        	}
                        	catch(FileNotFoundException e) {
                        	 System.out.println("Input File not found");
                        	 return;
                        	}
                        	try {
                        	 //fout =new FileOutputStream(destinationfield.getText());
                        	 String destination=destinationfield.getText();
                        	 destinationurl = new URL(destination);
                        	 connection = destinationurl.openConnection();
                        	 connection.setDoOutput(true);
                        	 //connection.connect();
                        	}
                        	catch (MalformedURLException e) {
                        	 e.printStackTrace(); // new URL() failed
                        	}
                        	catch (IOException e) {
                        	 System.out.println("unable to open");// openConnection() failed
                        	}
                        	/*catch(FileNotFoundException e)
                         
                        	{
                        	System.out.println("Output File not found");
                        	return;
                        	}*/
                           }
                           catch(ArrayIndexOutOfBoundsException e) {
                        	System.out.println(" Copy file frm to");
                        	return;
                           }
                           try {
                        	out = new OutputStreamWriter(connection.getOutputStream());
                        	in = new BufferedReader(new InputStreamReader(fin));
                        	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(IOException e) {
                        	e.printStackTrace();
                           }
                          }
                        }
                         
                         
                        public static void main(String[] args)
                        {
                        	 new remotefileupload();
                         
                         
                        }
                        }

                        Comment

                        • crazystone82
                          New Member
                          • Dec 2006
                          • 15

                          #13
                          if i run the program i displays two label boxes as well as textbox that we given in the program.when click the submit button it fires the alert box for confirmation but it also throws the runtime exceptions that i posted in last reply
                          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 {
                          	try {
                          	 fin = new FileInputStream(sourcefield.getText());
                          	}
                          	catch(FileNotFoundException e) {
                          	 System.out.println("Input File not found");
                          	 return;
                          	}
                          	try {
                          	 //fout =new FileOutputStream(destinationfield.getText());
                          	 String destination=destinationfield.getText();
                          	 destinationurl = new URL(destination);
                          	 connection = destinationurl.openConnection();
                          	 connection.setDoOutput(true);
                          	 //connection.connect();
                          	}
                          	catch (MalformedURLException e) {
                          	 e.printStackTrace(); // new URL() failed
                          	}
                          	catch (IOException e) {
                          	 System.out.println("unable to open");// openConnection() failed
                          	}
                          	/*catch(FileNotFoundException e)
                           
                          	{
                          	System.out.println("Output File not found");
                          	return;
                          	}*/
                             }
                             catch(ArrayIndexOutOfBoundsException e) {
                          	System.out.println(" Copy file frm to");
                          	return;
                             }
                             try {
                          	out = new OutputStreamWriter(connection.getOutputStream());
                          	in = new BufferedReader(new InputStreamReader(fin));
                          	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(IOException e) {
                          	e.printStackTrace();
                             }
                            }
                          }
                           
                           
                          public static void main(String[] args)
                          {
                          	 new remotefileupload();
                           
                           
                          }
                          }

                          Comment

                          • r035198x
                            MVP
                            • Sep 2006
                            • 13225

                            #14
                            Originally posted by crazystone82
                            if i run the program i displays two label boxes as well as textbox that we given in the program.when click the submit button it fires the alert box for confirmation but it also throws the runtime exceptions that i posted in last reply
                            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 {
                            	try {
                            	 fin = new FileInputStream(sourcefield.getText());
                            	}
                            	catch(FileNotFoundException e) {
                            	 System.out.println("Input File not found");
                            	 return;
                            	}
                            	try {
                            	 //fout =new FileOutputStream(destinationfield.getText());
                            	 String destination=destinationfield.getText();
                            	 destinationurl = new URL(destination);
                            	 connection = destinationurl.openConnection();
                            	 connection.setDoOutput(true);
                            	 //connection.connect();
                            	}
                            	catch (MalformedURLException e) {
                            	 e.printStackTrace(); // new URL() failed
                            	}
                            	catch (IOException e) {
                            	 System.out.println("unable to open");// openConnection() failed
                            	}
                            	/*catch(FileNotFoundException e)
                             
                            	{
                            	System.out.println("Output File not found");
                            	return;
                            	}*/
                            }
                            catch(ArrayIndexOutOfBoundsException e) {
                            	System.out.println(" Copy file frm to");
                            	return;
                            }
                            try {
                            	out = new OutputStreamWriter(connection.getOutputStream());
                            	in = new BufferedReader(new InputStreamReader(fin));
                            	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(IOException e) {
                            	e.printStackTrace();
                            }
                            }
                            }
                             
                             
                            public static void main(String[] args)
                            {
                            	 new remotefileupload();
                             
                             
                            }
                            }
                            Are you still getting the NullPointer at the same line?

                            Comment

                            • crazystone82
                              New Member
                              • Dec 2006
                              • 15

                              #15
                              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

                              Comment

                              Working...