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

Home Posts Topics Members FAQ

Java file upload to remote pc

15 New Member
Hi all,

please send me a source code to transfer a file to a server
running on another pc in the LAN...using JAVA .By getting the source path and destination path through textfield implemented in java swings
Dec 11 '06
24 7411
crazystone82
15 New Member
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
r035198x
13,262 MVP
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
crazystone82
15 New Member
i got the following error:


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

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

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

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



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


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

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

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

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



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

Try it and tell me how it goes
Dec 13 '06 #24
crazystone82
15 New Member
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

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

Similar topics

5
4443
by: Zach | last post by:
This is all on linux using jdk1.3. My application is written in AWT, no swing. The application requires running it on the host machine and tunneling the GUI back to a client. I've been doing this via an ssh session. In the building the GUI works pretty much as if I was running it directly from the host machine, no lags or performance problems. Over longer distances I start to see some lagging, the GUI loses some responsiveness, it's...
1
3029
by: PeterB | last post by:
Hi! I'm using Pure ASP File Upload (http://www.asp101.com/articles/jacob/scriptupload.asp) to upload a file from a client to a server. I am testing both on a local IIS and a remote server. The welcome page has a browse button (for locating your local file), a textfield where the path is displayed, and a upload button. When clicking upload a VB ASP script is run, and the outcome is displayed on a new page. 1. On my local machine I got the...
6
4018
by: Pat Carden | last post by:
Hi, We need to allow webusers to upload a file on our website (on Server3, all servers run Server 2003, remotely hosted) and eventually save it on our SBS Server (Server2) which is not exposed through our firewall. We have another server (Server1) within the SBS domain that is exposed through port 80 of the firewall on which we host some web services and images. What is the best architecture for getting the file from the remotely...
18
4363
by: Jen | last post by:
I'm using Microsoft's own VB.NET FTP Example: http://support.microsoft.com/default.aspx?scid=kb;en-us;832679 I can get the program to create directories, change directories, etc., but I can't get it to upload a file to the FTP server. I just get a "Cannot connect to remote server" error after this TRY: s = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
2
6971
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of attending interviews. If you own a company best way to judge if the candidate is worth of it. http://www.questpond.com/InterviewRatingSheet.zip
4
6920
by: Vlad | last post by:
I am having problems using the file.create method within a function that is called when looping through an array of filepaths. If I call my function with a hardcoded file path --C:\Temp.txt the function creates the file as expected. When I loop through my array I get the error - "ArgumentException was unhandled - Illegal characters in path" The value "C:\Temp.txt" is the first value in the array - as it works
1
1898
by: lPrentice | last post by:
Hello, After all this time, Linux file permissions still confuse me at times. I have a Python web-based application with an file (images) upload module. The application is running on two remote servers and a local server on my development network. The upload module works just fine when I'm uploading to my remote servers. But when I try to upload to my development server, I get a permission error. What I don't understand is who is...
1
8281
by: ndedhia1 | last post by:
I was hoping you could help me out with ftp vs sftp. Below is a method that I have that I call to ftp files from one unix box to another in house, but soon, we will have to ftp from here to NY so we have to start using sftp. I know that we have open ssh on our unix boxes but was wondering how different the syntax would be, going from ftp to sftp. Here is a method that I have written in my java code for ftping a file to a specific...
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9601
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,...
0
10635
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10115
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9198
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7653
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5687
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3013
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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

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