springboot+mybatis-plus+freemarker上传图片,将文件名保存到MySQL数据库

picture实体类,@TableId(type = IdType.AUTO)设置id自增长策略


import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.enums.IdType;

public class Picture {

	@TableId(type = IdType.AUTO)
	private Integer id;
	private String picture;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getPicture() {
		return picture;
	}
	public void setPicture(String picture) {
		this.picture = picture;
	}
	
	
}

配置类,配置本地资源的映射地址


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("/images/**").addResourceLocations("D:\\File\\images");
    }
}

控制类

@Controller
public class UploadController {
	@Autowired
	private PictureMapper pictureMapper;
	
    @RequestMapping("uploadPage")
    public String uploadPage() {
        return "uploadPage";  
    }

    @PostMapping("upload")
    public String uplaod(HttpServletRequest req, @RequestParam("file") MultipartFile file, Model m) {
    	Picture picture = new Picture();
    	try {
            //根据创建时间对文件进行重命名
            String fileName = System.currentTimeMillis() + file.getOriginalFilename();
            //上传文件存储的位置
            String destFileName = "D:\\File\\images" + File.separator + fileName;
            //防止改文件夹不存在,创建一个新文件夹
            File destFile = new File(destFileName);
            destFile.getParentFile().mkdirs();
            //将文件存储到该位置
            file.transferTo(destFile);
            //传递文件
            m.addAttribute("fileName", fileName);
            //将文件名存储到数据库中,以便查询调用
            picture.setPicture(fileName);
            pictureMapper.insert(picture);
            System.out.println(fileName);
            
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return "上传失败," + e.getMessage();
        } catch (IOException e) {
            e.printStackTrace();
            return "上传失败," + e.getMessage();
        }

        return "showImg";
    }
    
    //测试从数据库中查询
    @RequestMapping("selectImg")
    public String selectImg(HttpServletRequest request) {
    	List<Picture> pictures = pictureMapper.selectList(null);
    	request.setAttribute("pictures", pictures);
    	return "show4";
    }
}

上传图片页面

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加员工</title>
</head>
<body>
	<form action="upload" method="post" enctype="multipart/form-data">
        选择图片:<input type="file" name="file" accept="image/*" /> <br>
        	<input type="submit" value="立刻上传">
    </form>
</body>
</html>

显示图片页面

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>上传图片</title>
</head>
<body>
    <img src="/images/${fileName}">
</body>
</html>

查询图片显示页面

<table>
		<tr>
			<th>ID</th>
			<th>图片</th>
		</tr>
		<#--width="100" height="100"用来设置图片大小  -->
		<#list pictures as user>
		<tr>
			<td>${user.id}</td>
			<td><img src="/images/${user.picture}" width="100" height="100"></td>
			</tr>
		</#list>
</table>
	
	

运行项目,先输入以下地址
http://localhost:8080/uploadPage
在这里插入图片描述
选择图片上传,上传成功后显示该图像
在这里插入图片描述
输入以下地址,测试能否从数据库中调用
http://localhost:8080/selectImg

在这里插入图片描述

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值