473,809 Members | 2,757 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Java file upload to remote pc

15 New Member
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
Dec 11 '06
24 7411
crazystone82
15 New Member
sorry for double post

the line 137 is


in.close();
Dec 12 '06 #11
r035198x
13,262 MVP
sorry for double post

the line 137 is


in.close();
What happens if you run it like this?

Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5. import java.io.*;
  6. import java.net.*;
  7.  
  8. public class remotefileupload implements ActionListener {
  9.  JTextField sourcefield,destinationfield;
  10.  JLabel sourceLabel,destinationLabel;
  11.  JButton submit;
  12.  FileInputStream fin;
  13.  FileOutputStream fout;
  14.  URL destinationurl;
  15.  URLConnection connection;
  16.  OutputStreamWriter out;
  17.  BufferedReader in;
  18.  remotefileupload() {
  19.   //WindowUtilities.setNativeLookAndFeel();
  20.       JFrame f = new JFrame("This is a test");
  21.      f.setSize(300,300);
  22.       Container content = f.getContentPane();
  23.       content.setBackground(Color.white);
  24.       content.setLayout(null);
  25.       sourceLabel=new JLabel("Source Path");
  26.       sourceLabel.setBounds(10,20,70,20);
  27.   sourcefield=new JTextField(30);
  28.   sourcefield.setBounds(90,20,90,20);
  29.   destinationLabel=new JLabel("Destination");
  30.      destinationLabel.setBounds(10,50,70,20);
  31.   destinationfield=new JTextField(30);
  32.   destinationfield.setBounds(90,50,90,20);
  33.      submit=new JButton("Submit");
  34.   submit.setBounds(40,80,90,30);
  35.   content.add(sourceLabel);
  36.      content.add(sourcefield);
  37.      content.add(destinationLabel);
  38.      content.add(destinationfield);
  39.   content.add(submit);
  40.  
  41.   submit.addActionListener(this);
  42.   f.addWindowListener(new WindowAdapter() {
  43.       public void windowClosing(WindowEvent e) {
  44.        System.exit(0);
  45.       }});
  46.       f.setVisible(true);
  47.       //f.addWindowListener(new ExitListener());
  48.  }
  49.  public void actionPerformed(ActionEvent ae) {
  50.   int i;
  51.   String str=ae.getActionCommand();
  52.   if(str.equals("Submit")) {
  53.    try {
  54.     try {
  55.      fin = new FileInputStream(sourcefield.getText());
  56.     }
  57.     catch(FileNotFoundException e) {
  58.      System.out.println("Input File not found");
  59.      return;
  60.     }
  61.     try {
  62.      //fout =new FileOutputStream(destinationfield.getText());
  63.      String destination=destinationfield.getText();
  64.      destinationurl = new URL(destination);
  65.      connection = destinationurl.openConnection();
  66.      connection.setDoOutput(true);
  67.      //connection.connect();
  68.     }
  69.     catch (MalformedURLException e) {
  70.      e.printStackTrace(); // new URL() failed
  71.     }
  72.     catch (IOException e) {
  73.      System.out.println("unable to open");// openConnection() failed
  74.     }
  75.     /*catch(FileNotFoundException e)
  76.  
  77.     {
  78.     System.out.println("Output File not found");
  79.     return;
  80.     }*/
  81.    }
  82.    catch(ArrayIndexOutOfBoundsException e) {
  83.     System.out.println(" Copy file frm to");
  84.     return;
  85.    }
  86.    try {
  87.     out = new OutputStreamWriter(connection.getOutputStream());
  88.     in = new BufferedReader(new InputStreamReader(fin));
  89.     do {
  90.      i = fin.read();
  91.      if(i != -1)
  92.       out.write(i);
  93.     }
  94.     while(i != -1);
  95.     JOptionPane.showConfirmDialog(null,"success", "success", JOptionPane.YES_NO_OPTION);
  96.     fin.close();
  97.     out.close();
  98.     in.close();
  99.    }
  100.    catch(IOException e) {
  101.     e.printStackTrace();
  102.    }
  103.   }
  104. }
  105.  
  106.  
  107. public static void main(String[] args)
  108. {
  109.      new remotefileupload();
  110.  
  111.  
  112. }
  113. }
  114.  
  115.  
