在HttpServletResponse Spring Boot中下载多个映像的java方法
我必须实现下载功能,以便在触发时下载多个图像。到目前为止,我用一个图像实现了它。但不适用于多个。有办法吗
图像源来自数据库,通过blob转换为图像
以下是当前代码
public void downloadImages(HttpServletResponse response) throws IOException {
List<ScreenUserEntity> users = repo.findAllByImageIsNotNull();
byte[] decodedBytes = users.get(0).getImage();
ByteArrayInputStream bis = new ByteArrayInputStream(decodedBytes);
BufferedImage image = ImageIO.read(bis);
response.addHeader("Content-disposition", "attachment; filename=" + "name.jpg");
response.setContentType("image/jpeg");
File outputfile = new File("name.jpg");
ImageIO.write(image, "jpg", outputfile);
FileInputStream in = new FileInputStream(outputfile);
OutputStream out = response.getOutputStream();
byte[] buf = new byte[1024];
int count = 0;
while ((count = in.read(buf)) >= 0) {
out.write(buf, 0, count);
}
out.close();
in.close();
}
谢谢你的帮助
共 (0) 个答案