将多个图像写入 ServletOutputStream?
Writing multiple images to ServletOuputStream?
我有一个要求,我从 Web 服务(例如:列表)获取多个图像并且必须将这些图像写入 ServletOutputStream。
当我单击 'VIEW' link 时,它会调用 servlet,而该 servlet 会调用 web 服务并将多个图像作为列表接收。
现在我正在尝试将这些图像写入无法正常工作的 ServletOutputStream..
正在尝试以 zip 格式发送图像
response.setContentType("application/zip");
OutputStream os = null;
BufferedOutputStream bos = null;
ZipOutputStream zos = null;
try{
os = resp.getOutputStream();
bos = new BufferedOutputStream(os);
zos = new ZipOutputStream(bos);
zos.setLevel(ZipOutputStream.STORED);
sendMultipleFiles(zos, annotContent,"display");
}catch (IOException e) {
resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
} finally {
if (zos != null) {
zos.finish();
zos.flush();
}
bos.close();
os.close();
}
private void sendMultipleFiles(ZipOutputStream zos, Collection<byte[]> filesToSend, String name) throws IOException {
myLogger.info("sendMultipleFiles is invoked..");
for(byte[] f: filesToSend) {
InputStream inStream = null;
ZipEntry ze = null;
try {
inStream = new ByteArrayInputStream(f);
ze = new ZipEntry(name + "-archived");
ze.setComment("Dummy file");
zos.putNextEntry(ze);
int readByte = 0;
while((readByte = inStream.read()) != -1)
{
zos.write(readByte);
}
} catch (IOException e) {
System.out.println("Cannot find " );
} finally {
if (ze != null) {
zos.closeEntry();
}
inStream.close();
}
}
以上代码无效..任何建议将不胜感激..
您的解决方案将取决于您提供的内容类型 - 如果您提供 HTML 响应,则可以通过将图像存储在磁盘上 (outside the context root and writing a image display endpoint) or if the images are small - sending the bytes out as base64 encoding in the image tag directly.
如果您的回复类型不是 HTML - 这里有一些选项 - 您可以 return ZIP file.
使用带有 MultiPart MIME Extension 的 servlet 的不太标准的方式。
编辑:
根据评论,mime 类型是 JPEG - 一种选择是将 JPEG 组合成一个更大的(不能发送多个)。组合二进制文件的字节 - JPEG 有它自己的压缩格式以及页眉、页脚和 EXIF 信息 - 您可能需要专门的 API 将它们组合成一个。 Here 是我遇到的一个。如果合并 JPG 不是一种选择 - 您试图实现的目标无法一次完成。
我有一个要求,我从 Web 服务(例如:列表)获取多个图像并且必须将这些图像写入 ServletOutputStream。
当我单击 'VIEW' link 时,它会调用 servlet,而该 servlet 会调用 web 服务并将多个图像作为列表接收。
现在我正在尝试将这些图像写入无法正常工作的 ServletOutputStream..
正在尝试以 zip 格式发送图像
response.setContentType("application/zip");
OutputStream os = null;
BufferedOutputStream bos = null;
ZipOutputStream zos = null;
try{
os = resp.getOutputStream();
bos = new BufferedOutputStream(os);
zos = new ZipOutputStream(bos);
zos.setLevel(ZipOutputStream.STORED);
sendMultipleFiles(zos, annotContent,"display");
}catch (IOException e) {
resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
} finally {
if (zos != null) {
zos.finish();
zos.flush();
}
bos.close();
os.close();
}
private void sendMultipleFiles(ZipOutputStream zos, Collection<byte[]> filesToSend, String name) throws IOException {
myLogger.info("sendMultipleFiles is invoked..");
for(byte[] f: filesToSend) {
InputStream inStream = null;
ZipEntry ze = null;
try {
inStream = new ByteArrayInputStream(f);
ze = new ZipEntry(name + "-archived");
ze.setComment("Dummy file");
zos.putNextEntry(ze);
int readByte = 0;
while((readByte = inStream.read()) != -1)
{
zos.write(readByte);
}
} catch (IOException e) {
System.out.println("Cannot find " );
} finally {
if (ze != null) {
zos.closeEntry();
}
inStream.close();
}
}
以上代码无效..任何建议将不胜感激..
您的解决方案将取决于您提供的内容类型 - 如果您提供 HTML 响应,则可以通过将图像存储在磁盘上 (outside the context root and writing a image display endpoint) or if the images are small - sending the bytes out as base64 encoding in the image tag directly.
如果您的回复类型不是 HTML - 这里有一些选项 - 您可以 return ZIP file.
使用带有 MultiPart MIME Extension 的 servlet 的不太标准的方式。
编辑: 根据评论,mime 类型是 JPEG - 一种选择是将 JPEG 组合成一个更大的(不能发送多个)。组合二进制文件的字节 - JPEG 有它自己的压缩格式以及页眉、页脚和 EXIF 信息 - 您可能需要专门的 API 将它们组合成一个。 Here 是我遇到的一个。如果合并 JPG 不是一种选择 - 您试图实现的目标无法一次完成。