By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,798 Members | 1,349 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,798 IT Pros & Developers. It's quick & easy.

Java file upload to remote pc

P: 15
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 #1
Share this Question
Share on Google+
24 Replies


10K+
P: 13,264
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.
Dec 11 '06 #2

P: 15
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

Expand|Select|Wrap|Line Numbers
  1.  import java.awt.*; 
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import java.io.*;
  5. import java.net.*;
  6.  
  7. public class remotefileupload implements ActionListener 
  8. {
  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.  
  19. remotefileupload() 
  20. {
  21.      //WindowUtilities.setNativeLookAndFeel();
  22.      JFrame f = new JFrame("This is a test");
  23.      f.setSize(300,300);
  24.  
  25.      Container content = f.getContentPane();
  26.      content.setBackground(Color.white);
  27.      content.setLayout(null);
  28.  
  29.      sourceLabel=new JLabel("Source Path"); 
  30.      sourceLabel.setBounds(10,20,70,20);
  31.  
  32. sourcefield=new JTextField(30);
  33. sourcefield.setBounds(90,20,90,20);
  34.  
  35. destinationLabel=new JLabel("Destination"); 
  36.      destinationLabel.setBounds(10,50,70,20);
  37.  
  38. destinationfield=new JTextField(30);
  39. destinationfield.setBounds(90,50,90,20);
  40.  
  41.      submit=new JButton("Submit");
  42. submit.setBounds(40,80,90,30);
  43.  
  44. content.add(sourceLabel);
  45.      content.add(sourcefield);
  46.      content.add(destinationLabel);
  47.      content.add(destinationfield);
  48. content.add(submit);
  49.  
  50. submit.addActionListener(this);
  51.  
  52. f.addWindowListener(new WindowAdapter() {
  53.  
  54.      public void windowClosing(WindowEvent e) {
  55.      System.exit(0);
  56.      }
  57.      });
  58.  
  59.      f.setVisible(true);
  60.      //f.addWindowListener(new ExitListener());
  61.  
  62. }
  63.  
  64. public void actionPerformed(ActionEvent ae) 
  65. {
  66. int i;
  67.  
  68. String str=ae.getActionCommand();
  69.  
  70. if(str.equals("Submit"))
  71. {
  72. try
  73. {
  74. try
  75. {
  76.  
  77. fin = new FileInputStream(sourcefield.getText());
  78.  
  79. }
  80. catch(FileNotFoundException e)
  81. {
  82.     System.out.println("Input File not found");
  83.     return;
  84. }
  85. try
  86. {
  87. //fout =new FileOutputStream(destinationfield.getText());
  88. String destination=destinationfield.getText();
  89. destinationurl = new URL("destination");
  90. connection = destinationurl.openConnection();
  91. connection.setDoOutput(true);
  92. //connection.connect();
  93.  
  94. }
  95. catch (MalformedURLException e) 
  96. {
  97. e.printStackTrace(); // new URL() failed
  98.  
  99. catch (IOException e) 
  100. {
  101. System.out.println("unable to open");// openConnection() failed
  102.  
  103. }
  104.  
  105. /*catch(FileNotFoundException e)
  106.  
  107. {
  108. System.out.println("Output File not found");
  109. return;
  110. }*/
  111. }
  112. catch(ArrayIndexOutOfBoundsException e)
  113.  
  114. {
  115. System.out.println(" Copy file frm to");
  116. return;
  117. }
  118.  
  119. try
  120.  
  121. {
  122.  
  123. out = new OutputStreamWriter(connection.getOutputStream());
  124. //in = new BufferedReader(new InputStreamReader(connection.getInputStream(fin)));
  125. do
  126. {
  127. i = fin.read();
  128. if(i != -1)
  129. out.write(i);
  130. }while(i != -1);
  131. JOptionPane.showConfirmDialog(null,"success", "success", JOptionPane.YES_NO_OPTION);
  132.  
  133.  
  134. fin.close();
  135. out.close(); 
  136. in.close();
  137. }
  138. catch(IOException e)
  139. {
  140. e.printStackTrace();
  141. }
  142.  
  143.  
  144. }
  145.  
  146.  
  147.  
  148.  
  149.  
  150. public static void main(String[] args) 
  151. {
  152.      new remotefileupload();
  153.  
  154.  
  155. }
  156. }
  157.  
Dec 11 '06 #3

10K+
P: 13,264
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

Expand|Select|Wrap|Line Numbers
  1.  import java.awt.*; 
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import java.io.*;
  5. import java.net.*;
  6.  
  7. public class remotefileupload implements ActionListener 
  8. {
  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.  
  19. remotefileupload() 
  20. {
  21.      //WindowUtilities.setNativeLookAndFeel();
  22.      JFrame f = new JFrame("This is a test");
  23.      f.setSize(300,300);
  24.  
  25.      Container content = f.getContentPane();
  26.      content.setBackground(Color.white);
  27.      content.setLayout(null);
  28.  
  29.      sourceLabel=new JLabel("Source Path"); 
  30.      sourceLabel.setBounds(10,20,70,20);
  31.  
  32. sourcefield=new JTextField(30);
  33. sourcefield.setBounds(90,20,90,20);
  34.  
  35. destinationLabel=new JLabel("Destination"); 
  36.      destinationLabel.setBounds(10,50,70,20);
  37.  
  38. destinationfield=new JTextField(30);
  39. destinationfield.setBounds(90,50,90,20);
  40.  
  41.      submit=new JButton("Submit");
  42. submit.setBounds(40,80,90,30);
  43.  
  44. content.add(sourceLabel);
  45.      content.add(sourcefield);
  46.      content.add(destinationLabel);
  47.      content.add(destinationfield);
  48. content.add(submit);
  49.  
  50. submit.addActionListener(this);
  51.  
  52. f.addWindowListener(new WindowAdapter() {
  53.  
  54.      public void windowClosing(WindowEvent e) {
  55.      System.exit(0);
  56.      }
  57.      });
  58.  
  59.      f.setVisible(true);
  60.      //f.addWindowListener(new ExitListener());
  61.  
  62. }
  63.  
  64. public void actionPerformed(ActionEvent ae) 
  65. {
  66. int i;
  67.  
  68. String str=ae.getActionCommand();
  69.  
  70. if(str.equals("Submit"))
  71. {
  72. try
  73. {
  74. try
  75. {
  76.  
  77. fin = new FileInputStream(sourcefield.getText());
  78.  
  79. }
  80. catch(FileNotFoundException e)
  81. {
  82.     System.out.println("Input File not found");
  83.     return;
  84. }
  85. try
  86. {
  87. //fout =new FileOutputStream(destinationfield.getText());
  88. String destination=destinationfield.getText();
  89. destinationurl = new URL("destination");
  90. connection = destinationurl.openConnection();
  91. connection.setDoOutput(true);
  92. //connection.connect();
  93.  
  94. }
  95. catch (MalformedURLException e) 
  96. {
  97. e.printStackTrace(); // new URL() failed
  98.  
  99. catch (IOException e) 
  100. {
  101. System.out.println("unable to open");// openConnection() failed
  102.  
  103. }
  104.  
  105. /*catch(FileNotFoundException e)
  106.  
  107. {
  108. System.out.println("Output File not found");
  109. return;
  110. }*/
  111. }
  112. catch(ArrayIndexOutOfBoundsException e)
  113.  
  114. {
  115. System.out.println(" Copy file frm to");
  116. return;
  117. }
  118.  
  119. try
  120.  
  121. {
  122.  
  123. out = new OutputStreamWriter(connection.getOutputStream());
  124. //in = new BufferedReader(new InputStreamReader(connection.getInputStream(fin)));
  125. do
  126. {
  127. i = fin.read();
  128. if(i != -1)
  129. out.write(i);
  130. }while(i != -1);
  131. JOptionPane.showConfirmDialog(null,"success", "success", JOptionPane.YES_NO_OPTION);
  132.  
  133.  
  134. fin.close();
  135. out.close(); 
  136. in.close();
  137. }
  138. catch(IOException e)
  139. {
  140. e.printStackTrace();
  141. }
  142.  
  143.  
  144. }
  145.  
  146.  
  147.  
  148.  
  149.  
  150. public static void main(String[] args) 
  151. {
  152.      new remotefileupload();
  153.  
  154.  
  155. }
  156. }
  157.  
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.
Dec 11 '06 #4

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

java.net.MalformedURLException: no protocol: destination
at java.net.URL.<init>(URL.java:537)
at java.net.URL.<init>(URL.java:434)
at java.net.URL.<init>(URL.java:383)
at remotefileupload.actionPerformed(remotefileupload. java:89)
at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:17
86)
at javax.swing.AbstractButton$ForwardActionEvents.act ionPerformed(Abstra
ctButton.java:1839)
at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel
.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:258
)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonL
istener.java:245)
at java.awt.Component.processMouseEvent(Component.jav a:5100)
at java.awt.Component.processEvent(Component.java:489 7)
at java.awt.Container.processEvent(Container.java:156 9)
at java.awt.Component.dispatchEventImpl(Component.jav a:3615)
at java.awt.Container.dispatchEventImpl(Container.jav a:1627)
at java.awt.Component.dispatchEvent(Component.java:34 77)
at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:3483
)
at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:3198)