Dec 12 '06 #12
crazystone82
15 New Member
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
Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5. import java.io.*;
  6. import java.net.*;
  7.  
  8. public class remotefileupload implements ActionListener {
  9.  JTextField sourcefield,destinationfield;
  10.  JLabel sourceLabel,destinationLabel;
  11.  JButton submit;
  12.  FileInputStream fin;
  13.  FileOutputStream fout;
  14.  URL destinationurl;
  15.  URLConnection connection;
  16.  OutputStreamWriter out;
  17.  BufferedReader in;
  18.  remotefileupload() {
  19.   //WindowUtilities.setNativeLookAndFeel();
  20.       JFrame f = new JFrame("This is a test");
  21.      f.setSize(300,300);
  22.       Container content = f.getContentPane();
  23.       content.setBackground(Color.white);
  24.       content.setLayout(null);
  25.       sourceLabel=new JLabel("Source Path");
  26.       sourceLabel.setBounds(10,20,70,20);
  27.   sourcefield=new JTextField(30);
  28.   sourcefield.setBounds(90,20,90,20);
  29.   destinationLabel=new JLabel("Destination");
  30.      destinationLabel.setBounds(10,50,70,20);
  31.   destinationfield=new JTextField(30);
  32.   destinationfield.setBounds(90,50,90,20);
  33.      submit=new JButton("Submit");
  34.   submit.setBounds(40,80,90,30);
  35.   content.add(sourceLabel);
  36.      content.add(sourcefield);
  37.      content.add(destinationLabel);
  38.      content.add(destinationfield);
  39.   content.add(submit);
  40.  
  41.   submit.addActionListener(this);
  42.   f.addWindowListener(new WindowAdapter() {
  43.       public void windowClosing(WindowEvent e) {
  44.        System.exit(0);
  45.       }});
  46.       f.setVisible(true);
  47.       //f.addWindowListener(new ExitListener());
  48.  }
  49.  public void actionPerformed(ActionEvent ae) {
  50.   int i;
  51.   String str=ae.getActionCommand();
  52.   if(str.equals("Submit")) {
  53.    try {
  54.     try {
  55.      fin = new FileInputStream(sourcefield.getText());
  56.     }
  57.     catch(FileNotFoundException e) {
  58.      System.out.println("Input File not found");
  59.      return;
  60.     }
  61.     try {
  62.      //fout =new FileOutputStream(destinationfield.getText());
  63.      String destination=destinationfield.getText();
  64.      destinationurl = new URL(destination);
  65.      connection = destinationurl.openConnection();
  66.      connection.setDoOutput(true);
  67.      //connection.connect();
  68.     }
  69.     catch (MalformedURLException e) {
  70.      e.printStackTrace(); // new URL() failed
  71.     }
  72.     catch (IOException e) {
  73.      System.out.println("unable to open");// openConnection() failed
  74.     }
  75.     /*catch(FileNotFoundException e)
  76.  
  77.     {
  78.     System.out.println("Output File not found");
  79.     return;
  80.     }*/
  81.    }
  82.    catch(ArrayIndexOutOfBoundsException e) {
  83.     System.out.println(" Copy file frm to");
  84.     return;
  85.    }
  86.    try {
  87.     out = new OutputStreamWriter(connection.getOutputStream());
  88.     in = new BufferedReader(new InputStreamReader(fin));
  89.     do {
  90.      i = fin.read();
  91.      if(i != -1)
  92.       out.write(i);
  93.     }
  94.     while(i != -1);
  95.     JOptionPane.showConfirmDialog(null,"success", "success", JOptionPane.YES_NO_OPTION);
  96.     fin.close();
  97.     out.close();
  98.     in.close();
  99.    }
  100.    catch(IOException e) {
  101.     e.printStackTrace();
  102.    }
  103.   }
  104. }
  105.  
  106.  
  107. public static void main(String[] args)
  108. {
  109.      new remotefileupload();
  110.  
  111.  
  112. }
  113. }
  114.  
  115.  
