This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Servlets and the fly likes wrting multiple JPEG files to a Servlet output stream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "wrting multiple JPEG files to a Servlet output stream" Watch "wrting multiple JPEG files to a Servlet output stream" New topic
Author

wrting multiple JPEG files to a Servlet output stream

Madhu Bompaly
Greenhorn

Joined: Oct 11, 2007
Posts: 17
Hi,

Iam trying to wrtie multiple JPEG files to a Servlet output stream.
it is writing the first JPEG file and after that it is throwing some exception saying that.
"Connection reset by peer: socket write error".
or some times it is saying that "cannot set Header, Response already comitted".

Iam posting my code here.

try{
response.setContentType("image/jpg");
response.setHeader("Content-disposition","inline; filename=\""+destinationFile+"\"");

FileInputStream is = new FileInputStream(destinationFile);
ServletOutputStream os = response.getOutputStream();
System.out.println("this is inside try block");
int c;
while((c=is.read())!= -1){
os.write(c);
}
is.close();
os.close();

}catch(Exception e){
e.getMessage();
System.out.println("this is inside catch block:"+e.getMessage());

}
this is inside a for loop.

any replies greatly appreiciated,
Thanks,
Madhu.
Mladen Grabowsky
Greenhorn

Joined: Sep 19, 2007
Posts: 29

You are closing the outputstream, which flushes it, then you're trying to open it again and sending some more data through it...

open & close the outputstream outside the loop, i think you have to set the headers all at the beginning, but i could be wrong on the latter.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16095
    
  21

The HTTP protocol is precisely defined as a request/response method of communication. One request, one response. Another request, another response. That's not a Java thing, it's an Internet thing.

Each response is a single "file" (data stream). When you display a web page that has several pictures on it, the initial request returns an HTML text stream response. Embedded in the returned HTML stream are links to the various pictures - each picture has its own URL. The user's browser program understands these IMG tags as an indication that it should do additional requests, one per picture - each image URL gets a response of the picture that goes with that URL.

If you actually want to send multiple files back in a single response, what you have to do is embed them in a container-type file such as a ZIP file.


Customer surveys are for companies who didn't pay proper attention to begin with.
Madhu Bompaly
Greenhorn

Joined: Oct 11, 2007
Posts: 17
Originally posted by Mladen Girazovski:

You are closing the outputstream, which flushes it, then you're trying to open it again and sending some more data through it...

open & close the outputstream outside the loop, i think you have to set the headers all at the beginning, but i could be wrong on the latter.


Hi Mladen thanks for your reply I tried closing and opening the Servlet Stream out side the loop and also setting the headers outside,
but still Iam getting error.
Connection reset by peer: socket write error
Mladen Grabowsky
Greenhorn

Joined: Sep 19, 2007
Posts: 29
Hi Madhu,

apart from your problem, Tim is right, it is pointless to send multiple files in one stream, there is no way how they could be seperated by the client in a meaningful way, at least there is no way i would know about.
Madhu Bompaly
Greenhorn

Joined: Oct 11, 2007
Posts: 17
Hi Tim,
Thanks for your reply.
I used the ZIP file to write all the JPEG files into it and read the ZIP file and wrote it to ServletOutPutStream, but the problem is they are opening as a attachment when I set the 'Content-disposition' to both 'inline' and 'attchment'.
is there any way I can display all the iamges in a single page by reading the ZIP file.
Iam posting my code here for your reference.
Thanks,
Madhu.

if(!destinationFile.getName().equalsIgnoreCase(thisSessionFileName+".jpg")){

byte[] buf = new byte[1024];
FileInputStream in = new FileInputStream(destinationFile);

out.putNextEntry(new ZipEntry(destinationFile.getName()));

// Transfer bytes from the file to the ZIP file
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.closeEntry();
in.close();
}
}
// Complete the ZIP file
out.close();

response.setContentType("image/jpeg");
response.setHeader("Content-Disposition", "attachment; filename=" + outFilename);

FileInputStream is = new FileInputStream(outFilename);
ServletOutputStream sos = response.getOutputStream();

int c;
while ((c = is.read()) != -1) {

sos.write(c);

}
is.close();
sos.close();

}catch(Exception e){
e.printStackTrace();
System.out.println("this is inside catch block"+e.getMessage());
e.toString();
}

Originally posted by Tim Holloway:
The HTTP protocol is precisely defined as a request/response method of communication. One request, one response. Another request, another response. That's not a Java thing, it's an Internet thing.

Each response is a single "file" (data stream). When you display a web page that has several pictures on it, the initial request returns an HTML text stream response. Embedded in the returned HTML stream are links to the various pictures - each picture has its own URL. The user's browser program understands these IMG tags as an indication that it should do additional requests, one per picture - each image URL gets a response of the picture that goes with that URL.