at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:3128)
at java.awt.Container.dispatchEventImpl(Container.jav a:1613)
at java.awt.Window.dispatchEventImpl(Window.java:1606 )
at java.awt.Component.dispatchEvent(Component.java:34 77)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 480)
at java.awt.EventDispatchThread.pumpOneEventForHierar chy(EventDispatchTh
read.java:201)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThre
ad.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:145)

at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:137)

at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:100)
java.lang.NullPointerException
at remotefileupload.actionPerformed(remotefileupload. java:124)
at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:17
86)
at javax.swing.AbstractButton$ForwardActionEvents.act ionPerformed(Abstra
ctButton.java:1839)
at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel
.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:258
)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonL
istener.java:245)
at java.awt.Component.processMouseEvent(Component.jav a:5100)
at java.awt.Component.processEvent(Component.java:489 7)
at java.awt.Container.processEvent(Container.java:156 9)
at java.awt.Component.dispatchEventImpl(Component.jav a:3615)
at java.awt.Container.dispatchEventImpl(Container.jav a:1627)
at java.awt.Component.dispatchEvent(Component.java:34 77)
at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:3483
)
at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:3198)

at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:3128)
at java.awt.Container.dispatchEventImpl(Container.jav a:1613)
at java.awt.Window.dispatchEventImpl(Window.java:1606 )
at java.awt.Component.dispatchEvent(Component.java:34 77)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 480)
at java.awt.EventDispatchThread.pumpOneEventForHierar chy(EventDispatchTh
read.java:201)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThre
ad.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:145)