Dec 13 '06 #13
r035198x
13,262 MVP
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
Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5. import java.io.*;
  6. import java.net.*;
  7.  
  8. public class remotefileupload implements ActionListener {
  9. JTextField sourcefield,destinationfield;
  10. JLabel sourceLabel,destinationLabel;
  11. JButton submit;
  12. FileInputStream fin;
  13. FileOutputStream fout;
  14. URL destinationurl;
  15. URLConnection connection;
  16. OutputStreamWriter out;
  17. BufferedReader in;
  18. remotefileupload() {
  19. //WindowUtilities.setNativeLookAndFeel();
  20.      JFrame f = new JFrame("This is a test");
  21.      f.setSize(300,300);
  22.      Container content = f.getContentPane();
  23.      content.setBackground(Color.white);
  24.      content.setLayout(null);
  25.      sourceLabel=new JLabel("Source Path");
  26.      sourceLabel.setBounds(10,20,70,20);
  27. sourcefield=new JTextField(30);
  28. sourcefield.setBounds(90,20,90,20);
  29. destinationLabel=new JLabel("Destination");
  30.      destinationLabel.setBounds(10,50,70,20);
  31. destinationfield=new JTextField(30);
  32. destinationfield.setBounds(90,50,90,20);
  33.      submit=new JButton("Submit");
  34. submit.setBounds(40,80,90,30);
  35. content.add(sourceLabel);
  36.      content.add(sourcefield);
  37.      content.add(destinationLabel);
  38.      content.add(destinationfield);
  39. content.add(submit);
  40.  
  41. submit.addActionListener(this);
  42. f.addWindowListener(new WindowAdapter() {
  43.      public void windowClosing(WindowEvent e) {
  44.      System.exit(0);
  45.      }});
  46.      f.setVisible(true);
  47.      //f.addWindowListener(new ExitListener());
  48. }
  49. public void actionPerformed(ActionEvent ae) {
  50. int i;
  51. String str=ae.getActionCommand();
  52. if(str.equals("Submit")) {
  53. try {
  54.     try {
  55.      fin = new FileInputStream(sourcefield.getText());
  56.     }
  57.     catch(FileNotFoundException e) {
  58.      System.out.println("Input File not found");
  59.      return;
  60.     }
  61.     try {
  62.      //fout =new FileOutputStream(destinationfield.getText());
  63.      String destination=destinationfield.getText();
  64.      destinationurl = new URL(destination);
  65.      connection = destinationurl.openConnection();
  66.      connection.setDoOutput(true);
  67.      //connection.connect();
  68.     }
  69.     catch (MalformedURLException e) {
  70.      e.printStackTrace(); // new URL() failed
  71.     }
  72.     catch (IOException e) {
  73.      System.out.println("unable to open");// openConnection() failed
  74.     }
  75.     /*catch(FileNotFoundException e)
  76.  
  77.     {
  78.     System.out.println("Output File not found");
  79.     return;
  80.     }*/
  81. }
  82. catch(ArrayIndexOutOfBoundsException e) {
  83.     System.out.println(" Copy file frm to");
  84.     return;
  85. }
  86. try {
  87.     out = new OutputStreamWriter(connection.getOutputStream());
  88.     in = new BufferedReader(new InputStreamReader(fin));
  89.     do {
  90.      i = fin.read();
  91.      if(i != -1)
  92.      out.write(i);
  93.     }
  94.     while(i != -1);
  95.     JOptionPane.showConfirmDialog(null,"success", "success", JOptionPane.YES_NO_OPTION);
  96.     fin.close();
  97.     out.close();
  98.     in.close();
  99. }
  100. catch(IOException e) {
  101.     e.printStackTrace();
  102. }
  103. }
  104. }
  105.  
  106.  
  107. public static void main(String[] args)
  108. {
  109.      new remotefileupload();
  110.  
  111.  
  112. }
  113. }
  114.  
  115.  
Are you still getting the NullPointer at the same line?
Dec 13 '06 #14
crazystone82
15 New Member
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
Dec 13 '06 #15
r035198x
13,262 MVP
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


Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5. import java.io.*;
  6. import java.net.*;
  7. public class remotefileupload implements ActionListener {
  8.  JTextField sourcefield,destinationfield;
  9.  JLabel sourceLabel,destinationLabel;
  10.  JButton submit;
  11.  FileInputStream fin;
  12.  FileOutputStream fout;
  13.  URL destinationurl;
  14.  URLConnection connection;
  15.  OutputStreamWriter out;
  16.  BufferedReader in;
  17.  remotefileupload() {
  18.  //WindowUtilities.setNativeLookAndFeel();
  19.    JFrame f = new JFrame("This is a test");
  20.    f.setSize(300,300);
  21.    Container content = f.getContentPane();
  22.    content.setBackground(Color.white);
  23.    content.setLayout(null);
  24.    sourceLabel=new JLabel("Source Path");
  25.    sourceLabel.setBounds(10,20,70,20);
  26.   sourcefield=new JTextField(30);
  27.   sourcefield.setBounds(90,20,90,20);
  28.   destinationLabel=new JLabel("Destination");
  29.    destinationLabel.setBounds(10,50,70,20);
  30.   destinationfield=new JTextField(30);
  31.   destinationfield.setBounds(90,50,90,20);
  32.    submit=new JButton("Submit");
  33.   submit.setBounds(40,80,90,30);
  34.   content.add(sourceLabel);
  35.    content.add(sourcefield);
  36.    content.add(destinationLabel);
  37.    content.add(destinationfield);
  38.   content.add(submit);
  39.   submit.addActionListener(this);
  40.   f.addWindowListener(new WindowAdapter() {
  41.    public void windowClosing(WindowEvent e) {
  42.    System.exit(0);
  43.    }});
  44.    f.setVisible(true);
  45.    //f.addWindowListener(new ExitListener());
  46.  }
  47.  public void actionPerformed(ActionEvent ae) {
  48.   int i;
  49.   String str=ae.getActionCommand();
  50.   if(str.equals("Submit")) {
  51.    try {
  52.     fin = new FileInputStream(sourcefield.getText());
  53.     in = new BufferedReader(new InputStreamReader(fin));
  54.     //fout =new FileOutputStream(destinationfield.getText());
  55.     String destination=destinationfield.getText();
  56.     destinationurl = new URL(destination);
  57.     connection = destinationurl.openConnection();
  58.     connection.setDoOutput(true);
  59.     //connection.connect();
  60.     out = new OutputStreamWriter(connection.getOutputStream());
  61.     do {
  62.      i = fin.read();
  63.      if(i != -1)
  64.       out.write(i);
  65.     }while(i != -1);
  66.     JOptionPane.showConfirmDialog(null,"success", "success", JOptionPane.YES_NO_OPTION);
  67.     fin.close();
  68.     out.close();
  69.     in.close();
  70.    }
  71.    catch(FileNotFoundException e) {
  72.     System.out.println("Input File not found");
  73.     return;
  74.    }
  75.    catch (MalformedURLException e) {
  76.     e.printStackTrace(); // new URL() failed
  77.    }
  78.    catch(ArrayIndexOutOfBoundsException e) {
  79.     System.out.println(" Copy file frm to");
  80.     return;
  81.    }
  82.    catch(IOException e) {
  83.     e.printStackTrace();
  84.    }
  85.   }
  86.  }
  87.  public static void main(String[] args)
  88.  {
  89.    new remotefileupload();
  90.  
  91.  }
  92. }
  93.  
