上传图片的话,我们直接用SpringBoot项目,所以就不用了引入什么依赖了,也不需要写什么文件之类的,只是说你可以自己设置它的上传文件的大小限制。
我分享的呢是我们点击上传过后,我们将它的路径存到数据库中,然后我们他弄个资源映射,在你需要的时候将它显示出来
写一个例子吧,比如注册,我们需要输入账号,密码,已经上传一个头像
UserBean
@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserBean implements Serializable {
//用户id
private int id;
//头像
private String img;
//用户名
private String name;
//用户密码
private String password;
}
service层接口
public interface RegisterService {
//注册
public int register(UserBean userBean);
}
service层实现类
@Service
public class RegisterServiceImpl implements RegisterService {
@Autowired
RegisterMapper mapper;
/**
* 注册
* @param userBean
* @return
*/
@Override
public int register(UserBean userBean) {
return mapper.register(userBean);
}
}
dao层
@Mapper
@Repository
public interface RegisterMapper {
//注册
public int register(UserBean userBean);
}
映射文件.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.novel.mapper.RegisterMapper">
<resultMap id="User" type="UserBean">
<result property="id" column="id"/>
<result property="img" column="img"/>
<result property="name" column="name"/>
<result property="password" column="password"/>
<result property="sex" column="sex"/>
<result property="balance" column="balance"/>
<result property="limits" column="limits"/>
</resultMap>
<!--注册-->
<insert id="register" parameterType="UserBean">
insert into user (id,img,name,password,sex,balance,limits) value (#{id},#{img},#{name},#{password},#{sex},'0','user');
</insert>
</mapper>
controller层
package com.example.novel.controller;/**
* @Auther: http://www.bjsxt.com
* @Date: 2020/9/15
* Description: com.example.novel.controller
* version: 1.0
*/
import com.example.novel.bean.ResultBean;
import com.example.novel.bean.UserBean;
import com.example.novel.service.RegisterService;
import com.example.novel.until.ResultUntil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
/**
*@ClassName RegisterController
*@Description TODO
*@Author lenovo
*@Date 2020/9/15 19:33
*@Version 1.0
**/
@Controller
public class RegisterController {
@Autowired
RegisterService service;
@GetMapping("/topage")
public String topage(){
return "add";
}
/**
* 注册
* @param userBean
* @param file
* @return
*/
@PostMapping("/user/register")
@ResponseBody
public ResultBean registerUser(UserBean userBean, MultipartFile file) {
if (file.isEmpty()) {
return ResultUntil.result(ResultUntil.code500, " ", "请选择文件");
}
String fileName = file.getOriginalFilename();//文件名
String suffixName = fileName.substring(fileName.lastIndexOf("."));//后缀名
String path = "C:\\Users\\lenovo\\Desktop\\pic";//文件存储位置
File dest = new File(path + "\\" + fileName);
if (!dest.getParentFile().exists()) {//判断你输入的文件夹是否存在
//getParentFile().mkdirs的含义
//如果你输入的路径中的文件夹不存在,它会帮你创建,"C:\\Users\\lenovo\\Desktop\\pic\\logo.jpg"
//如果你没有C:\\Users\\lenovo\\Desktop\\pic\\logo.jpg这个文件,它会创建一个文件
//将相当于,你把你选择的文件上传后,它会在另一个文件夹里面创建你输入的文件,
//就相当于是把你上传的所有文件,都放在这个C:\\Users\\lenovo\\Desktop\\pic文件夹里面
dest.getParentFile().mkdirs();
}
try {
file.transferTo(dest);
String url = "\\pic\\" + fileName;
//获取文件的路径
System.out.println(url);
userBean.setImg(url);
String name = userBean.getName();
int count1 = service.register(userBean);
if (count1 > 0) {
return ResultUntil.result(ResultUntil.code200, count1, "注册成功");
}else {
return ResultUntil.result(ResultUntil.code401, "", "注册失败");
}
} catch (IOException e) {
e.printStackTrace();
}
return ResultUntil.result(ResultUntil.code500,"","你这操作我都没搞明白");
}
}
里面具体的代码意思,每段代码的含义我也说得很清楚了,当然也不是说的很对,只是我自己的理解而已 ,勿喷,姿势觉得这样好理解,当然这个对以后项目的一些什么效率这种,就不是很清楚,现在还没考虑到