Converting Multiple Images to Byte Array

1k Views Asked by At

i trying to give responce as multiple images byte array to another application.

I get the vehicle images list from database and convert to byte array.

ByteArrayInputStream instream = null;
ServletOutputStream out = null ;
for(Vehicle vehicle : vehicleList)
{
   byte[] imageInByte = null;
   VehicleImage vehicleImage=VehicleImagePeer.doSelectFirst(c.add(VehicleImagePeer.VEHICLE_ID, getId()));
   c.clear();
   String path = TaxiApp.getConfigValue("IMAGE.PATH")+vehicleImage.getPath();
   System.out.print(path);
   BufferedImage originalImage = ImageIO.read(new File(path));
   ImageIO.write(originalImage, "png", baos);
   imageInByte = baos.toByteArray();
   baos.close();

using this code i convert all the images to byte Array.

As Now i need to give response. i used this code to response

out = response.getOutputStream();
response.setContentLength(imageInByte.length);
response.setCharacterEncoding(Charset.forName("UTF-8").name());
int bufferSize = response.getBufferSize();
byte[] buffer = new byte[bufferSize];
instream = new ByteArrayInputStream(imageInByte);
TaxiApp.logprintf("Total page size in bytes: %s and default response bufferSize in bytes: %s%n", new Object[] 
{
   Integer.valueOf(imageInByte.length), Integer.valueOf(bufferSize) 
});
int length = 0;
while ((length = instream.read(buffer, 0, bufferSize)) != -1)
{
out.write(buffer, 0, length);
out.flush();
}

Here is a problem for me. The response can send the first image byte array only.but i need to send all the image byte Array...please sort out this problem.

0

There are 0 best solutions below