Dec 13 '06 #16
crazystone82
15 New Member
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

Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5. import java.io.*;
  6. import java.net.*;
  7. public class remotefileupload implements ActionListener {
  8.  JTextField sourcefield,destinationfield;
  9.  JLabel sourceLabel,destinationLabel;
  10.  JButton submit;
  11.  FileInputStream fin;
  12.  FileOutputStream fout;
  13.  URL destinationurl;
  14.  URLConnection connection;
  15.  OutputStreamWriter out;
  16.  BufferedReader in;
  17.  remotefileupload() {
  18.  //WindowUtilities.setNativeLookAndFeel();
  19.    JFrame f = new JFrame("This is a test");
  20.    f.setSize(300,300);
  21.    Container content = f.getContentPane();
  22.    content.setBackground(Color.white);
  23.    content.setLayout(null);
  24.    sourceLabel=new JLabel("Source Path");
  25.    sourceLabel.setBounds(10,20,70,20);
  26.   sourcefield=new JTextField(30);
  27.   sourcefield.setBounds(90,20,90,20);
  28.   destinationLabel=new JLabel("Destination");
  29.    destinationLabel.setBounds(10,50,70,20);
  30.   destinationfield=new JTextField(30);
  31.   destinationfield.setBounds(90,50,90,20);
  32.    submit=new JButton("Submit");
  33.   submit.setBounds(40,80,90,30);
  34.   content.add(sourceLabel);
  35.    content.add(sourcefield);
  36.    content.add(destinationLabel);
  37.    content.add(destinationfield);
  38.   content.add(submit);
  39.   submit.addActionListener(this);
  40.   f.addWindowListener(new WindowAdapter() {
  41.    public void windowClosing(WindowEvent e) {
  42.    System.exit(0);
  43.    }});
  44.    f.setVisible(true);
  45.    //f.addWindowListener(new ExitListener());
  46.  }
  47.  public void actionPerformed(ActionEvent ae) {
  48.   int i;
  49.   String str=ae.getActionCommand();
  50.   if(str.equals("Submit")) {
  51.    try {
  52.     fin = new FileInputStream(sourcefield.getText());
  53.     in = new BufferedReader(new InputStreamReader(fin));
  54.     //fout =new FileOutputStream(destinationfield.getText());
  55.     String destination=destinationfield.getText();
  56.     destinationurl = new URL(destination);
  57.     connection = destinationurl.openConnection();
  58.     connection.setDoOutput(true);
  59.     //connection.connect();
  60.     out = new OutputStreamWriter(connection.getOutputStream());
  61.     do {
  62.      i = fin.read();
  63.      if(i != -1)
  64.       out.write(i);
  65.     }while(i != -1);
  66.     JOptionPane.showConfirmDialog(null,"success", "success", JOptionPane.YES_NO_OPTION);
  67.     fin.close();
  68.     out.close();
  69.     in.close();
  70.    }
  71.    catch(FileNotFoundException e) {
  72.     System.out.println("Input File not found");
  73.     return;
  74.    }
  75.    catch (MalformedURLException e) {
  76.     e.printStackTrace(); // new URL() failed
  77.    }
  78.    catch(ArrayIndexOutOfBoundsException e) {
  79.     System.out.println(" Copy file frm to");
  80.     return;
  81.    }
  82.    catch(IOException e) {
  83.     e.printStackTrace();
  84.    }
  85.   }
  86.  }
  87.  public static void main(String[] args)
  88.  {
  89.    new remotefileupload();
  90.  
  91.  }
  92. }
  93.  
