Java Newbie... Read file at HTTP

Discussion in 'Java' started by sven.dz@gmail.com, Apr 29, 2005.

  1. Guest

    Hi,

    I need the code for "loading" a picture over www (ex.
    http://www.xyz.com/pic.jpg). I need only to load it, I do not need to
    save it on my disk or something like that... Sounds useless and it
    is... But I want to learn it and I need it :)

    Greetings
    , Apr 29, 2005
    #1
    1. Advertising

  2. Lloyd Duke Guest

    How do you mean *load*?
    If you want an image:

    BufferedImage bi = ImageIO.read(urlForXYZ.com);
    you then have the image.

    If you need just a file over HTTP:

    URL image = new URL(urlForXYZ.com, pic.jpg);
    URLConnection con = getImages.openConnection();
    con.setDoInput(true);
    con.setUseCaches(false);
    InputStream stream = con.getInputStream();
    ....
    do something with the stream
    stream.close();
    con.close();


    wrote:
    > Hi,
    >
    > I need the code for "loading" a picture over www (ex.
    > http://www.xyz.com/pic.jpg). I need only to load it, I do not need to
    > save it on my disk or something like that... Sounds useless and it
    > is... But I want to learn it and I need it :)
    >
    > Greetings
    >


    --
    http://lloyd-duke.net + news AT website.domain
    Lloyd Duke, Apr 30, 2005
    #2
    1. Advertising

  3. Lloyd Duke Guest

    OOPS!
    sorry cut and paste got the best of me :-(

    that second part should read

    If you need just a file over HTTP:

    URL image = new URL(urlForXYZ.com, "pic.jpg");
    URLConnection con = image.openConnection();
    con.setDoInput(true);
    con.setUseCaches(false);
    InputStream stream = con.getInputStream();
    ....
    do something with the stream
    stream.close();
    con.close();


    Lloyd Duke wrote:
    > How do you mean *load*?
    > If you want an image:
    >
    > BufferedImage bi = ImageIO.read(urlForXYZ.com);
    > you then have the image.
    >
    > If you need just a file over HTTP:
    >
    > URL image = new URL(urlForXYZ.com, pic.jpg);
    > URLConnection con = getImages.openConnection();
    > con.setDoInput(true);
    > con.setUseCaches(false);
    > InputStream stream = con.getInputStream();
    > ...
    > do something with the stream
    > stream.close();
    > con.close();
    >
    >
    > wrote:
    >
    >> Hi,
    >>
    >> I need the code for "loading" a picture over www (ex.
    >> http://www.xyz.com/pic.jpg). I need only to load it, I do not need to
    >> save it on my disk or something like that... Sounds useless and it
    >> is... But I want to learn it and I need it :)
    >>
    >> Greetings
    >>

    >


    --
    http://lloyd-duke.net + news AT website.domain
    Lloyd Duke, Apr 30, 2005
    #3
  4. Guest

    Hi Lloyd,

    so... I've made some mistakes... I guess!
    The compiler says:

    Building target "MakeTraffic" with build style "Development"
    (optimization:level 'N/A', debug-symbols:eek:ff) - (5 errors)
    frameworkjars=""
    for i in `echo ` ; do if [ -f "$i" ] ; then
    frameworkjars="$frameworkjars":"$i" ; fi ; done
    classpath="/Users/sdz/Desktop/Tempora¨res
    Verzeichnis/XCode/maketraffic/MakeTraffic/build/MakeTraffic.build/MakeTraffic.build/JavaClasses$frameworkjars:"`/usr/bin/javaconfig
    DefaultClasspath`
    /usr/bin/javac -J-Xms64m -J-XX:NewSize=4M -J-Dfile.encoding=UTF8
    -g -encoding MACINTOSH -sourcepath "/Users/sdz/Desktop/Tempora¨res
    Verzeichnis/XCode/maketraffic/MakeTraffic/." -classpath "$classpath"
    -d "/Users/sdz/Desktop/Tempora¨res
    Verzeichnis/XCode/maketraffic/MakeTraffic/build/MakeTraffic.build/MakeTraffic.build/JavaClasses"
    '@/Users/sdz/Desktop/Tempora¨res
    Verzeichnis/XCode/maketraffic/MakeTraffic/build/MakeTraffic.build/MakeTraffic.build/JavaFileList'

    MakeTraffic.java:3: cannot resolve symbol
    symbol : class URL
    location: class MakeTraffic
    URL image = new URL("www.darknob.de","IMGA0356.JPG");
    ^
    MakeTraffic.java:3: cannot resolve symbol
    symbol : class URL
    location: class MakeTraffic
    URL image = new URL("www.darknob.de","IMGA0356.JPG");
    ^
    MakeTraffic.java:4: cannot resolve symbol
    symbol : class URLConnection
    location: class MakeTraffic
    URLConnection con = image;
    ^
    MakeTraffic.java:5: cannot resolve symbol
    symbol : method openConnection ()
    location: class MakeTraffic
    openConnection();
    ^
    MakeTraffic.java:8: cannot resolve symbol
    symbol : class InputStream
    location: class MakeTraffic
    InputStream stream = con.getInputStream();
    ^
    5 errors
    MakeTraffic.java:3: cannot resolve symbol
    symbol : class URL
    MakeTraffic.java:3: cannot resolve symbol
    symbol : class URL
    MakeTraffic.java:4: cannot resolve symbol
    symbol : class URLConnection
    MakeTraffic.java:5: cannot resolve symbol
    symbol : method openConnection ()
    MakeTraffic.java:8: cannot resolve symbol
    symbol : class InputStream


    :-( I'm a java newbie!
    , Apr 30, 2005
    #4
  5. Lloyd Duke Guest

    Could you post the source that caused this error?
    It seems that at *minimum* you don't have java.net or java.io imported
    into your source. You should familurise yourself with these packages if
    you wish to do any type of network programing. Also the NIO package is
    WELL worth the time to look at.


    wrote:
    > Hi Lloyd,
    >
    > so... I've made some mistakes... I guess!
    > The compiler says:
    >
    > Building target "MakeTraffic" with build style "Development"
    > (optimization:level 'N/A', debug-symbols:eek:ff) - (5 errors)
    > frameworkjars=""
    > for i in `echo ` ; do if [ -f "$i" ] ; then
    > frameworkjars="$frameworkjars":"$i" ; fi ; done
    > classpath="/Users/sdz/Desktop/Tempora¨res
    > Verzeichnis/XCode/maketraffic/MakeTraffic/build/MakeTraffic.build/MakeTraffic.build/JavaClasses$frameworkjars:"`/usr/bin/javaconfig
    > DefaultClasspath`
    > /usr/bin/javac -J-Xms64m -J-XX:NewSize=4M -J-Dfile.encoding=UTF8
    > -g -encoding MACINTOSH -sourcepath "/Users/sdz/Desktop/Tempora¨res
    > Verzeichnis/XCode/maketraffic/MakeTraffic/." -classpath "$classpath"
    > -d "/Users/sdz/Desktop/Tempora¨res
    > Verzeichnis/XCode/maketraffic/MakeTraffic/build/MakeTraffic.build/MakeTraffic.build/JavaClasses"
    > '@/Users/sdz/Desktop/Tempora¨res
    > Verzeichnis/XCode/maketraffic/MakeTraffic/build/MakeTraffic.build/MakeTraffic.build/JavaFileList'
    >
    > MakeTraffic.java:3: cannot resolve symbol
    > symbol : class URL
    > location: class MakeTraffic
    > URL image = new URL("www.darknob.de","IMGA0356.JPG");
    > ^
    > MakeTraffic.java:3: cannot resolve symbol
    > symbol : class URL
    > location: class MakeTraffic
    > URL image = new URL("www.darknob.de","IMGA0356.JPG");
    > ^
    > MakeTraffic.java:4: cannot resolve symbol
    > symbol : class URLConnection
    > location: class MakeTraffic
    > URLConnection con = image;
    > ^
    > MakeTraffic.java:5: cannot resolve symbol
    > symbol : method openConnection ()
    > location: class MakeTraffic
    > openConnection();
    > ^
    > MakeTraffic.java:8: cannot resolve symbol
    > symbol : class InputStream
    > location: class MakeTraffic
    > InputStream stream = con.getInputStream();
    > ^
    > 5 errors
    > MakeTraffic.java:3: cannot resolve symbol
    > symbol : class URL
    > MakeTraffic.java:3: cannot resolve symbol
    > symbol : class URL
    > MakeTraffic.java:4: cannot resolve symbol
    > symbol : class URLConnection
    > MakeTraffic.java:5: cannot resolve symbol
    > symbol : method openConnection ()
    > MakeTraffic.java:8: cannot resolve symbol
    > symbol : class InputStream
    >
    >
    > :-( I'm a java newbie!
    >


    --
    http://lloyd-duke.net + news AT website.domain
    Lloyd Duke, Apr 30, 2005
    #5
  6. Guest

    That's my source... :-(

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

    class MakeTraffic {
    MakeTraffic () {
    URL image = new URL("www.darknob.de","IMGA0356.JPG");
    URLConnection con = image;
    openConnection();
    con.setDoInput(true);
    con.setUseCaches(false);
    InputStream stream = con.getInputStream();
    //
    stream.close();
    con.close();
    }

    public static void main(String [] args) {
    new MakeTraffic();
    }
    }

    Get always errors, I hope that I'm not to stupid for Java! :-(
    , Apr 30, 2005
    #6
  7. writes:

    > URLConnection con = image;
    > openConnection();


    Should be:

    URLConnection con = image.openConnection();
    Tor Iver Wilhelmsen, Apr 30, 2005
    #7
  8. Guest

    Ok... Got already some mistakes:

    Building target "MakeTraffic" with build style "Development"
    (optimization:level 'N/A', debug-symbols:eek:ff) - (2 errors)
    frameworkjars=""
    for i in `echo ` ; do if [ -f "$i" ] ; then
    frameworkjars="$frameworkjars":"$i" ; fi ; done
    classpath="/Users/sdz/Desktop/Tempora¨res
    Verzeichnis/XCode/maketraffic/MakeTraffic/build/MakeTraffic.build/MakeTraffic.build/JavaClasses$frameworkjars:"`/usr/bin/javaconfig
    DefaultClasspath`
    /usr/bin/javac -J-Xms64m -J-XX:NewSize=4M -J-Dfile.encoding=UTF8
    -g -encoding MACINTOSH -sourcepath "/Users/sdz/Desktop/Tempora¨res
    Verzeichnis/XCode/maketraffic/MakeTraffic/." -classpath "$classpath"
    -d "/Users/sdz/Desktop/Tempora¨res
    Verzeichnis/XCode/maketraffic/MakeTraffic/build/MakeTraffic.build/MakeTraffic.build/JavaClasses"
    '@/Users/sdz/Desktop/Tempora¨res
    Verzeichnis/XCode/maketraffic/MakeTraffic/build/MakeTraffic.build/MakeTraffic.build/JavaFileList'

    MakeTraffic.java:6: cannot resolve symbol
    symbol : constructor URL (java.lang.String,java.lang.String)
    location: class java.net.URL
    URL image = new URL("www.darknob.de","IMGA0356.JPG");
    ^
    MakeTraffic.java:13: cannot resolve symbol
    symbol : method close ()
    location: class java.net.URLConnection
    con.close();
    ^
    2 errors
    MakeTraffic.java:6: cannot resolve symbol
    symbol : constructor URL (java.lang.String,java.lang.String)
    MakeTraffic.java:13: cannot resolve symbol
    symbol : method close ()




    With that Source code:

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

    class MakeTraffic {
    MakeTraffic () {
    URL image = new URL("www.darknob.de","IMGA0356.JPG");
    URLConnection con = image.openConnection();
    con.setDoInput(true);
    con.setUseCaches(false);
    InputStream stream = con.getInputStream();
    //
    stream.close();
    con.close();
    }

    public static void main(String [] args) {
    new MakeTraffic();
    }
    }


    Any idea?
    , May 1, 2005
    #8
  9. In article <>,
    wrote:

    > MakeTraffic.java:6: cannot resolve symbol
    > symbol : constructor URL (java.lang.String,java.lang.String)
    > location: class java.net.URL
    > URL image = new URL("www.darknob.de","IMGA0356.JPG");


    You need to take a look at the documentation for the classes you are
    using. On my Mac, for 1.4.2 with the dev tools and java dev tools
    installed, they are in
    file:///System/Library/Frameworks/JavaVM.framework/Versions/1.4.2/Resourc
    es/Documentation/Reference/doc/api/java/net/URL.html .

    In this case, there is no (String,String) constructor for URL. There is
    an URL(String host, int port, String file), and an URL(String), among
    others.

    > ^
    > MakeTraffic.java:13: cannot resolve symbol
    > symbol : method close ()
    > location: class java.net.URLConnection
    > con.close();


    Again, read the javadocs. Do you see a "close" method in your jdk
    javadocs? If not, then it does not exist and cannot be used.

    The javadocs are a good thing to peruse when you get error messages, as
    they often contain interesting facts about how to use the classes in
    question. If they do not contain enough information, go hunting for
    tutorials. I find the Sun-provided ones pretty good.

    Scott
    Scott Ellsworth, May 2, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mike
    Replies:
    5
    Views:
    960
    Keith M. Corbett
    Sep 21, 2004
  2. Sreejith K
    Replies:
    24
    Views:
    1,015
    Sreejith K
    Mar 24, 2009
  3. Alex Dowad
    Replies:
    4
    Views:
    268
    Michel Demazure
    May 1, 2010
  4. iMath
    Replies:
    0
    Views:
    121
    iMath
    Aug 7, 2013
  5. iMath
    Replies:
    0
    Views:
    114
    iMath
    Aug 7, 2013
Loading...

Share This Page