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

Recommended Answers

All 13 Replies

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.

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.

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

///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.

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...

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.

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, please use [code] [/code] tags if you are going to post code examples.

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..

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.

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 ? :) :-)

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);

}
%>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.