Dec 13 '06 #17
r035198x
13,262 MVP
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

Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5. import java.io.*;
  6. import java.net.*;
  7. public class remotefileupload implements ActionListener {
  8. JTextField sourcefield,destinationfield;
  9. JLabel sourceLabel,destinationLabel;
  10. JButton submit;
  11. FileInputStream fin;
  12. FileOutputStream fout;
  13. URL destinationurl;
  14. URLConnection connection;
  15. OutputStreamWriter out;
  16. BufferedReader in;
  17. remotefileupload() {
  18. //WindowUtilities.setNativeLookAndFeel();
  19. JFrame f = new JFrame("This is a test");
  20. f.setSize(300,300);
  21. Container content = f.getContentPane();
  22. content.setBackground(Color.white);
  23. content.setLayout(null);
  24. sourceLabel=new JLabel("Source Path");
  25. sourceLabel.setBounds(10,20,70,20);
  26. sourcefield=new JTextField(30);
  27. sourcefield.setBounds(90,20,90,20);
  28. destinationLabel=new JLabel("Destination");
  29. destinationLabel.setBounds(10,50,70,20);
  30. destinationfield=new JTextField(30);
  31. destinationfield.setBounds(90,50,90,20);
  32. submit=new JButton("Submit");
  33. submit.setBounds(40,80,90,30);
  34. content.add(sourceLabel);
  35. content.add(sourcefield);
  36. content.add(destinationLabel);
  37. content.add(destinationfield);
  38. content.add(submit);
  39. submit.addActionListener(this);
  40. f.addWindowListener(new WindowAdapter() {
  41. public void windowClosing(WindowEvent e) {
  42. System.exit(0);
  43. }});
  44. f.setVisible(true);
  45. //f.addWindowListener(new ExitListener());
  46. }
  47. public void actionPerformed(ActionEvent ae) {
  48. int i;
  49. String str=ae.getActionCommand();
  50. if(str.equals("Submit")) {
  51. try {
  52.     fin = new FileInputStream(sourcefield.getText());
  53.     in = new BufferedReader(new InputStreamReader(fin));
  54.     //fout =new FileOutputStream(destinationfield.getText());
  55.     String destination=destinationfield.getText();
  56.     destinationurl = new URL(destination);
  57.     connection = destinationurl.openConnection();
  58.     connection.setDoOutput(true);
  59.     //connection.connect();
  60.     out = new OutputStreamWriter(connection.getOutputStream());
  61.     do {
  62.      i = fin.read();
  63.      if(i != -1)
  64.      out.write(i);
  65.     }while(i != -1);
  66.     JOptionPane.showConfirmDialog(null,"success", "success", JOptionPane.YES_NO_OPTION);
  67.     fin.close();
  68.     out.close();
  69.     in.close();
  70. }
  71. catch(FileNotFoundException e) {
  72.     System.out.println("Input File not found");
  73.     return;
  74. }
  75. catch (MalformedURLException e) {
  76.     e.printStackTrace(); // new URL() failed
  77. }
  78. catch(ArrayIndexOutOfBoundsException e) {
  79.     System.out.println(" Copy file frm to");
  80.     return;
  81. }
  82. catch(IOException e) {
  83.     e.printStackTrace();
  84. }
  85. }
  86. }
  87. public static void main(String[] args)
  88. {
  89. new remotefileupload();
  90.  
  91. }
  92. }
  93.  
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




Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5. import java.io.*;
  6. import java.net.*;
  7. public class remotefileupload implements ActionListener {
  8.  JTextField sourcefield,destinationfield;
  9.  JLabel sourceLabel,destinationLabel;
  10.  JButton submit;
  11.  FileInputStream fin;
  12.  FileOutputStream fout;
  13.  URL destinationurl;
  14.  URLConnection connection;
  15.  OutputStreamWriter out;
  16.  BufferedReader in;
  17.  remotefileupload() {
  18.  //WindowUtilities.setNativeLookAndFeel();
  19.    JFrame f = new JFrame("This is a test");
  20.    f.setSize(300,300);
  21.    Container content = f.getContentPane();
  22.    content.setBackground(Color.white);
  23.    content.setLayout(null);
  24.    sourceLabel=new JLabel("Source Path");
  25.    sourceLabel.setBounds(10,20,70,20);
  26.   sourcefield=new JTextField(30);
  27.   sourcefield.setBounds(90,20,90,20);
  28.   destinationLabel=new JLabel("Destination");
  29.    destinationLabel.setBounds(10,50,70,20);
  30.   destinationfield=new JTextField(30);
  31.   destinationfield.setBounds(90,50,90,20);
  32.    submit=new JButton("Submit");
  33.   submit.setBounds(40,80,90,30);
  34.   content.add(sourceLabel);
  35.    content.add(sourcefield);
  36.    content.add(destinationLabel);
  37.    content.add(destinationfield);
  38.   content.add(submit);
  39.   submit.addActionListener(this);
  40.   f.addWindowListener(new WindowAdapter() {
  41.    public void windowClosing(WindowEvent e) {
  42.    System.exit(0);
  43.    }});
  44.    f.setVisible(true);
  45.    //f.addWindowListener(new ExitListener());
  46.  }
  47.  public void actionPerformed(ActionEvent ae) {
  48.   int i;
  49.   String str=ae.getActionCommand();
  50.   if(str.equals("Submit")) {
  51.    try {
  52.     fin = new FileInputStream(sourcefield.getText());
  53.     in = new BufferedReader(new InputStreamReader(fin));
  54.     //fout =new FileOutputStream(destinationfield.getText());
  55.     String destination=destinationfield.getText();
  56.     destinationurl = new URL(destination);
  57.     connection = destinationurl.openConnection();
  58.     connection.setDoOutput(true);
  59.     //connection.connect();
  60.     out = new OutputStreamWriter(connection.getOutputStream());
  61.     String line;
  62.     while ((line = in.readLine()) != null) {
  63.      System.out.println(line);//Just read the lines and print them.
  64.     }
  65.     //do {
  66.     // i = fin.read();
  67.     // if(i != -1)
  68.     //  out.write(i);
  69.     //}while(i != -1);
  70.     JOptionPane.showConfirmDialog(null,"success", "success", JOptionPane.YES_NO_OPTION);
  71.     fin.close();
  72.     out.close();
  73.     in.close();
  74.    }
  75.    catch(FileNotFoundException e) {
  76.     System.out.println("Input File not found");
  77.     return;
  78.    }
  79.    catch (MalformedURLException e) {
  80.     e.printStackTrace(); // new URL() failed
  81.    }
  82.    catch(ArrayIndexOutOfBoundsException e) {
  83.     System.out.println(" Copy file frm to");
  84.     return;
  85.    }
  86.    catch(IOException e) {
  87.     e.printStackTrace();
  88.    }
  89.   }
  90.  }
  91.  public static void main(String[] args)
  92.  {
  93.    new remotefileupload();
  94.  
  95.  }
  96. }
  97.  
