?
Solved

Reading a file in the internet

Posted on 2005-03-31
24
Medium Priority
?
250 Views
Last Modified: 2012-05-05
Whats wrong with this one ? I compiled it and there was no error but
why is it not displaying the file in the dos prompt?

import java.net.*;
import java.io.*;

public class NetFile {

      public static void main(String args[]) {
            
            try {

                  String line;
                      StringBuffer buf = new StringBuffer();

                  String url = "http://mysite.com/Sample.txt";
                  URL path = new URL(url);

                  URLConnection conn = path.openConnection();
                        conn.connect();

                        BufferedReader data = new BufferedReader(new InputStreamReader(conn.getInputStream()));

                        while ((line = data.readLine()) != null) {
                          buf.append(line + "\n");
                        }

                        System.out.println(buf.toString());
                        data.close();
            } catch(Exception e) {
            }
      }
}
0
Comment
Question by:simple21
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 12
  • 9
  • 3
24 Comments
 
LVL 15

Expert Comment

by:aozarov
ID: 13678062
The program looks fine to me (I changed the URL and runned sucessfuly).
Does http://mysite.com/Sample.txt exist?
can you print the exception:
change:
 } catch(Exception e) {
          }

to:
 } catch(Exception e) {
e.printStackTrace();
          }
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13678081
check what happens when you use your browser to get http://mysite.com/Sample.txt
0
 

Author Comment

by:simple21
ID: 13678129
java.net.ConnectException: Connection refused: connect
        at java.net.PlainSocketImpl.socketConnect(Native Met
        at java.net.PlainSocketImpl.doConnect(Unknown Source
        at java.net.PlainSocketImpl.connectToAddress(Unknown
        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)

It seems there was a problem in the file that I'm accessing or it can be because I'm behind a firewall?
Give me a link to your file and I'll try to read it thanks.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:simple21
ID: 13678134
> Does http://mysite.com/Sample.txt exist?
That is just a dummy site my site really has the file.
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13678139
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13678144
>> This is just a dummy site
I assumed so. did you try to request that URL using your browser?
0
 

Author Comment

by:simple21
ID: 13678181
> I assumed so. did you try to request that URL using your browser?

Yes and its working so do you think because I'm behind a firewall ?
0
 
LVL 6

Expert Comment

by:CodingExperts
ID: 13678202
try {
      InputStream in=url.openStream ();
      BufferedReader dis =
        new BufferedReader (new InputStreamReader (in));
      fBuf = new StringBuffer  () ;

      while ( (line = dis.readLine ()) != null) {
        fBuf.append (line + "\n");
      }

      in.close ();
    }

-CE
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13678217
CodingExperts, why do you think url.openStream() is going to change anything?
>> Yes and its working so do you think because I'm behind a firewall ?
So, I assume your program was working for you with google URL but what about requesting the URL
from your browser on the same machine that you run the program?
firewall might be one reason, but then the browser should fail as well.
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13678218
is your site using port 80 or 8080?
0
 

Author Comment

by:simple21
ID: 13678256
> So, I assume your program was working for you with google URL but what about requesting the URL
> from your browser on the same machine that you run the program?

Everything works fine w/ my browser but when I try to run my codes its giving this :

 java.net.ConnectException: Connection refused: connect
        at java.net.PlainSocketImpl.socketConnect(Native Met
        at java.net.PlainSocketImpl.doConnect(Unknown Source
        at java.net.PlainSocketImpl.connectToAddress(Unknown
        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)

>  is your site using port 80 or 8080?
The website i'm trying to access is myjavaserver.com its using port : 21
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13678267
You are aware that port 21 is used normally by FTP...
But if this is your case then try http://myjavaserver.com:21/Sample.txt
That is strange that putting http://myjavaserver.com/Sample.txt worked as is in your web browser.
0
 
LVL 6

Expert Comment

by:CodingExperts
ID: 13678272
You can use the httpunit to do the required, wherein you can connect thru a proxy server.

-CE
0
 
LVL 6

Expert Comment

by:CodingExperts
ID: 13678283
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13678300
BTW, does your browser have proxy settings enabled?
If so, then you will need to set them for you java app (check this link for the settings: http://www.davidreilly.com/java/java_network_programming/#2.4)
0
 

Author Comment

by:simple21
ID: 13678341
I'm actually at work the site and file that I'm trying to access is on I can actually see it but when I run the codes
as I said its giving the error so what do you think about it?
0
 

Author Comment

by:simple21
ID: 13678355
> BTW, does your browser have proxy settings enabled?
> If so, then you will need to set them for you java app (check this link for the settings:

Yes I have a proxy enabled and how will I add this to my codes ?

Properties sysProperties = System.getProperties();

// Specify proxy settings
sysProperties.put("proxyHost", "myhost");
sysProperties.put("proxyPort", "myport");
sysProperties.put("proxySet",  "true");
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13678358
Right. before you do anything else
0
 
LVL 15

Accepted Solution

by:
aozarov earned 1000 total points
ID: 13678362
or as mentioned using the jvm command line options:
java -DproxySet=true -DproxyHost=myhost -DproxyPort=myport NetFile
0
 

Author Comment

by:simple21
ID: 13678387
Haha that works cool ;)
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13678392
Nice :-)
0
 

Author Comment

by:simple21
ID: 13678401
So its really something about work related proxies ey :D
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13678428
Yep, you had a good hunch about it ;-)
0
 

Author Comment

by:simple21
ID: 13678444
Thanks for the help I really appriciate both of you.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month14 days, 12 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question