Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 258
  • Last Modified:

Reading a file in the internet

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
simple21
Asked:
simple21
  • 12
  • 9
  • 3
1 Solution
 
aozarovCommented:
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
 
aozarovCommented:
check what happens when you use your browser to get http://mysite.com/Sample.txt
0
 
simple21Author Commented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
simple21Author Commented:
> Does http://mysite.com/Sample.txt exist?
That is just a dummy site my site really has the file.
0
 
aozarovCommented:
0
 
aozarovCommented:
>> This is just a dummy site
I assumed so. did you try to request that URL using your browser?
0
 
simple21Author Commented:
> 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
 
CodingExpertsCommented:
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
 
aozarovCommented:
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
 
aozarovCommented:
is your site using port 80 or 8080?
0
 
simple21Author Commented:
> 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
 
aozarovCommented:
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
 
CodingExpertsCommented:
You can use the httpunit to do the required, wherein you can connect thru a proxy server.

-CE
0
 
CodingExpertsCommented:
0
 
aozarovCommented:
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
 
simple21Author Commented:
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
 
simple21Author Commented:
> 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
 
aozarovCommented:
Right. before you do anything else
0
 
aozarovCommented:
or as mentioned using the jvm command line options:
java -DproxySet=true -DproxyHost=myhost -DproxyPort=myport NetFile
0
 
simple21Author Commented:
Haha that works cool ;)
0
 
aozarovCommented:
Nice :-)
0
 
simple21Author Commented:
So its really something about work related proxies ey :D
0
 
aozarovCommented:
Yep, you had a good hunch about it ;-)
0
 
simple21Author Commented:
Thanks for the help I really appriciate both of you.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 12
  • 9
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now