Dec 13 '06 #18
crazystone82
15 New Member
i tested that program it reads the content of file and displays it on the system console of the source machine



Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5. import java.io.*;
  6. import java.net.*;
  7. public class remotefileupload implements ActionListener {
  8.  JTextField sourcefield,destinationfield;
  9.  JLabel sourceLabel,destinationLabel;
  10.  JButton submit;
  11.  FileInputStream fin;
  12.  FileOutputStream fout;
  13.  URL destinationurl;
  14.  URLConnection connection;
  15.  OutputStreamWriter out;
  16.  BufferedReader in;
  17.  remotefileupload() {
  18.  //WindowUtilities.setNativeLookAndFeel();
  19.    JFrame f = new JFrame("This is a test");
  20.    f.setSize(300,300);
  21.    Container content = f.getContentPane();
  22.    content.setBackground(Color.white);
  23.    content.setLayout(null);
  24.    sourceLabel=new JLabel("Source Path");
  25.    sourceLabel.setBounds(10,20,70,20);
  26.   sourcefield=new JTextField(30);
  27.   sourcefield.setBounds(90,20,90,20);
  28.   destinationLabel=new JLabel("Destination");
  29.    destinationLabel.setBounds(10,50,70,20);
  30.   destinationfield=new JTextField(30);
  31.   destinationfield.setBounds(90,50,90,20);
  32.    submit=new JButton("Submit");
  33.   submit.setBounds(40,80,90,30);
  34.   content.add(sourceLabel);
  35.    content.add(sourcefield);
  36.    content.add(destinationLabel);
  37.    content.add(destinationfield);
  38.   content.add(submit);
  39.   submit.addActionListener(this);
  40.   f.addWindowListener(new WindowAdapter() {
  41.    public void windowClosing(WindowEvent e) {
  42.    System.exit(0);
  43.    }});
  44.    f.setVisible(true);
  45.    //f.addWindowListener(new ExitListener());
  46.  }
  47.  public void actionPerformed(ActionEvent ae) {
  48.   int i;
  49.   String str=ae.getActionCommand();
  50.   if(str.equals("Submit")) {
  51.    try {
  52.     fin = new FileInputStream(sourcefield.getText());
  53.     in = new BufferedReader(new InputStreamReader(fin));
  54.     //fout =new FileOutputStream(destinationfield.getText());
  55.     String destination=destinationfield.getText();
  56.     destinationurl = new URL(destination);
  57.     connection = destinationurl.openConnection();
  58.     connection.setDoOutput(true);
  59.     //connection.connect();
  60.     out = new OutputStreamWriter(connection.getOutputStream());
  61.     String line;
  62.     while ((line = in.readLine()) != null) {
  63.      System.out.println(line);//Just read the lines and print them.
  64.     }
  65.     //do {
  66.     // i = fin.read();
  67.     // if(i != -1)
  68.     //  out.write(i);
  69.     //}while(i != -1);
  70.     JOptionPane.showConfirmDialog(null,"success", "success", JOptionPane.YES_NO_OPTION);
  71.     fin.close();
  72.     out.close();
  73.     in.close();
  74.    }
  75.    catch(FileNotFoundException e) {
  76.     System.out.println("Input File not found");
  77.     return;
  78.    }
  79.    catch (MalformedURLException e) {
  80.     e.printStackTrace(); // new URL() failed
  81.    }
  82.    catch(ArrayIndexOutOfBoundsException e) {
  83.     System.out.println(" Copy file frm to");
  84.     return;
  85.    }
  86.    catch(IOException e) {
  87.     e.printStackTrace();
  88.    }
  89.   }
  90.  }
  91.  public static void main(String[] args)
  92.  {
  93.    new remotefileupload();
  94.  
  95.  }
  96. }
  97.  
