473,799 Members | 2,837 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 #1
24 7409
r035198x
13,262 MVP
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
crazystone82
15 New Member
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
r035198x
13,262 MVP
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
crazystone82
15 New Member
if we give the destination as http://ws111/share/copy.txt at run time
it shows the following error

java.net.Malfor medURLException : no protocol: destination
at java.net.URL.<i nit>(URL.java:5 37)
at java.net.URL.<i nit>(URL.java:4 34)
at java.net.URL.<i nit>(URL.java:3 83)
at remotefileuploa d.actionPerform ed(remotefileup load.java:89)
at javax.swing.Abs tractButton.fir eActionPerforme d(AbstractButto n.java:17
86)
at javax.swing.Abs tractButton$For wardActionEvent s.actionPerform ed(Abstra
ctButton.java:1 839)
at javax.swing.Def aultButtonModel .fireActionPerf ormed(DefaultBu ttonModel
.java:420)
at javax.swing.Def aultButtonModel .setPressed(Def aultButtonModel .java:258
)
at javax.swing.pla f.basic.BasicBu ttonListener.mo useReleased(Bas icButtonL
istener.java:24 5)
at java.awt.Compon ent.processMous eEvent(Componen t.java:5100)
at java.awt.Compon ent.processEven t(Component.jav a:4897)
at java.awt.Contai ner.processEven t(Container.jav a:1569)
at java.awt.Compon ent.dispatchEve ntImpl(Componen t.java:3615)
at java.awt.Contai ner.dispatchEve ntImpl(Containe r.java:1627)
at java.awt.Compon ent.dispatchEve nt(Component.ja va:3477)
at java.awt.Lightw eightDispatcher .retargetMouseE vent(Container. java:3483
)
at java.awt.Lightw eightDispatcher .processMouseEv ent(Container.j ava:3198)

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

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

at java.awt.EventD ispatchThread.r un(EventDispatc hThread.java:10 0)
java.lang.NullP ointerException
at remotefileuploa d.actionPerform ed(remotefileup load.java:124)
at javax.swing.Abs tractButton.fir eActionPerforme d(AbstractButto n.java:17
86)
at javax.swing.Abs tractButton$For wardActionEvent s.actionPerform ed(Abstra
ctButton.java:1 839)
at javax.swing.Def aultButtonModel .fireActionPerf ormed(DefaultBu ttonModel
.java:420)
at javax.swing.Def aultButtonModel .setPressed(Def aultButtonModel .java:258
)
at javax.swing.pla f.basic.BasicBu ttonListener.mo useReleased(Bas icButtonL
istener.java:24 5)
at java.awt.Compon ent.processMous eEvent(Componen t.java:5100)
at java.awt.Compon ent.processEven t(Component.jav a:4897)
at java.awt.Contai ner.processEven t(Container.jav a:1569)
at java.awt.Compon ent.dispatchEve ntImpl(Componen t.java:3615)
at java.awt.Contai ner.dispatchEve ntImpl(Containe r.java:1627)
at java.awt.Compon ent.dispatchEve nt(Component.ja va:3477)
at java.awt.Lightw eightDispatcher .retargetMouseE vent(Container. java:3483
)
at java.awt.Lightw eightDispatcher .processMouseEv ent(Container.j ava:3198)

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

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

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



please give me the solution
Dec 12 '06 #5
r035198x
13,262 MVP
if we give the destination as http://ws111/share/copy.txt at run time
it shows the following error

java.net.Malfor medURLException : no protocol: destination
at java.net.URL.<i nit>(URL.java:5 37)
at java.net.URL.<i nit>(URL.java:4 34)
at java.net.URL.<i nit>(URL.java:3 83)
at remotefileuploa d.actionPerform ed(remotefileup load.java:89)
at javax.swing.Abs tractButton.fir eActionPerforme d(AbstractButto n.java:17
86)
at javax.swing.Abs tractButton$For wardActionEvent s.actionPerform ed(Abstra
ctButton.java:1 839)
at javax.swing.Def aultButtonModel .fireActionPerf ormed(DefaultBu ttonModel
.java:420)
at javax.swing.Def aultButtonModel .setPressed(Def aultButtonModel .java:258
)
at javax.swing.pla f.basic.BasicBu ttonListener.mo useReleased(Bas icButtonL
istener.java:24 5)
at java.awt.Compon ent.processMous eEvent(Componen t.java:5100)
at java.awt.Compon ent.processEven t(Component.jav a:4897)
at java.awt.Contai ner.processEven t(Container.jav a:1569)
at java.awt.Compon ent.dispatchEve ntImpl(Componen t.java:3615)
at java.awt.Contai ner.dispatchEve ntImpl(Containe r.java:1627)
at java.awt.Compon ent.dispatchEve nt(Component.ja va:3477)
at java.awt.Lightw eightDispatcher .retargetMouseE vent(Container. java:3483
)
at java.awt.Lightw eightDispatcher .processMouseEv ent(Container.j ava:3198)

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

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

