将多个图像转换为字节数组

时间:2013-07-24 13:20:03

标签: java servlets apache-torque

我试图将多个图像字节数组作为响应给另一个应用程序。

我从数据库中获取车辆图像列表并转换为字节数组。

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();

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();

使用此代码我将所有图像转换为字节数组。

现在我需要回应。 我用这段代码回复了

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();
}

这对我来说是一个问题。 响应只能发送第一个图像字节数组。但是我需要发送所有图像字节数组...请解决这个问题。

0 个答案:

没有答案