SpringBoot图片下载

jsp页面:


后台:

@RequestMapping("/showImg")
public void showImg(HttpServletRequest request, HttpServletResponse response){
	 String id= request.getParameter("imgID");
	 byte[] data = null;
	 HardwarePic hard = new HardwarePic();
	 hard = customHardwarePicMapper.selectImageByImgId(id);//根据id查询返回对象
	 data=hard.getPicFile();//将数据库存储二进制图片赋值
	
	 response.setContentType("image/jpeg");//设置输出流内容格式为图片格式
     response.setCharacterEncoding("utf-8");//response的响应的编码方式为utf-8
	try {
                 OutputStream outputStream = response.getOutputStream();//输出流
                  InputStream in = new ByteArrayInputStream(data);//字节输入流
                  int len = 0;
                  byte[] buf = new byte[1024];
                  while ((len = in.read(buf, 0, 1024)) != -1) {
                      outputStream.write(buf, 0, len);
                  }
                  in.close();
                  outputStream.close();
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
}

你可能感兴趣的:(SpringBoot)