File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Passing multiple images to Browser Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Passing multiple images to Browser" Watch "Passing multiple images to Browser" New topic
Author

Passing multiple images to Browser

milan doshi
Ranch Hand

Joined: Nov 03, 2000
Posts: 43
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

Joined: Dec 11, 2004
Posts: 13410

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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
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

Joined: Nov 03, 2000
Posts: 43
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

Joined: Dec 11, 2004
Posts: 13410

Start by passing text, HTML to be exact.
Did you look at the example link I gave you?
vasu devan
Ranch Hand

Joined: Apr 14, 2005
Posts: 38
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing multiple images to Browser