If you actually want to send multiple files back in a single response, what you have to do is embed them in a container-type file such as a ZIP file.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Originally posted by Madhu Bompaly:
is there any way I can display all the iamges in a single page by reading the ZIP file.
From the server? No. All you can do is to send the ZIP file, it's up to the client to decide to do with it. And all the client is going to do with it is (1) save it to the client's disk -- most likely -- or (2) open it with some ZIP archive reader -- not very likely.

If you really want to display a page of images, by far the easiest way is to send an HTML page containing links to all the images. If that was your goal then you are doing it in entirely the wrong way.
Madhu Bompaly
Greenhorn

Joined: Oct 11, 2007
Posts: 17
Hi Paul,

Thanks for your reply.
my actual requirement is display a page of images.
but right now Iam using an servlet for that but it didn't work.
as you said,I need to call the servlet any way to the get images,
these images I will get dynamically from database and vary in number(some time I will get 3 or 4 images)
can you please post some code for JSP that can call the servlet and write the links for images.

Originally posted by Paul Clapham:
From the server? No. All you can do is to send the ZIP file, it's up to the client to decide to do with it. And all the client is going to do with it is (1) save it to the client's disk -- most likely -- or (2) open it with some ZIP archive reader -- not very likely.

If you really want to display a page of images, by far the easiest way is to send an HTML page containing links to all the images. If that was your goal then you are doing it in entirely the wrong way.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61310
    
  66

Originally posted by Madhu Bompaly:
[QBcan you please post some code for JSP [/QB]


You'll need to write your own code, but it's nothing that's complicated. Just like any other HTML page that needs to display multiple images, you'll need to create an <img> tag for each image. The href of the image tags can reference the servlet that sends back the data for one image.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Madhu Bompaly
Greenhorn

Joined: Oct 11, 2007
Posts: 17
Hi Paul,
Thanks for your idea.
it is working fine for me, after writing HTML links for the images.

Thanks again,
Madhu.

Originally posted by Paul Clapham:
From the server? No. All you can do is to send the ZIP file, it's up to the client to decide to do with it. And all the client is going to do with it is (1) save it to the client's disk -- most likely -- or (2) open it with some ZIP archive reader -- not very likely.

If you really want to display a page of images, by far the easiest way is to send an HTML page containing links to all the images. If that was your goal then you are doing it in entirely the wrong way.
Emanuel Kadziela
Ranch Hand

Joined: Mar 24, 2005
Posts: 186
If you want to display the actual images and not the links, you can use the fact that the browser will make a separate request per each asset referenced in the page (image, css file, javascript file, etc.). Simply intercept these requests (they will always be of the form xxx/assetPath/assetName.assetExtension where xxx is the URL of your page with the part after the last "/" missing (in other words it will chop off the page name; for example, if you have a page with a URL of "http://mydomain.com/picturePage.html" which references some image with this tag: "<img src="images/pic23.jpg"/>" then as the browser renders the page, upon encountering the image reference it will make a request to the server at the following URL: "http://mydomain.com/images/pic23.jpg"). You can do this using a filter or simply modifying your response serving method (doPost, doGet, handleRequest or some such beast) to serve up the right content based on what is being requested.
Mohmedabbas Burmawala
Greenhorn

Joined: Jun 16, 2009
Posts: 1
Hi,

We had almost the same requirement viz. rendering multiple images in a single web page, the images to be constructed (i.e. gathered) at runtime at server side. The way we accomplished is:
a) Wrote JSP custom tag handler which writes the image file content to a ServletOutputStream
ServletOutputStream outputStream = pageContext.getResponse().getOutputStream();
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream,"UTF-8");
BufferedInputStream bufferedinputstream;
byte abyte0[] = new byte[4096];
int bytesRead;

bufferedinputstream = new BufferedInputStream(new FileInputStream(new File(fileName)));

while((bytesRead = bufferedinputstream.read(abyte0, 0, 4096)) != -1)
outputStream.write(abyte0, 0, bytesRead);

outputStreamWriter.flush();
outputStreamWriter.close();
bufferedinputstream.close();

b) Wrote a JSP which uses this custom tag. Call it "externalImg.jsp". One attribute that it expects (and this it inturn passes to custom tag) is the file name.
<externalimg:gen fileName="<%=request.getParameter(\"externalFileName\")%>" />


c) Wrote Main.jsp which uses img tag with src = <<the_JSP_created_in_step b>>
<img src="externalImg.jsp?externalFileName=abc.jpg"/>
<img src="externalImg.jsp?externalFileName=def.jpg"/>

Hoping that this might help somebody.


 
GeeCON Prague 2014
 
subject: wrting multiple JPEG files to a Servlet output stream