at java.awt.EventD ispatchThread.r un(EventDispatc hThread.java:10 0)
java.lang.NullP ointerException
at remotefileuploa d.actionPerform ed(remotefileup load.java:124)
at javax.swing.Abs tractButton.fir eActionPerforme d(AbstractButto n.java:17
86)
at javax.swing.Abs tractButton$For wardActionEvent s.actionPerform ed(Abstra
ctButton.java:1 839)
at javax.swing.Def aultButtonModel .fireActionPerf ormed(DefaultBu ttonModel
.java:420)
at javax.swing.Def aultButtonModel .setPressed(Def aultButtonModel .java:258
)
at javax.swing.pla f.basic.BasicBu ttonListener.mo useReleased(Bas icButtonL
istener.java:24 5)
at java.awt.Compon ent.processMous eEvent(Componen t.java:5100)
at java.awt.Compon ent.processEven t(Component.jav a:4897)
at java.awt.Contai ner.processEven t(Container.jav a:1569)
at java.awt.Compon ent.dispatchEve ntImpl(Componen t.java:3615)
at java.awt.Contai ner.dispatchEve ntImpl(Containe r.java:1627)
at java.awt.Compon ent.dispatchEve nt(Component.ja va:3477)
at java.awt.Lightw eightDispatcher .retargetMouseE vent(Container. java:3483
)
at java.awt.Lightw eightDispatcher .processMouseEv ent(Container.j ava:3198)

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

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

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



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

http://www.cafeaulait.org/course/week12/10.html
Dec 12 '06 #6
horace1
1,510 Recognized Expert Top Contributor
if we give the destination as http://ws111/share/copy.txt at run time
it shows the following error

java.net.Malfor medURLException : no protocol: destination


please give me the solution
you get this error because you have " around destination in
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
crazystone82
15 New Member
dear friends,

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

java.lang.NullP ointerException
at remotefileuploa d.actionPerform ed(remotefileup load.java:137)
at javax.swing.Abs tractButton.fir eActionPerforme d(AbstractButto n.java:17
86)
at javax.swing.Abs tractButton$For wardActionEvent s.actionPerform ed(Abstra
ctButton.java:1 839)
at javax.swing.Def aultButtonModel .fireActionPerf ormed(DefaultBu ttonModel
.java:420)
at javax.swing.Def aultButtonModel .setPressed(Def aultButtonModel .java:258
)
at javax.swing.pla f.basic.BasicBu ttonListener.mo useReleased(Bas icButtonL
istener.java:24 5)
at java.awt.Compon ent.processMous eEvent(Componen t.java:5100)
at java.awt.Compon ent.processEven t(Component.jav a:4897)
at java.awt.Contai ner.processEven t(Container.jav a:1569)
at java.awt.Compon ent.dispatchEve ntImpl(Componen t.java:3615)
at java.awt.Contai ner.dispatchEve ntImpl(Containe r.java:1627)
at java.awt.Compon ent.dispatchEve nt(Component.ja va:3477)
at java.awt.Lightw eightDispatcher .retargetMouseE vent(Container. java:3483
)
at java.awt.Lightw eightDispatcher .processMouseEv ent(Container.j ava:3198)

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

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

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

please correct that code in order to run correct .its urgent
Dec 12 '06 #8
crazystone82
15 New Member
hi friends ,

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

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

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

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

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

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

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

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

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

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

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

submit.addActio nListener(this) ;

f.addWindowList ener(new WindowAdapter() {

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

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

}

public void actionPerformed (ActionEvent ae)
{
int i;

String str=ae.getActio nCommand();

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

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

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

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

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

}

/*catch(FileNotF oundException e)

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

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

try

{

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


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


}



}


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


}
}
please modify the code to upload the file correctly to remote url.please its very urgent
Dec 12 '06 #9
r035198x
13,262 MVP
hi friends ,

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

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

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

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

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

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

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

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

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

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

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

submit.addActio nListener(this) ;

f.addWindowList ener(new WindowAdapter() {

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

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

}

public void actionPerformed (ActionEvent ae)
{
int i;

String str=ae.getActio nCommand();

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

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

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

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

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

}

/*catch(FileNotF oundException e)

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

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

try

{

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


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


}



}


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


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

Your last post said you had a nullpointer exception on line 137. Can you post which one is line 137 in your code.
Dec 12 '06 #10

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

Similar topics

5
4442
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
3028
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
4355
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
6917
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
1897
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
8278
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
9687
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...
0
9541
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10225
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
10027
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
7564
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
5463
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...
1
4139
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
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.