at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:137)

at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:100)



please give me the solution
Dec 12 '06 #5

10K+
P: 13,264
if we give the destination as http://ws111/share/copy.txt at run time
it shows the following error

java.net.MalformedURLException: no protocol: destination
at java.net.URL.<init>(URL.java:537)
at java.net.URL.<init>(URL.java:434)
at java.net.URL.<init>(URL.java:383)
at remotefileupload.actionPerformed(remotefileupload. java:89)
at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:17
86)
at javax.swing.AbstractButton$ForwardActionEvents.act ionPerformed(Abstra
ctButton.java:1839)
at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel
.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:258
)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonL
istener.java:245)
at java.awt.Component.processMouseEvent(Component.jav a:5100)
at java.awt.Component.processEvent(Component.java:489 7)
at java.awt.Container.processEvent(Container.java:156 9)
at java.awt.Component.dispatchEventImpl(Component.jav a:3615)
at java.awt.Container.dispatchEventImpl(Container.jav a:1627)
at java.awt.Component.dispatchEvent(Component.java:34 77)
at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:3483
)
at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:3198)

at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:3128)
at java.awt.Container.dispatchEventImpl(Container.jav a:1613)
at java.awt.Window.dispatchEventImpl(Window.java:1606 )
at java.awt.Component.dispatchEvent(Component.java:34 77)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 480)
at java.awt.EventDispatchThread.pumpOneEventForHierar chy(EventDispatchTh
read.java:201)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThre
ad.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:145)

