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

ssm 往mysql里存图片

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

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>
&lt;properties&gt;
	&lt;java.version&gt;1.8&lt;/java.version&gt;
&lt;/properties&gt;

&lt;dependencies&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
		&lt;artifactId&gt;spring-boot-starter-jdbc&lt;/artifactId&gt;
	&lt;/dependency&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
		&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
	&lt;/dependency&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;org.mybatis.spring.boot&lt;/groupId&gt;
		&lt;artifactId&gt;mybatis-spring-boot-starter&lt;/artifactId&gt;
		&lt;version&gt;1.3.2&lt;/version&gt;
	&lt;/dependency&gt;

	&lt;dependency&gt;
		&lt;groupId&gt;mysql&lt;/groupId&gt;
		&lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
		&lt;scope&gt;runtime&lt;/scope&gt;
	&lt;/dependency&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
		&lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
		&lt;scope&gt;test&lt;/scope&gt;
	&lt;/dependency&gt;

&lt;/dependencies&gt;

&lt;build&gt;
	&lt;plugins&gt;
		&lt;plugin&gt;
			&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
			&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
		&lt;/plugin&gt;
	&lt;/plugins&gt;
&lt;/build&gt;

</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

评论区