基于SpringBoot与Mysql的图片数据上传与下载的处理

您所在的位置:网站首页 varchar存储图片 基于SpringBoot与Mysql的图片数据上传与下载的处理

基于SpringBoot与Mysql的图片数据上传与下载的处理

2024-07-16 06:01:25| 来源: 网络整理| 查看: 265

Sql表结构

create table img( uuid varchar(40) unique primary key , img_file blob ) comment '图片库';

ImgPojo类

用于传递与接收img实体

package com.Itcase.Pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.io.BufferedInputStream; import java.io.InputStream; @Data @NoArgsConstructor @AllArgsConstructor public class img { private String uuid; private InputStream img; }

Controller层

package com.Itcase.Controller; import com.Itcase.Pojo.Result; import com.Itcase.Pojo.img; import com.Itcase.Server.ImgServer; import com.Itcase.Util.AliOSSUtils; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.IOException; @Slf4j @RestController @RequestMapping(value = "/upload" ) public class UpLoad { @Autowired AliOSSUtils au; @Autowired ImgServer is; // 阿里云对象存储 @PostMapping public Result upLoadImg(MultipartFile image) throws Exception{ String imageName = au.upload(image); return Result.success(imageName); } // Mysql上传 @PostMapping("/addImg") public Result addImg(MultipartFile image) throws IOException { // 返回上传后的图片的访问链接 String url = is.addImg(image); return Result.success(url); } // Mysql下载 @GetMapping("/{imgName}") public void getImg(@PathVariable String imgName, HttpServletResponse response) throws IOException { response.setContentType("application/octet-stream"); img img = is.getImg(imgName); // 使用缓冲IO流对数据库Blob数据的处理 BufferedInputStream bis = new BufferedInputStream(img.getImg()); BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream()); int len=-1; byte[] bytes = new byte[1024]; while ((len=bis.read(bytes))!=-1) { bos.write(bytes); } img.getImg().close(); bis.close(); bos.close(); } }

Server层

package com.Itcase.Server.Impl; import com.Itcase.Dao.EmpMapper; import com.Itcase.Dao.ImgMapper; import com.Itcase.Pojo.img; import com.Itcase.Server.ImgServer; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import java.io.BufferedInputStream; import java.io.IOException; import java.util.UUID; @Slf4j @Service public class ImgServerImpl implements ImgServer { @Autowired ImgMapper im; @Override public String addImg(MultipartFile image) throws IOException { //本机项目对应图片传输的get访问路径 String url="http://localhost:8086/upload/"; String fileFormot = image.getOriginalFilename().toString().substring(image.getOriginalFilename().lastIndexOf(".")); String imageUuid = UUID.randomUUID().toString()+fileFormot; // BufferedInputStream bis = new BufferedInputStream(image.getInputStream()); // log.info("uuid:{}",imageUuid); img img = new img(imageUuid, image.getInputStream()); im.addImg(img); log.info("数据库uuid:{}",img.getUuid()); url += url = img.getUuid(); return url; } @Override public img getImg(String imgName) { img img = im.selectImg(imgName); return img; } }

Dao层

package com.Itcase.Dao; import com.Itcase.Pojo.img; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Options; import org.apache.ibatis.annotations.Select; @Mapper public interface ImgMapper { @Options(keyProperty = "uuid",useGeneratedKeys = true) @Insert("insert into img values (#{uuid},#{img})") void addImg(img im); @Select("select * from img where uuid=#{imgName}") img selectImg(String imgName); }

大概实现思路就是利用input流将前端上传的图片上传至数据库,读取的话就是利用output流,使用HttpServletResponse对二进制数据进行返回。使用的MySQL,对于二进制文件读取的都是比较慢,做完后发现还不如直接上传到本地文件里使用FileInput流进行回显,这样就没有数据库的读的慢的烦恼了。

这个实现是二进制文件数据基本都可以存储与下载不仅仅针对于img数据,本例中是使用img最为参考,当然,在上面也是用了阿里云的对象存储,有钱的话可以去哪里开个资源包,也不会,相对来说也很方便,传输速度也快。



【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