SpringBoot上传图片,并进行显示

前提:前面的前端页面需要写成input type=“file”,method=“post”,enctype="multipart/form-data"
springBoot中用 @RequestParam(“file”) MultipartFile file进行获取前端的值

  1. 然后看看servlet页面的操作

    @RequestMapping("add")
    public String addStudent(@RequestParam("sname") String sname,
                             @RequestParam("birthday") String birthday,@RequestParam("address") String address,
                             @RequestParam("file") MultipartFile file,
                             HttpServletRequest request,
                             HttpServletResponse response){
        String sname1 = request.getParameter("sname");
        String birthday1 = request.getParameter("birthday");
//把字符串的date转换成为java.sql.date
        Date date = Date.valueOf(birthday1);
        String address1 = request.getParameter("address");

        String filename = fileUpload(request, response, file);


        Student student=new Student(sname1,date,address1,filename);


        studentService.addStudent(student);

        return "redirect:liststu";
    }

private String fileUpload(HttpServletRequest request, HttpServletResponse response,
                            MultipartFile file1) {

        if(file1.isEmpty()){
            System.out.println("文件为空");
        }
        String fileName = file1.getOriginalFilename();//文件名

        System.out.println(fileName);


//        可以知道fileName.lastIndexOf返回的是一个数值,从后面进行配置,配置的索引从前面进行取值为5
        int i = fileName.lastIndexOf(".");
        System.out.println(i);

        String suffixName = fileName.substring(fileName.lastIndexOf("."));//后缀名
        System.out.println(suffixName);

        String filePath="E:/img/";//上传后的路径

//        通过UUID生成新的文件名

        fileName= UUID.randomUUID()+suffixName;

        File dest=new File(filePath+fileName);
        if(!dest.getParentFile().exists()){
            dest.getParentFile().mkdirs();
        }

        try {
            file1.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
        }

        String filename="/img/"+ fileName;

        System.out.println(filename);


        return filename;


    }

  1. 创建config包,创建MyWebAppConfigurer 类:
package com.zjj.config;


import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


/**
 * 资源映射路径
 */
@Configuration
public class MyWebAppConfigurer implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/img/**").addResourceLocations("file:E:/img/");
    }



}

3.可以用thymleaf进行取值显示

                    <!--                th:src="${stu.photo}

                    和 th:src="${stu.getPhoto()}"都可以
                    "-->
                    <img th:src="${page.photo}" alt="" width="100" height="100">


                </td>
  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

有时间指导毕业设计

觉得写的好的话可以给我打赏

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

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

打赏作者

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

抵扣说明:

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

余额充值