数据库存储blob图片文件,前端打包下载
数据库图片文件实体类
package com.cmrh.mspserver.pos.dto; import java.io.Serializable; import org.springframework.format.annotation.DateTimeFormat; public class MspPosImage implements Serializable { private static final long serialVersionUID = -4668256147224146458L; /** * 影像类型 */ private String imageType; /** * 影像内容 */ private byte[] imageValue; /** * 影像顺序 */ private Integer sortNum; /** * 创建人 db_column: CREATED_USER */ private java.lang.String createdUser; /** * 创建时间 db_column: CREATED_DATE */ @DateTimeFormat(pattern="yyyy-MM-dd") private java.util.Date createdDate; /** * 更新人 db_column: UPDATED_USER */ private java.lang.String updatedUser; /** * 更新时间 db_column: UPDATED_DATE */ @DateTimeFormat(pattern="yyyy-MM-dd") private java.util.Date updatedDate; /** * 流水号 db_column: PK_SERIAL */ private java.lang.String pkSerial; public String getImageType() { return imageType; } public void setImageType(String imageType) { this.imageType = imageType; } public byte[] getImageValue() { return imageValue; } public void setImageValue(byte[] imageValue) { this.imageValue = imageValue; } public java.lang.String getCreatedUser() { return createdUser; } public void setCreatedUser(java.lang.String createdUser) { this.createdUser = createdUser; } public java.util.Date getCreatedDate() { return createdDate; } public void setCreatedDate(java.util.Date createdDate) { this.createdDate = createdDate; } public java.lang.String getUpdatedUser() { return updatedUser; } public void setUpdatedUser(java.lang.String updatedUser) { this.updatedUser = updatedUser; } public java.util.Date getUpdatedDate() { return updatedDate; } public void setUpdatedDate(java.util.Date updatedDate) { this.updatedDate = updatedDate; } public java.lang.String getPkSerial() { return pkSerial; } public void setPkSerial(java.lang.String pkSerial) { this.pkSerial = pkSerial; } public Integer getSortNum() { return sortNum; } public void setSortNum(Integer sortNum) { this.sortNum = sortNum; } }
下载sql
<!-- 获取影像信息 --> <select id="getMspPosImageList" parameterClass="java.util.List" resultClass="com.cmrh.mspserver.pos.dto.MspPosImage"> SELECT mpi.PK_SERIAL pkSerial,mpi.IMAGE_TYPE imageType,mpi.IMAGE_VALUE imageValue,mpi.SORT_NUM sortNum FROM MSPDATA.MSP_POS_IMAGE mpi WHERE mpi.PK_SERIAL IN <iterate open="(" close=")" conjunction=","> #pkSerial[]# </iterate> ORDER BY mpi.CREATED_DATE,mpi.SORT_NUM </select>
请求处理类
public void downloadImage(HttpServletResponse response, String imageIds) { ZipOutputStream zos = null; try { String nowTimeString = DateUtil.millis2String(System.currentTimeMillis()); //文件的名称 String downloadFilename = nowTimeString + ".zip"; //转换中文否则可能会产生乱码 downloadFilename = URLEncoder.encode(downloadFilename, "UTF-8"); // 指明response的返回对象是文件流 response.setContentType("application/octet-stream"); // 设置在下载框默认显示的文件名 response.setHeader("Content-Disposition", "attachment;filename=" + downloadFilename); zos = new ZipOutputStream(response.getOutputStream()); if (StringUtils.isNotEmpty(imageIds)) { List<String> idList = Arrays.asList(imageIds.split(",")); List<MspPosImage> images = debunkService.getMspPosImageList(idList); if (!CollectionUtils.isEmpty(images)) { for (int i = 0; i < images.size(); i++) { InputStream fis = CommonUtil.byte2Input(images.get(i).getImageValue()); zos.putNextEntry(new ZipEntry("第" + (i+1) + "张截图.jpg")); byte[] buffer = new byte[1024]; int r = 0; while ((r = fis.read(buffer)) != -1) { zos.write(buffer, 0, r); } fis.close(); } } } } catch (IOException e) { e.printStackTrace(); } finally { if (zos != null) { try { zos.flush(); zos.close(); } catch (IOException e) { e.printStackTrace(); } } } }