Springboot结合前端上传图片保存到数据库读取

前言

最近在做一个前后端分离系统,也是闲的无聊做个好玩的练练手。就突然想着之前想了一天的问题,前端怎么去发送图片到后端保存(不是专业前端,轻点喷),图片到底是保存在本地还是存在oss上,保存图片的方式又是什么,这些问题想到我头皮发麻。最后,还是花了一下午的时间写出来了个半成品(最后一步没保存到数据库,后期更新)。
首先我们先来看看效果图:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
虽说界面有点丑,但是也还将就吧。
接下来就直接把代码贡献给各位(后期改进点击图片上传)

思路

前端包装一个FormData参数,发送给后端接收,后端定义好实体类和文件,数据交互使用ajax,页面跳转使用Ajax,数据传递使用session。网页展示中,使用FileReader来实时预览。
可能有人问,为什么不用base64编码去保存图片信息,不为什么,因为我不喜欢。。(其实是太长了,每次测试都眼花)

实体类

由于这里是测试,所以我就定义了三个,用户名-username、密码-password、文件名-imgName

@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("user") // 忽略这里,还没连接数据库。。。
public class Info {
    private Integer id;
    private String username;
    private String password;
    private String imgName;
}
前端登录层
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    <script th:src="@{style.js}"></script>
</head>
<body>
<form id="form-data">
    用户名:<input type="text" name="username" placeholder="用户名"><br>
    密码:<input type="password" name="password" placeholder="密码"><br>
    头像:<input type="file" accept="*/*" name="file" id="FileImg" onchange="xmTanUploadImg(this)">
    <img src="" alt="默认头像地址(可以自己填)" id="avarimgs" style="border-radius: 50%" width="200px" height="200px">
    <input type="button" value="注册" id="registerBtn" onclick="reg()">
</form>

</body>
</html>
function xmTanUploadImg(obj) {
    var file = obj.files[0];
    console.log("obj:" + obj);
    console.log("file:" + file);
    console.log("fileName:" + file.name)
    console.log("file.size = " + file.size);
    var reader = new FileReader();
    reader.onload = function (e) {
        console.log(e)// ProgressEvent 对象,里面的target.result就是base64编码
        console.log("成功读取....");
        var img = document.getElementById("avarimgs");
        img.src = e.target.result;
        //或者 img.src = this.result;  //e.target == this
    }
    reader.readAsDataURL(file)
}

function reg() {
    let form = new FormData($("#form-data")[0])
    console.log(form)
    $.ajax({
        //接口地址
        url: 'submit',
        type: 'POST',
        data: form,
        async: false,
        cache: false,
        contentType: false,
        processData: false,
        success:function (data) {
            console.log(data)
            console.log(data.imgName);
            sessionStorage.setItem("img_name",data.imgName);
            window.location='/info'
        }
    });
}
后端控制层
@Controller
@ResponseBody
public class FileController {

    @RequestMapping(value = "/submit", method = RequestMethod.POST)
    public Info submit(MultipartFile file,Info info)
            throws Exception {
        //这里就可以获取里面的上传过来的数据了
        //做一些存库操作,以及返回的数据
        String filename = file.getOriginalFilename();
        System.out.println(filename);
        String filePath = System.getProperty("user.dir")+"\\src\\main\\resources\\static\\img";
        if (!new File(filePath).exists()){
            new File(filePath).mkdirs();
        }
        File dest = new File(filePath + File.separator + info.getUsername()+"_"+filename);
        try {
            file.transferTo(dest);
        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println(info);
        info.setImgName(info.getUsername()+"_"+filename);
        return info;
    }
}

我在后端由于要保存用户头像,所以命名为了防止冲突,直接暴力的使用用户名加上图片文件名进行保存,这样可以很大程度上保证用户的头像唯一(或者在登录的时候直接加一个判断,先保证用户名在数据库中唯一,因为我自己在设计的时候用的主要是一个别名而不是用户名,所以我就没有加)
保存目录不能直接写死路径,假如你的项目需要部署在服务器上,那么就会直接报找不到文件的错误。所以我借用了MyBatis-plus的写法,用了当前系统目录;

踩的坑
在测试的时候,由于第一次上传上去之后图片不能立即显示,需要重启服务器。这个坑折磨了我半个小时,最后看了一眼网上大佬们的做法,加一个资源映射就完美的解决了。确实,这个很nice。所以我也给大家:

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        System.out.println("图片配置生效");
        String filePath = System.getProperty("user.dir")+"\\src\\main\\resources\\static\\img\\";
        System.out.println(filePath);
        registry.addResourceHandler("/img/**").addResourceLocations("file:"+filePath);
    }
}

依赖问题
很简答,我没用什么很花里胡哨的操作。如果你是直接复制这个demo的话,只需要加一个thymeleaf就行,配置文件不用谢任何东西。

这个也是缠了我好几天的问题了,我i也终于解决了,啊,爽
在这里插入图片描述
最后给一张结构图,仅供参考:
在这里插入图片描述
如果对你有帮助,请尽情留下你的评论和点赞,我不会嫌弃的哦。
如果哪儿有值得改进的地方,也请告诉我,我会马上加以改进;

谢谢你这么忙还来看我的文章,靓仔!

  • 23
    点赞
  • 94
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
在Spring Boot中,可以使用Base64编码将本地图片保存数据库。以下是一个示例代码: ```java import java.io.File; import java.nio.file.Files; import java.util.Base64; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Lob; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication public class ImageToDatabaseApplication { public static void main(String[] args) { SpringApplication.run(ImageToDatabaseApplication.class, args); } } @RestController class ImageController { private final ImageRepository imageRepository; public ImageController(ImageRepository imageRepository) { this.imageRepository = imageRepository; } @PostMapping("/upload") public String uploadImage(@RequestParam("file") File file) { try { // 读取图片文件并进行Base64编码 byte[] fileContent = Files.readAllBytes(file.toPath()); String base64Image = Base64.getEncoder().encodeToString(fileContent); // 创建实体对象并保存数据库 Image image = new Image(); image.setData(base64Image); imageRepository.save(image); return "图片保存成功!"; } catch (Exception e) { e.printStackTrace(); } return "图片保存失败!"; } } @Entity class Image { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Lob private String data; // getters and setters public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getData() { return data; } public void setData(String data) { this.data = data; } } interface ImageRepository extends JpaRepository<Image, Long> { } ``` 在这个示例中,我们创建了一个Spring Boot应用程序,使用`ImageController`来处理上传图片的请求。在`uploadImage`方法中,我们首先将文件内容读取为字节数组,然后使用Base64编码转换为字符串。接下来,我们创建一个`Image`实体对象,将Base64编码后的图片数据设置到`data`属性中,并通过`ImageRepository`将实体保存数据库中。 请确保在`application.properties`文件中配置正确的数据库连接信息,并在pom.xml文件中添加适当的依赖项(例如Spring Data JPA和MySQL驱动程序),以便应用程序能够正常工作。 这是一个简单的示例,你可以根据实际需求进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鱼小洲

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值