• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

wrting multiple JPEG files to a Servlet output stream

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Madhu Bompaly
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Madhu Bompaly
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


 
reply
    Bookmark Topic Watch Topic
  • New Topic