954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

help me to read a file from another machine in the network.

haii
I have a requirement in which i want to read a file from another
machine in the network.
I want to know how to set the URL

Thanks in advance

ajithraj
Junior Poster in Training
71 posts since Jan 2008
Reputation Points: 10
Solved Threads: 1
 

And how exactly is the target file shared on the network ?? FTP, HTTP ?? or just by SMB (Windows Sharing) ??

You need to divulge more details as to what exactly are you trying to achieve.

Also although I know its useless saying this but still read this essay and learn how to frame your questions on a forum like this, it is you who will benefit most because if you ask questions correctly or ask the correct questions, more people will feel like answering to your query.

stephen84s
Nearly a Posting Virtuoso
1,443 posts since Jul 2007
Reputation Points: 668
Solved Threads: 154
 

As mentioned above how is the file shared ? But more importantly whats this got to do with Java ? Please put your question in proper context so that we are able to understand and in turn help you better.

verruckt24
Posting Shark
952 posts since Nov 2008
Reputation Points: 485
Solved Threads: 89
 

really Sorry .....and thanks for answering me....

I want to read an image from a machine in network using http...
Actually my need is to read the image from a system and to store this in another one
I know that it can be done only by setting the Url..


///verruckt24:::But more importantly whats this got to do with Java ?
I dont understand this...i want to access this from a java class

ajithraj
Junior Poster in Training
71 posts since Jan 2008
Reputation Points: 10
Solved Threads: 1
 
///verruckt24:::But more importantly whats this got to do with Java ? I dont understand this...i want to access this from a java class

Verruckt asked that because you simply had forgotten to mention that fact.I know that it can be done only by setting the Url..

I do not what you are talking about here, but to access files over HTTP you will definitely be needing the URL and URLConnection classes. They can be used connect to URLs over an HTTP connection.

Here is an example that deals with text files.

stephen84s
Nearly a Posting Virtuoso
1,443 posts since Jul 2007
Reputation Points: 668
Solved Threads: 154
 

Thanksss.. But i still have problems..


The code i wrote is :::
URL hp = new URL("http", "www.rediff.com", 80, "/");
URLConnection hpCon = hp.openConnection();


but hpCon.getContentLength() = -1....
It means that No Content is Available....

why this happens...

ajithraj
Junior Poster in Training
71 posts since Jan 2008
Reputation Points: 10
Solved Threads: 1
 

To quote the Javadocs of URLConnection to which I had already linked to in the previous post :

1. The connection object is created by invoking the openConnection method on a URL. 2. The setup parameters and general request properties are manipulated. 3. The actual connection to the remote object is made, using the connect method. 4. The remote object becomes available. The header fields and the contents of the remote object can be accessed.

So you need to first connect and then check whether the content is available.

stephen84s
Nearly a Posting Virtuoso
1,443 posts since Jul 2007
Reputation Points: 668
Solved Threads: 154
 
public void  URLReader(){
       try{
            URL logic = new URL("http://your host name & port number/folder/");
            BufferedReader in = new BufferedReader(new InputStreamReader(logic.openStream()));

            String inputLine;
      
            while ((inputLine = in.readLine()) != null)
                System.out.println(inputLine);

            in.close();
      }
      catch (MalformedURLException e) {
            System.out.println ("Malformed URL= "+e);
       }    
        catch (IOException e) {
       System.out.println ("IO Exception = "+e);
      
    }
       
Albert    
}
AlbertPi
Light Poster
33 posts since Nov 2008
Reputation Points: 9
Solved Threads: 2
 

AlbertPi, please use [code] [/code] tags if you are going to post code examples.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

okk...this is what i too is trying now..but since my connection is through a proxy server i want to add its IP..but the problem is that how can i get the IP adress of my proxy server...

Thanks..

ajithraj
Junior Poster in Training
71 posts since Jan 2008
Reputation Points: 10
Solved Threads: 1
 

Now to connect via a Proxy **I think** you need to use the following method while opening the URL Connection using this "openConnection()" method of the URL class.

It takes an object of the Proxy class as an argument.

To construct the Proxy using the Proxy( Proxy ( ProxyType , SocketAddress )) constructor itself you will need two items, First is the Proxy type, I am guessing yours is an HTTP proxy server so use Proxy.Type.HTTP as the first argument.
Next you will need a SocketAddress, the SocketAddress class is itself abstract so you will need to use the InetSocketAddress class which is its concrete sub class. Using InetSocketAddress(String hostname, int port) create an object of the InetSocketAddress of your Proxy Server (using the host name and port on which your proxy is running).

So that **should** do the job of getting past your Proxy.

stephen84s
Nearly a Posting Virtuoso
1,443 posts since Jul 2007
Reputation Points: 668
Solved Threads: 154
 

A proxy server is a server that services the requests of its clients by forwarding requests to other servers and acts as a gateway.

Ask your networking admin. about ip address of your proxy server ? :) :-)

AlbertPi
Light Poster
33 posts since Nov 2008
Reputation Points: 9
Solved Threads: 2
 

Using this code the file can be uploaded to the server

<%@ page import="java.io.*" %>

<%
String contentType = request.getContentType();
System.out.println("Content type is :: " +contentType);
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) {
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();

byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while (totalBytesRead < formDataLength) {
byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
totalBytesRead += byteRead;
}


String file = new String(dataBytes);
String saveFile = file.substring(file.indexOf("filename=\"") + 10);
//out.print("FileName:" + saveFile.toString());
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
//out.print("FileName:" + saveFile.toString());
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
//out.print("FileName:" + saveFile.toString());

//out.print(dataBytes);

int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
//out.println(boundary);
int pos;
pos = file.indexOf("filename=\"");

pos = file.indexOf("\n", pos) + 1;

pos = file.indexOf("\n", pos) + 1;

pos = file.indexOf("\n", pos) + 1;


int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;

out.println(boundaryLocation+"pppp"+file.length()+"ooooooooooooo"+(endPos-startPos)+"kkkkk");

saveFile = "/usr/programs/apache-tomcat-6.0.16/webapps/Pinnacle/hr_photoGallery/" + saveFile;
FileOutputStream fileOut = new FileOutputStream(saveFile);


//fileOut.write(dataBytes);
fileOut.write(dataBytes, startPos, file.length());
fileOut.flush();
fileOut.close();

out.println("File saved as " +saveFile);

}
%>
ajithraj
Junior Poster in Training
71 posts since Jan 2008
Reputation Points: 10
Solved Threads: 1
 

See the comments on this code here:

http://www.daniweb.com/forums/thread162658.html

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You