| Author |
How to read a file from remote System
|
somenath chatterjee
Greenhorn
Joined: Mar 20, 2008
Posts: 16
|
|
Hi friends, I want to read a file from remote computer, verifying with user authentication using java.If any one done this kind of code please help me.. Thank you.
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
|
JDBC wouldn't help you with that. Let's move this somewhere more appropriate.
|
 |
Norm Radder
Ranch Hand
Joined: Aug 10, 2005
Posts: 681
|
|
There are many ways to do this. Depends on what software in running on the server and on the client. Perhaps you could explain about that. On the server what services such as JSP or CGI to handle the verification. On the client, would there be a browser to read an HTML page and send <Form data to the server? Or do you intend to write the both sides from scratch?
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12929
|
|
|
You'll first need to decide on what network protocol you're going to use to talk between your computer and the server. If you'd use FTP for example, you'd need to set up an FTP service on the server. You could use for example the Apache Jakarta Commons Net library to communicate with the server via FTP.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Collins Mbianda
Ranch Hand
Joined: Aug 11, 2007
Posts: 259
|
|
hello, I think Commons Virtual File System is interesting to access file systems. It supports FTP, HTTP, HTTPS, SFTP, etc... Hope it help ! Collins
|
SCJP 5.0 | SCWCD 1.4
|
 |
somenath chatterjee
Greenhorn
Joined: Mar 20, 2008
Posts: 16
|
|
I am sending you the code, but its giving me an exception please tell me how can i handle this exception- package remoteFileTest; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.net.URL; import java.net.URLConnection; import org.apache.commons.net.ftp.*; public class AccessRemotFile { public static void main(String[] args) { FTPClient ftp = null; String usrID = "userID"; String pass = "password"; String url = "SystemIP"; //String fileName = "E:/vishal/vishal.txt"; File file = new File("E:/FOLDER/file.txt"); StringBuffer sb = new StringBuffer("ftp://"); sb.append(usrID); sb.append(':'); sb.append(pass); sb.append('@'); sb.append(url); sb.append('/'); sb.append(':'); //sb.append(fileName); BufferedInputStream bis = null; BufferedOutputStream bos = null; System.out.println("sb: "+sb); try { URL url2 = new URL(sb.toString()); URLConnection connection = url2.openConnection(); bos = new BufferedOutputStream(connection.getOutputStream()); bis = new BufferedInputStream(new FileInputStream(file.getName())); int i; while((i = bis.read())!=-1){ bos.write(i); } } catch (Exception e) { System.out.println("Exception in AccessRemotFile: "+e); e.printStackTrace(); }finally{ if (bis != null) try { bis.close(); } catch (Exception ioe) { /* ignore*/ } if (bos != null) try { bos.close(); } catch (Exception ioe) { /* ignore*/ } } } } Exception : Exception in AccessRemotFile: java.net.ConnectException: Connection refused: connect java.net.ConnectException: Connection refused: connect at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.PlainSocketImpl.doConnect(Unknown Source) at java.net.PlainSocketImpl.connectToAddress(Unknown Source) at java.net.PlainSocketImpl.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at sun.net.NetworkClient.doConnect(Unknown Source) at sun.net.NetworkClient.openServer(Unknown Source) at sun.net.ftp.FtpClient.openServer(Unknown Source) at sun.net.ftp.FtpClient.openServer(Unknown Source) at sun.net.www.protocol.ftp.FtpURLConnection.connect(Unknown Source) at sun.net.www.protocol.ftp.FtpURLConnection.getOutputStream(Unknown Source) at remoteFileTest.AccessRemotFile.main(AccessRemotFile.java:42)
|
 |
Collins Mbianda
Ranch Hand
Joined: Aug 11, 2007
Posts: 259
|
|
Without using that programm, Are you able to connect to SystemIP with ID "userID" and password "password" from your post ? It seem you don't have enough rigths to connect to that server.
|
 |
Norm Radder
Ranch Hand
Joined: Aug 10, 2005
Posts: 681
|
|
You've nicely hidden the URL you're using and then didn't copy the println() of it with your error message. It'd make it easier to help you if you'd show all the details of what is happening. Also it would help if you would put an eye catching comment on line 42 where the error is. As Collins asks: Are you able to manually connect to that server from you computer? Ie bring up a telnet like screen and enter the command and see what happens. [ June 27, 2008: Message edited by: Norm Radder ]
|
 |
 |
|
|
subject: How to read a file from remote System
|
|
|