侧边栏壁纸
  • 累计撰写 793 篇文章
  • 累计创建 1 个标签
  • 累计收到 1 条评论
标签搜索

目 录CONTENT

文章目录

ssm 往mysql里存图片

Dettan
2021-04-10 / 0 评论 / 0 点赞 / 199 阅读 / 903 字
温馨提示:
本文最后更新于 2022-07-23,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

1、创建springboot项目
2、引入依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.9.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.llg</groupId>
	<artifactId>mybatis</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>mybatis</name>
	<description>Demo project for Spring Boot</description>
 
	<properties>
		<java.version>1.8</java.version>
	</properties>
 
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.3.2</version>
		</dependency>
 
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
 
	</dependencies>
 
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
 
</project>
3、实体类
package com.llg.mybatis.entity;
 
import lombok.Data;
 
@Data
public class Users {
    private Integer id;
    private String username;
    private String password;
    private byte[] pic;
}
4、前端页面
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form action="/upload" enctype="multipart/form-data" method="post">
        <input type="file" name="file"><br>
        <input type="submit" value="提交">
    </form>
</body>
</html>
5、上传方法
@PostMapping("/upload")
public String savePic(@RequestParam("file") MultipartFile file){
	if (file.isEmpty()) {
		return "上传失败,请选择文件";
	}
	try {
		InputStream is = file.getInputStream();
		Users u = new Users();
		u.setUsername("哈哈");
		u.setUsername("123456");
		byte[] pic = new byte[(int)file.getSize()];
		is.read(pic);
		u.setPic(pic);
		userService.addUser(u);
		return "上传成功";
	} catch (IOException e) {
		e.printStackTrace();
	}
	return "success";
}
6、mapper sql语句
<insert id="addUser">
    insert into users (username, password, pic) values (#{username}, #{password}, #{pic})
</insert>
7、启动程序-打开页面-上传图片
成功
8、前端获取数据库的图片并展示
后端代码
@GetMapping(value="/getPhoto")
public void getPhotoById(final HttpServletResponse response) throws IOException {
	Users users = userService.getLastUser();
	byte[] data = users.getPic();
	response.setContentType("image/jpeg");
	response.setCharacterEncoding("UTF-8");
	OutputStream outputSream = response.getOutputStream();
	InputStream in = new ByteArrayInputStream(data);
	int len = 0;
	byte[] buf = new byte[1024];
	while ((len = in.read(buf, 0, 1024)) != -1) {
		outputSream.write(buf, 0, len);
	}
	outputSream.close();
}
sql语句
<select id="getLastUser" resultType="com.llg.mybatis.entity.Users">
    select * from users order by id desc limit 1
</select>

<resultMap type="com.entity.ToufangfanganEntity" id="picMap">
    <result property="pic" column="pic" />
</resultMap>

<select id="getRandPic" resultType="byte[]">
    SELECT pic
    FROM `toufangfangan`
    where pic is not null
    ORDER BY RAND() LIMIT 0,1;
</select>

List<ToufangfanganEntity> getPic(String id);

List<Byte[]> getRandPic();
结果


0

评论区