views:

148

answers:

2

Hi,

I am trying to append 2 images (as byte[] ) in GoogleAppEngine Java and then ask HttpResponseServlet to display it. However, it does not seem like the second image is being appended.

Is there anything wrong with the snippet below?

...

resp.setContentType("image/jpeg");
byte[] allimages = new byte[1000000]; //1000kB in size
int destPos = 0;
for(Blob savedChart : savedCharts) {
  byte[] imageData = savedChart.getBytes(); //imageData is 150k in size
  System.arraycopy(imageData, 0, allimages, destPos, imageData.length);
  destPos += imageData.length;
}

resp.getOutputStream().write(allimages);
return;

Regards

+1  A: 

Seem that you have completely wrong concept about image file format and how they works in HTML.

In short, the arrays are copied very well without problem. But it is not the way how image works.

You will need to do AWT to combine images in Java

Dennis Cheung
+1  A: 

I would expect the browser/client to issue 2 separate requests for these images, and the servlet would supply each in turn.

You can't just concatenate images together (like most other data structures). What about headers etc.? At the moment you're providing 2 jpegs butted aainst one another and a browser won't handle that at all.

If you really require 2 images together, you're going to need some image processing library to do this for you (or perhaps, as noted, AWT). Check out the ImageIO library.

Brian Agnew