当前位置: 首页 > news >正文

网站建设教程科普广告公司推广方案

网站建设教程科普,广告公司推广方案,网站百度突然不收录,mac 怎么下wordpress前言 如果你的前后端分离项目采用SpringBoot3Vue3Element Plus,且在没有OSS(对象存储)的情况下,使用mysql读写图片(可能不限于图片,待测试)。 耗时三天,在踩了无数雷后&#xff0c…

前言

如果你的前后端分离项目采用SpringBoot3+Vue3+Element Plus,且在没有OSS(对象存储)的情况下,使用mysql读写图片(可能不限于图片,待测试)。

耗时三天,在踩了无数雷后,终于完成本功能。为你呈上。

本文完成功能:

  1. 前端采用Element发起上传图片请求,后端接收并将其存储到mysql。
  2. 后端相应图片base64数据,前端接收并渲染到页面。

1.前端上传到数据库

1.1前端上传图片

<el-form-item label="宠物照片" prop="pictureId">
<el-upload v-model="form.pictureId" :auto-upload="false" :action="''" :show-file-list="true" :on-change="handleAvatarChangeIcon"><el-button type="primary">选取文件</el-button>
</el-upload>
</el-form-item>

参数:

:auto-upload 是否自动上传

:action 自动上传的请求路径

:show-file-list 显示已上传的图片列表

:on-change 选中文件触发的change事件

自动上传与否都不影响,这里主要是判断一下图片的大小、后缀名。如下:

const handleAvatarChangeIcon = (file) => {// 限制文件后缀名const isJPG = file.raw.type === 'image/jpeg'const isPNG = file.raw.type === 'image/png'// 限制上传文件的大小const isLt5M = file.raw.size / 1024 / 1024 < 5if (!isPNG && !isJPG) {ElMessage.error('图片只能是 JPG/PNG 格式')return false} else if (!isLt5M) {ElMessage.error('图片应在5MB以内')return false} else {// 发起请求let param = new FormData();// 文件为form data格式param.append("file", file.raw);post('/api/file/upload', param, (res) => {ElMessage.success('上传成功');// 返回值为图片idform.pictureId = res})}
}

1.2后端接收并保存数据库

controller

@RestController
@RequestMapping("/api/file")
public class FileController {@Resourceprivate FileService fileService;@PostMapping("/upload")public RestBean<String> upload(@RequestParam MultipartFile file) {Integer res = fileService.upload(file);return RestBean.success(String.valueOf(res));}
}

serviceImpl

@Service
public class FileServiceImpl implements FileService {@Resourceprivate FileMapper fileMapper;/*** 文件上传到数据库*/@Overridepublic Integer upload(MultipartFile file) throws IOException {// 获取文件原始名String originalFilename = file.getOriginalFilename();// 获取文件后缀名String endName = "png";if (originalFilename != null) {endName = originalFilename.substring(originalFilename.lastIndexOf("."));}// 拼接文件名String filename = UUID.randomUUID() + endName;Integer pictureId;// 创建图片对象byte[] fileBytes = file.getBytes();Picture picture = new Picture();picture.setName(filename);picture.setPicData(fileBytes);// 上传数据库fileMapper.upload(picture);pictureId = picture.getId();// 返回图片idreturn pictureId;}
}

mapper.xml

<mapper namespace="com.ycb.mapper.FileMapper"><insert id="upload" useGeneratedKeys="true" keyProperty="id">insert into `pet-adoption`.picture(name, pic_data)value (#{name}, #{picData})</insert>
</mapper>

数据库设计

在这里插入图片描述

2.前端从数据库获取图片并渲染

2.1后端从数据库中获取

entity

public class PetAndBulVO {/*** 照片*/private byte[] picData;
}

controller

如果是一个图片数据直接封装到实体类,很多数据就封装成集合

@RequestMapping("/api/pet")
public class PetController {@Resourceprivate PetService petService;@GetMapping("/getAllPB")public RestBean<List<PetAndBulVO>> getAll() {List<PetAndBulVO> pets = petService.getAll();return RestBean.success(pets);}
}

serviceImpl

@Service
public class PetServiceImpl implements PetService {@Resourceprivate PetMapper petMapper;@Overridepublic List<PetAndBulVO> getAll() {return petMapper.getAll();}
}

mapper.xml

<mapper namespace="com.ycb.mapper.PetMapper"><!--  一定要映射结果集  --><resultMap type="com.ycb.entity.vo.response.PetAndBulVO" id="petAndBulVO"><id column="pic_data" property="picData" javaType="byte[]" jdbcType="BLOB" typeHandler="org.apache.ibatis.type.BlobTypeHandler"/></resultMap><select id="getAll" resultMap="petAndBulVO">select *from `pet-adoption`.pet petjoin `pet-adoption`.picture p on p.id = pet.picture_id</select>

后端返回的图片数据如下:

在这里插入图片描述

2.2前端接收数据并渲染

后端传来的数据是 base64 形式的,需要解码

// 解码
const base64ToUrl = (base64) => {return 'data:image/png;base64,' + base64
}// 获取数据
get('/api/pet/getAllPB', (data) => {for (let i = 0; i < data.length; i++) {data[i].picData = base64ToUrl(data[i].picData)}pBList.value = data
}, (err) => {ElMessage.error(err)
})

解码后的图片数据如下:

在这里插入图片描述

渲染是大坑!一定要 v-bind: 绑定 src

// v-for循环获取picData, v-for="(pb) in pBList"
<el-image v-bind:src="pb.picData"/>

《林克可爱图》
在这里插入图片描述

写在最后

虽然可以实现仅用mysql就能完成图片读写,但其性能堪忧。

很难,但贵在坚持。

http://www.ds6.com.cn/news/38101.html

相关文章:

  • 网站皮肤样板windows优化大师自动安装
  • 嘉兴企业网站推广方法百度一下官方网页版
  • 房地产销售提成seo公司网站推广
  • 引航博景网站做的很好吗武汉百度推广入口
  • 可视化网站开发系统介绍外贸订单一般在哪个平台接
  • 做网站 360的好不好网上销售推广方案
  • 网站目录 index.html青岛网站建设维护
  • wordpress自带站内搜索功能域名网站
  • 寮步网站建设高性能seo是什么意思知乎
  • 网站开发啊广告视频
  • 想做cpa 没有网站怎么做saascrm国内免费pdf
  • 做网站按什么收费多少钱域名被墙查询检测
  • 做网站必须要买空间搜索引擎营销的实现方法有哪些
  • 公司网站关键词优化怎么做微信小程序排名关键词优化
  • 官方网站下载抖音海外网络推广平台
  • 小程序需不需要服务器上海网站排名seo公司
  • 做网站婚介简历怎么写品牌网络营销策划
  • 做后期的网站有哪些网站权重一般有几个等级
  • 如何进行网站备案数字营销是干啥的
  • 定制网站建设服务商免费广告推广
  • 如何管理wordpress网站模板下载nba最新交易动态
  • 科普类网站怎么做线上推广平台
  • 河北提供网站制作公司电话餐饮营销策划与运营
  • 做网站要用到哪些技术重庆百度推广
  • 番禺网站建设a2345抖音seo排名软件
  • WordPress网站转HTPPS端点seo博客
  • 济南网站优化百度网盘资源共享
  • 网站建设有什么服务南京seo排名优化公司
  • 友汇网网站建设my63777免费域名查询
  • 做企业网站 排名百度网址大全免费下载