at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:137)

at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:100)
java.lang.NullPointerException
at remotefileupload.actionPerformed(remotefileupload. java:124)
at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:17
86)
at javax.swing.AbstractButton$ForwardActionEvents.act ionPerformed(Abstra
ctButton.java:1839)
at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel
.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:258
)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonL
istener.java:245)
at java.awt.Component.processMouseEvent(Component.jav a:5100)
at java.awt.Component.processEvent(Component.java:489 7)
at java.awt.Container.processEvent(Container.java:156 9)
at java.awt.Component.dispatchEventImpl(Component.jav a:3615)
at java.awt.Container.dispatchEventImpl(Container.jav a:1627)
at java.awt.Component.dispatchEvent(Component.java:34 77)
at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:3483
)
at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:3198)

at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:3128)
at java.awt.Container.dispatchEventImpl(Container.jav a:1613)
at java.awt.Window.dispatchEventImpl(Window.java:1606 )
at java.awt.Component.dispatchEvent(Component.java:34 77)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 480)
at java.awt.EventDispatchThread.pumpOneEventForHierar chy(EventDispatchTh
read.java:201)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThre
ad.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:145)

at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:137)

at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:100)



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

http://www.cafeaulait.org/course/week12/10.html
Dec 12 '06 #6

Expert 100+
P: 1,510
if we give the destination as http://ws111/share/copy.txt at run time
it shows the following error

java.net.MalformedURLException: no protocol: destination


please give me the solution
you get this error because you have " around destination in
Expand|Select|Wrap|Line Numbers
  1. destinationurl = new URL("destination");
  2.  
it should be?
Expand|Select|Wrap|Line Numbers
  1. destinationurl = new URL(destination);
  2.  
Dec 12 '06 #7

P: 15
dear friends,

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

java.lang.NullPointerException
at remotefileupload.actionPerformed(remotefileupload. java:137)
at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:17
86)
at javax.swing.AbstractButton$ForwardActionEvents.act ionPerformed(Abstra
ctButton.java:1839)
at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel
.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:258
)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonL
istener.java:245)
at java.awt.Component.processMouseEvent(Component.jav a:5100)
at java.awt.Component.processEvent(Component.java:489 7)
at java.awt.Container.processEvent(Container.java:156 9)
at java.awt.Component.dispatchEventImpl(Component.jav a:3615)
at java.awt.Container.dispatchEventImpl(Container.jav a:1627)
at java.awt.Component.dispatchEvent(Component.java:34 77)
at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:3483
)
at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:3198)

at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:3128)
at java.awt.Container.dispatchEventImpl(Container.jav a:1613)
at java.awt.Window.dispatchEventImpl(Window.java:1606 )
at java.awt.Component.dispatchEvent(Component.java:34 77)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 480)
at java.awt.EventDispatchThread.pumpOneEventForHierar chy(EventDispatchTh
read.java:201)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThre
ad.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:145)

at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:137)

at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:100)

please correct that code in order to run correct .its urgent
Dec 12 '06 #8

P: 15
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 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();


}
}
please modify the code to upload the file correctly to remote url.please its very urgent
Dec 12 '06 #9