Dec 13 '06 #19
r035198x
13,262 MVP
i tested that program it reads the content of file and displays it on the system console of the source machine



Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5. import java.io.*;
  6. import java.net.*;
  7. public class remotefileupload implements ActionListener {
  8. JTextField sourcefield,destinationfield;
  9. JLabel sourceLabel,destinationLabel;
  10. JButton submit;
  11. FileInputStream fin;
  12. FileOutputStream fout;
  13. URL destinationurl;
  14. URLConnection connection;
  15. OutputStreamWriter out;
  16. BufferedReader in;
  17. remotefileupload() {
  18. //WindowUtilities.setNativeLookAndFeel();
  19. JFrame f = new JFrame("This is a test");
  20. f.setSize(300,300);
  21. Container content = f.getContentPane();
  22. content.setBackground(Color.white);
  23. content.setLayout(null);
  24. sourceLabel=new JLabel("Source Path");
  25. sourceLabel.setBounds(10,20,70,20);
  26. sourcefield=new JTextField(30);
  27. sourcefield.setBounds(90,20,90,20);
  28. destinationLabel=new JLabel("Destination");
  29. destinationLabel.setBounds(10,50,70,20);
  30. destinationfield=new JTextField(30);
  31. destinationfield.setBounds(90,50,90,20);
  32. submit=new JButton("Submit");
  33. submit.setBounds(40,80,90,30);
  34. content.add(sourceLabel);
  35. content.add(sourcefield);
  36. content.add(destinationLabel);
  37. content.add(destinationfield);
  38. content.add(submit);
  39. submit.addActionListener(this);
  40. f.addWindowListener(new WindowAdapter() {
  41. public void windowClosing(WindowEvent e) {
  42. System.exit(0);
  43. }});
  44. f.setVisible(true);
  45. //f.addWindowListener(new ExitListener());
  46. }
  47. public void actionPerformed(ActionEvent ae) {
  48. int i;
  49. String str=ae.getActionCommand();
  50. if(str.equals("Submit")) {
  51. try {
  52.     fin = new FileInputStream(sourcefield.getText());
  53.     in = new BufferedReader(new InputStreamReader(fin));
  54.     //fout =new FileOutputStream(destinationfield.getText());
  55.     String destination=destinationfield.getText();
  56.     destinationurl = new URL(destination);
  57.     connection = destinationurl.openConnection();
  58.     connection.setDoOutput(true);
  59.     //connection.connect();
  60.     out = new OutputStreamWriter(connection.getOutputStream());
  61.     String line;
  62.     while ((line = in.readLine()) != null) {
  63.      System.out.println(line);//Just read the lines and print them.
  64.     }
  65.     //do {
  66.     // i = fin.read();
  67.     // if(i != -1)
  68.     // out.write(i);
  69.     //}while(i != -1);
  70.     JOptionPane.showConfirmDialog(null,"success", "success", JOptionPane.YES_NO_OPTION);
  71.     fin.close();
  72.     out.close();
  73.     in.close();
  74. }
  75. catch(FileNotFoundException e) {
  76.     System.out.println("Input File not found");
  77.     return;
  78. }
  79. catch (MalformedURLException e) {
  80.     e.printStackTrace(); // new URL() failed
  81. }
  82. catch(ArrayIndexOutOfBoundsException e) {
  83.     System.out.println(" Copy file frm to");
  84.     return;
  85. }
  86. catch(IOException e) {
  87.     e.printStackTrace();
  88. }
  89. }
  90. }
  91. public static void main(String[] args)
  92. {
  93. new remotefileupload();
  94.  
  95. }
  96. }
  97.  
