Java Newbie... Read file at HTTP

S

sven.dz

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
 
L

Lloyd Duke

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

Lloyd Duke

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

sven.dz

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!
 
L

Lloyd Duke

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.


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!
 
S

sven.dz

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! :-(
 
S

sven.dz

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?
 
S

Scott Ellsworth

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top