10K+
P: 13,264
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 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();


}
}
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.
Dec 12 '06 #10

P: 15
sorry for double post

the line 137 is


in.close();
Dec 12 '06 #11

10K+
P: 13,264
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

P: 15
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

10K+
P: 13,264
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

P: 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
Dec 13 '06 #15

10K+
P: 13,264
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

P: 15
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

10K+
P: 13,264
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

P: 15
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

10K+
P: 13,264
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

P: 15
if i run the program i got the Exception "input file not found"

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.  
Dec 13 '06 #21

10K+
P: 13,264
if i run the program i got the Exception "input file not found"

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.  
What had you specified as the destination?
Run this and check the stack trace to see which file was not found.

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.     e.printStackTrace();
  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.  

I'm begginning to think we may need a Socket for this
Dec 13 '06 #22

P: 15
i got the following error:


Input File not found
java.io.FileNotFoundException: http:\ws111\Share\source.txt (The filename, direc
tory name, or volume label syntax is incorrect)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.j ava:179)
at java.io.FileOutputStream.<init>(FileOutputStream.j ava:70)
at remotefileupload.actionPerformed(remotefileupload. java:60)
at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:17
86)
at javax.swing.AbstractButton$ForwardActionEvents.act ionPerformed(Abstra
ctButton.java:1839)
at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel
.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:258
)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonL
istener.java:245)
at java.awt.Component.processMouseEvent(Component.jav a:5100)
at java.awt.Component.processEvent(Component.java:489 7)
at java.awt.Container.processEvent(Container.java:156 9)
at java.awt.Component.dispatchEventImpl(Component.jav a:3615)
at java.awt.Container.dispatchEventImpl(Container.jav a:1627)
at java.awt.Component.dispatchEvent(Component.java:34 77)
at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:3483
)
at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:3198)

at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:3128)
at java.awt.Container.dispatchEventImpl(Container.jav a:1613)
at java.awt.Window.dispatchEventImpl(Window.java:1606 )
at java.awt.Component.dispatchEvent(Component.java:34 77)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 480)
at java.awt.EventDispatchThread.pumpOneEventForHierar chy(EventDispatchTh
read.java:201)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThre
ad.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:145)

at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:137)

at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:100)



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

10K+
P: 13,264
i got the following error:


Input File not found
java.io.FileNotFoundException: http:\ws111\Share\source.txt (The filename, direc
tory name, or volume label syntax is incorrect)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.j ava:179)
at java.io.FileOutputStream.<init>(FileOutputStream.j ava:70)
at remotefileupload.actionPerformed(remotefileupload. java:60)
at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:17
86)
at javax.swing.AbstractButton$ForwardActionEvents.act ionPerformed(Abstra
ctButton.java:1839)
at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel
.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:258
)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonL
istener.java:245)
at java.awt.Component.processMouseEvent(Component.jav a:5100)
at java.awt.Component.processEvent(Component.java:489 7)
at java.awt.Container.processEvent(Container.java:156 9)
at java.awt.Component.dispatchEventImpl(Component.jav a:3615)
at java.awt.Container.dispatchEventImpl(Container.jav a:1627)
at java.awt.Component.dispatchEvent(Component.java:34 77)
at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:3483
)
at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:3198)

at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:3128)
at java.awt.Container.dispatchEventImpl(Container.jav a:1613)
at java.awt.Window.dispatchEventImpl(Window.java:1606 )
at java.awt.Component.dispatchEvent(Component.java:34 77)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 480)
at java.awt.EventDispatchThread.pumpOneEventForHierar chy(EventDispatchTh
read.java:201)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThre
ad.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:145)

at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:137)

at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:100)



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

Try it and tell me how it goes
Dec 13 '06 #24

P: 15
in that also it need one more program that should run on the server and program run on the client.but we not need such servlet implementation we need simple java program
Dec 13 '06 #25

Post your reply

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