springboot 图片文件的上传并显示于页面

springboot中图片文件的上传并显示在页面上

学生页面

在数据库中用picUrl来放图片

<div class="table-responsive ">
                <table class="table table-striped table-sm">
                    <thead>
                    <tr>
                        <th>序号</th>
                        <th>学号</th>
                        <th>姓名</th>
                        <th>年龄</th>
                        <th>照片</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr th:each="stu:${stu}">
                        <td th:text="${stuStat.index+1}"></td>
                        <td th:text="${stu.cno} "></td>
                        <td th:text="${stu.name}"></td>
                        <td th:text="${stu.age}"></td>
                        <td><img th:src="@{${stu.picUrl}}" style="width: 250px; height: 125px"></td>
                        <td><a th:href="@{/update(cno=${stu.cno}) }" class="btn bg-success">更新</a>&nbsp;&nbsp;&nbsp;<a th:href="@{/delete(cno=${stu.cno})}" class="btn btn-danger">删除</a></td>
                    </tr>
                    </tbody>
                </table>
            </div>

更新页面

这里我们只需要做图片的更新就行

<form class="form-horizontal" style="margin-top: 40px" th:action="@{/update}" th:method="post" enctype="multipart/form-data">
                <div class="form-group">
                    <label for="cno" class="col-sm-2 control-label">学号</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="cno" name="cno" th:value="${stu.cno}" placeholder="">
                    </div>
                </div>
                <div class="form-group">
                    <label for="name" class="col-sm-2 control-label">姓名</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="name" name="name" th:value="${stu.name}" placeholder="">
                    </div>
                </div>
                <div class="form-group">
                    <label for="age" class="col-sm-2 control-label">年龄</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="age" name="age" th:value="${stu.age}" placeholder="">
                    </div>
                </div>
                <div class="form-group">
                    <label for="picture" class="col-sm-2 control-label">照片</label>
                    <div class="col-sm-10">
                        <input type="file" class="form-control" id="picture" name="picture" placeholder="">
                        <img th:src="@{${stu.picUrl}}" alt="">
                    </div>
                </div>

                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <button type="submit" class="btn btn-default bg-info">修改学生</button>
                    </div>
                </div>
            </form>

控制器

 @PostMapping("/update")
    public String receiveStudent(Student student, @RequestParam("picture") MultipartFile img){
        if (!img.isEmpty()) {
            String filename = img.getOriginalFilename();
            try {
                String newFilename= UUID.randomUUID()+"_"+filename;
                img.transferTo(new File("D:\\大二上\\SpringBoot\\springboot01_10\\src\\main\\resources\\static\\upload\\" + newFilename));
                student.setPicUrl("/upload/"+newFilename);
            }catch (IOException e){
                e.printStackTrace();
            }
        }
        int flag = stuService.updateStu(student);
        return "redirect:/student";
    }

效果显示

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

编程初学者01

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

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

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

打赏作者

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

抵扣说明:

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

余额充值