Now you want to test if you can create the file on the remote machine. Try this

Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5. import java.io.*;
  6. import java.net.*;
  7. public class remotefileupload implements ActionListener {
  8.  JTextField sourcefield,destinationfield;
  9.  JLabel sourceLabel,destinationLabel;
  10.  JButton submit;
  11.  FileInputStream fin;
  12.  FileOutputStream fout;
  13.  URL destinationurl;
  14.  URLConnection connection;
  15.  OutputStreamWriter out;
  16.  BufferedReader in;
  17.  remotefileupload() {
  18.  //WindowUtilities.setNativeLookAndFeel();
  19.    JFrame f = new JFrame("This is a test");
  20.    f.setSize(300,300);
  21.    Container content = f.getContentPane();
  22.    content.setBackground(Color.white);
  23.    content.setLayout(null);
  24.    sourceLabel=new JLabel("Source Path");
  25.    sourceLabel.setBounds(10,20,70,20);
  26.   sourcefield=new JTextField(30);
  27.   sourcefield.setBounds(90,20,90,20);
  28.   destinationLabel=new JLabel("Destination");
  29.    destinationLabel.setBounds(10,50,70,20);
  30.   destinationfield=new JTextField(30);
  31.   destinationfield.setBounds(90,50,90,20);
  32.    submit=new JButton("Submit");
  33.   submit.setBounds(40,80,90,30);
  34.   content.add(sourceLabel);
  35.    content.add(sourcefield);
  36.    content.add(destinationLabel);
  37.    content.add(destinationfield);
  38.   content.add(submit);
  39.   submit.addActionListener(this);
  40.   f.addWindowListener(new WindowAdapter() {
  41.    public void windowClosing(WindowEvent e) {
  42.    System.exit(0);
  43.    }});
  44.    f.setVisible(true);
  45.    //f.addWindowListener(new ExitListener());
  46.  }
  47.  public void actionPerformed(ActionEvent ae) {
  48.   int i;
  49.   String str=ae.getActionCommand();
  50.   if(str.equals("Submit")) {
  51.    try {
  52.     fin = new FileInputStream(sourcefield.getText());
  53.     in = new BufferedReader(new InputStreamReader(fin));
  54.     ;
  55.     String destination=destinationfield.getText();
  56.     destinationurl = new URL(destination);
  57.     connection = destinationurl.openConnection();
  58.     connection.setDoOutput(true);
  59.     //connection.connect();
  60.     out = new OutputStreamWriter(connection.getOutputStream());
  61.     fout = new FileOutputStream(destinationfield.getText());
  62.     String line;
  63.     while ((line = in.readLine()) != null) {
  64.      System.out.println(line);//Just read the lines and print them.
  65.     }
  66.     //do {
  67.     // i = fin.read();
  68.     // if(i != -1)
  69.     //  out.write(i);
  70.     //}while(i != -1);
  71.     JOptionPane.showConfirmDialog(null,"success", "success", JOptionPane.YES_NO_OPTION);
  72.     fin.close();
  73.     out.close();
  74.     in.close();
  75.    }
  76.    catch(FileNotFoundException e) {
  77.     System.out.println("Input File not found");
  78.     return;
  79.    }
  80.    catch (MalformedURLException e) {
  81.     e.printStackTrace(); // new URL() failed
  82.    }
  83.    catch(ArrayIndexOutOfBoundsException e) {
  84.     System.out.println(" Copy file frm to");
  85.     return;
  86.    }
  87.    catch(IOException e) {
  88.     e.printStackTrace();
  89.    }
  90.   }
  91.  }
  92.  public static void main(String[] args)
  93.  {
  94.    new remotefileupload();
  95.  
  96.  }
  97. }
  98.  

and see if the blank file is getting created
Dec 13 '06 #20

Sign in to post your reply or Sign up for a free account.

Similar topics

5
4443
by: Zach | last post by:
This is all on linux using jdk1.3. My application is written in AWT, no swing. The application requires running it on the host machine and tunneling the GUI back to a client. I've been doing this via an ssh session. In the building the GUI works pretty much as if I was running it directly from the host machine, no lags or performance problems. Over longer distances I start to see some lagging, the GUI loses some responsiveness, it's...
1
3029
by: PeterB | last post by:
Hi! I'm using Pure ASP File Upload (http://www.asp101.com/articles/jacob/scriptupload.asp) to upload a file from a client to a server. I am testing both on a local IIS and a remote server. The welcome page has a browse button (for locating your local file), a textfield where the path is displayed, and a upload button. When clicking upload a VB ASP script is run, and the outcome is displayed on a new page. 1. On my local machine I got the...
6
4018
by: Pat Carden | last post by:
Hi, We need to allow webusers to upload a file on our website (on Server3, all servers run Server 2003, remotely hosted) and eventually save it on our SBS Server (Server2) which is not exposed through our firewall. We have another server (Server1) within the SBS domain that is exposed through port 80 of the firewall on which we host some web services and images. What is the best architecture for getting the file from the remotely...
18
4363
by: Jen | last post by:
I'm using Microsoft's own VB.NET FTP Example: http://support.microsoft.com/default.aspx?scid=kb;en-us;832679 I can get the program to create directories, change directories, etc., but I can't get it to upload a file to the FTP server. I just get a "Cannot connect to remote server" error after this TRY: s = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
2
6971
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of attending interviews. If you own a company best way to judge if the candidate is worth of it. http://www.questpond.com/InterviewRatingSheet.zip
4
6920
by: Vlad | last post by:
I am having problems using the file.create method within a function that is called when looping through an array of filepaths. If I call my function with a hardcoded file path --C:\Temp.txt the function creates the file as expected. When I loop through my array I get the error - "ArgumentException was unhandled - Illegal characters in path" The value "C:\Temp.txt" is the first value in the array - as it works
1
1898
by: lPrentice | last post by:
Hello, After all this time, Linux file permissions still confuse me at times. I have a Python web-based application with an file (images) upload module. The application is running on two remote servers and a local server on my development network. The upload module works just fine when I'm uploading to my remote servers. But when I try to upload to my development server, I get a permission error. What I don't understand is who is...
1
8281
by: ndedhia1 | last post by:
I was hoping you could help me out with ftp vs sftp. Below is a method that I have that I call to ftp files from one unix box to another in house, but soon, we will have to ftp from here to NY so we have to start using sftp. I know that we have open ssh on our unix boxes but was wondering how different the syntax would be, going from ftp to sftp. Here is a method that I have written in my java code for ftping a file to a specific...
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10378
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10115
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7653
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5687
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3013
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.