Pulling image from other website

S

Steve W. Jackson

Steven said:
Hi All,

Does anybody have any sourcecodes, examples on how I can get an image from a
different website?
I don't mean something like <img
src="http://www.differentwebsite.com/image.gif"> ofcourse. :)

Thanks for any tips/help!

Steven.

Instead of what you don't mean, how about explaining what you do want?
Java code for an applet that can retrieve an image file from a URL?
Something else entirely?

= Steve =
 
K

klynn47

Steven,
This is what I would do.

public void getImage(String url) {
try {
BufferedInputStream input = new BufferedInputStream(new
URL(url).openStream());

byte[] bytes = new byte[0];

byte[] buffer = new byte[4000*1024];

int length = 0;

while ((length = input.read(buffer,0,buffer.length)) != -1) {
byte[] temp = new byte[bytes.length+length];

System.arraycopy(bytes,0,temp,0,bytes.length);
System.arraycopy(buffer,0,temp,bytes.length,length);

bytes = temp;
}

return(Toolkit.getDefaultToolkit().createImage(bytes));
}

There may be a few syntax errors. But do you understand what the code
does?
 
S

Steven

To be honest, no.

How would I put this in a JSP page?

Thanks for the help!

Steven.
 
S

Steven

Hi,

Well I can't do <img src=http://other.website.com/img.jpg> because the
website I want to show it
on is a secure website (https) an doing it this why the user/visitor will
get a message if he wants to
display nonsecure items. So I will have to stream the image in and because
the application uses jsp
it need to be done in jsp or servlet.

As I'm a newbie to jsp I don't really understand how I should implement the
code
(e-mail address removed) (see thread above) gave. I think this will have to
be done in an servlet
but I'm not sure and have no clue how.

Thanks for any help!

Steven.
 
O

opalpa

wow dude, like I read the final symptom "visitor will
get a message if he wants to display nonsecure items" and like that
isn't about streaming images from "other" websites but something
related to a means of acquisition.

Also images are streamed to client browsers in regular and secure http
whether you are using servlets, jsp, or html's image tag.
 
D

David Van de Voorde

Images or any object embedded in a browser's page (ultimately, all HTML),
will be retrieved SEPARATELY by the browser. That is, the browser will see
the extrenal link (to the image or other object), and will issue a new
request to the specific server. In short: the browser will first load the
HTML, then all the images.

For this particular problem, I would take the pragmatic approach of copying
the image to my own webserver. If it will always be the same image, simpply
copy it. If it is changing over time (eg a wheaterforecast that has always
the same name but changes several times a day), I would run a background
process that copies the image to your local server whenever it is needed.

David.
 
J

Jon Caldwell

Steven,
This is what I would do.

public void getImage(String url) {
try {
BufferedInputStream input = new BufferedInputStream(new
URL(url).openStream());

byte[] bytes = new byte[0];

byte[] buffer = new byte[4000*1024];

int length = 0;

while ((length = input.read(buffer,0,buffer.length)) != -1) {
byte[] temp = new byte[bytes.length+length];

System.arraycopy(bytes,0,temp,0,bytes.length);
System.arraycopy(buffer,0,temp,bytes.length,length);

bytes = temp;
}

return(Toolkit.getDefaultToolkit().createImage(bytes));
}

There may be a few syntax errors. But do you understand what the code
does?
Toolkit.getDefaultToolkit().createImage(java.net.URL) should save you
some steps.
 

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

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top