栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring Boot + MySQL 图片写入/读取

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring Boot + MySQL 图片写入/读取

目录

一、代码实现

1、创建 SpringBoot 项目

2、导入相关依赖 pom.xml

3、配置 druid 数据库连接池、mybatis

4、创建数据表

5、创建实体类(Pojo层)

6、Mapper层

7、Service层

8、Controller层

9、前端页面

10、目录结构


一、代码实现

1、创建 SpringBoot 项目

2、导入相关依赖 pom.xml


    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.5.0
         
    
    com.ImageUploadAndDownload
    demo
    0.0.1-SNAPSHOT
    demo
    image upload and download demo project for Spring Boot
    
        1.8
    
    
        
        
            org.thymeleaf
            thymeleaf-spring5
        
        
            org.thymeleaf.extras
            thymeleaf-extras-java8time
        

        
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.1.4
        

        
        
            com.alibaba
            druid
            1.2.6
        

        
        
            mysql
            mysql-connector-java
            5.1.24
        

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


3、配置 druid 数据库连接池、mybatis

application.yml

spring:
  datasource:
    username: root
    password: su666
    url: jdbc:mysql://localhost:3306/save_image?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
    driver-class-name: com.mysql.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource

application.properties

server.port=8081
# 设置 mybatis 的查询路径
mybatis.type-aliases-package=com.imageuploadanddownload.demo.Pojo
mybatis.mapper-locations=classpath:mapper/*.xml

4、创建数据表
CREATE TABLE image_test (
    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    name VARCHAr(100) COMMENT '名称',
    photo blob COMMENT '照片'
)
ENGINE=InnoDB
DEFAULT CHARSET=utf8
COLLATE=utf8_general_ci;

5、创建实体类(Pojo层)

ImgObj.java

package com.imageuploadanddownload.demo.Pojo;

import java.util.Arrays;

public class ImgObj {

    private Integer id;
    private String name;
    private byte[] photo;

    public ImgObj() {
    }

    public ImgObj(Integer id, String name, byte[] photo) {
        this.id = id;
        this.name = name;
        this.photo = photo;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public byte[] getPhoto() {
        return photo;
    }

    public void setPhoto(byte[] photo) {
        this.photo = photo;
    }

    @Override
    public String toString() {
        return "ImgObj{" +
                "id=" + id +
                ", name='" + name + ''' +
                ", photo=" + Arrays.toString(photo) +
                '}';
    }
}

6、Mapper层

ImageMapper.java

package com.imageuploadanddownload.demo.Mapper;

import com.imageuploadanddownload.demo.Pojo.ImgObj;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

@Repository
@Mapper
public interface ImageMapper {

    // 通过 mybatis 向 MySQL 中插入一条数据
    public boolean insertToMySQL(String imageName, byte[] imageData);

    // 通过 id 读取数据
    public ImgObj queryById(int id);

}

ImageMapper.xml





    
    
        insert into image_test (name, photo) values (#{imageName}, #{imageData})
    

    
        select * from image_test where id = #{id}
    
    

7、Service层

ImageService.java

package com.imageuploadanddownload.demo.Service;

import com.imageuploadanddownload.demo.Pojo.ImgObj;

public interface ImageService {

    // 向 MySQL 中插入数据
    public boolean insertToMySQL(String imageName, byte[] imageData);

    // 根据 id 查寻数据
    public ImgObj queryById(int id);

}

ImageServiceImpl.java

package com.imageuploadanddownload.demo.Service;

import com.imageuploadanddownload.demo.Mapper.ImageMapper;
import com.imageuploadanddownload.demo.Pojo.ImgObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ImageServiceImpl implements ImageService{

    @Autowired
    private ImageMapper imageMapper;

    @Override
    public boolean insertToMySQL(String imageName, byte[] imageData) {
        return imageMapper.insertToMySQL(imageName, imageData);
    }

    @Override
    public ImgObj queryById(int id) {
        return imageMapper.queryById(id);
    }
}

8、Controller层

ImageController.java

package com.imageuploadanddownload.demo.Controller;

import com.fasterxml.jackson.databind.util.ByteBufferBackedInputStream;
import com.imageuploadanddownload.demo.Pojo.ImgObj;
import com.imageuploadanddownload.demo.Service.ImageServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

@Controller
public class ImageController {

    @Autowired
    private ImageServiceImpl imageService;

    @RequestMapping("/imageTest")
    public String image_test(){
        return "upload_image";
    }

    @PostMapping("/upload")
    public String upload_image(@RequestParam("file") MultipartFile file) throws IOException {
        InputStream inputStream = file.getInputStream();
        byte[] imageData = new byte[(int) file.getSize()];
        inputStream.read(imageData);

        boolean img1 = imageService.insertToMySQL("img1", imageData);
        System.out.println(img1);

        ImgObj imgObj1 = imageService.queryById(1);
        System.out.println(imgObj1.getName());
        FileOutputStream fileOutputStream = new FileOutputStream("D:\images\Photo\read1.jpg");
        InputStream inputStream1 = new ByteArrayInputStream(imgObj1.getPhoto());
        byte[] buffer = new byte[1024];
        int len = 0;
        while((len = inputStream1.read(buffer)) != -1){
            fileOutputStream.write(buffer, 0, len);
        }
        inputStream1.close();
        fileOutputStream.close();

        return "upload_image";
    }
}

9、前端页面

upload_image.html




    
    Title



    

10、目录结构

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/880746.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号