Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing multiple images to Browser

 
milan doshi
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am reading a file and then try to pass it as an output of a Servlet response as follows:

ServletOutputStream out = response.getOutputStream();
File file = new File("somelaocation\\image1.jpeg");
response.setContentType("image/jpeg");
response.setHeader("Content-Disposition","inline");

FileInputStream in = new FileInputStream(file);
byte[] bytes = new byte[10000];
// Read in file and at the same time write the stream to the resposne.
while (true) {
int count = in.read(bytes);
if (count == -1)
break;
out.write(bytes, 0, count);
}

Now I have to pass many images to the output. So how is that accomplished?

I just tried a very RAW way of doing it (i.e by reading another file and passing it out again ) but it just doesnt work:

file = new File("someImage/image2.jpeg");
in = new FileInputStream(file);
byte[] bytes2 = new byte[10000];
while (true) {
count = in.read(bytes2);
if (count== -1)
break;
out.write(bytes2, 0, count);

}

Please advice. It only passes the first image.BOTH the images do not come SIMULTANEOUSLY! Matter is crucial.

Thanks in advance,
MD
[ April 08, 2005: Message edited by: milan doshi ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at SimpleStream at http://simple.souther.us.
The browser will assemble the page out of multiple requests.
The first is for the HTML. It then reads the HTML and makes a new request for each image.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please advice. It only passes the first image.BOTH the images do not come SIMULTANEOUSLY! Matter is crucial.

Stop and think for a moment - what does an HTML page that contains multiple images look like?
Right - each image is obtained by a separate request in a separate IMG tag. The browser may be assembling the final page out of dozens of separate requests - it never gets multiple resources in a single request.
When in doubt about how to get a servlet to build a page design, create a mock-up in plain HTML first.
Bill
 
milan doshi
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes,

..The browser may be assembling the final page out of dozens of separate requests - it never gets multiple resources in a single request.


So what do I do now? How do I pass many images?Sorry..for sounding dumb...(but thats what I am)...

Thanks,
MD
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start by passing text, HTML to be exact.
Did you look at the example link I gave you?
 
vasu devan
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
i think ur hardcoding works well..
better u may try above option told by ther professionals..

and also try this option .

out.flush(); method after writing output streams..

cheers
vasu
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic