准备工作:
1、数据库:
表中字段:
说明:我这里的数据库名:day8_7,一张user表,三个字段,id,username,avatar,其中username默认是aaa;
2、项目:
从上到下说明:
项目名:uploaddemo
包:com.rzy开头
controller代码:
package com.rzy.controller;
import com.rzy.mapper.UserMapper;
import com.rzy.pojo.User;
import com.rzy.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
//文件上传
@Controller
//跨域请求
@CrossOrigin
public class UploadController {
@Autowired
private UserMapper userMapper;
@RequestMapping("/upload")
public String upload(){
return "upload";
}
@PostMapping("/toUpload")
public String uploadAvatar(@RequestParam("file") MultipartFile file, Model model) {
// 如果图片为空,显示错误信息
if ((file.getOriginalFilename().isEmpty())) {
model.addAttribute("error", "文件不能为空!!!");
return "upload";
} else {
User user = new User();
// 获取图片的名字
String fileName = file.getOriginalFilename();
// 图片存放的文件夹地址
String filePath = "C:\\Users\\2020\\8\\7\\Desktop\\head\\";
// 文件夹地址 + 名字 = 图片的路径
String fileAddress = filePath + fileName;
try {
// 图片上传的工具类
// 参数一:图片的编码格式的字节数组
// 参数二:图片存放的文件夹地址
// 参数三:图片的名字
FileUtil.uploadFile(file.getBytes(), filePath, fileName);
// 把图片路径写入数据库
System.out.println(filePath);
user.setAvatar(fileAddress);
userMapper.uploadInfo(user);
} catch (Exception e) {
e.printStackTrace();
}
model.addAttribute("user", user);
return "index";
}
}
}
这里业务简单,省去service层
mapper代码:
package com.rzy.mapper;
import com.rzy.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
@Repository
@Mapper
public interface UserMapper {
public void uploadInfo(User user);
}
pojo代码:
package com.rzy.pojo;
import lombok.Data;
@Data
public class User {
private int id;
public String username;
private String avatar;
}
说明:这里使用了lombok
util代码:
package com.rzy.util;
import java.io.File;
import java.io.FileOutputStream;
public class FileUtil {
//上传文件的工具类
public static void uploadFile(byte[] file, String filePath, String fileName) throws Exception {
File targetFile = new File(filePath);
if(!targetFile.exists()){
targetFile.mkdirs();
}
FileOutputStream out = new FileOutputStream(filePath+fileName);
out.write(file);
out.flush();
out.close();
}
}
mapper
<?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.rzy.mapper.UserMapper">
<insert id="uploadInfo" parameterType="com.rzy.pojo.User">
INSERT INTO `user` (`avatar`)
VALUES (#{avatar});
</insert>
</mapper>
html代码:
upload.html用于发送页面
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>上传图片区</title>
</head>
<body>
选择图片:
<form enctype="multipart/form-data" method="post" action="/toUpload">
<input type="file" name="file"/>
<input type="submit" value="上传">
</form>
<span th:text="${error}" style="color: red;"></span>
</body>
</html>
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>主页</title>
</head>
<body>
<h1>头像上传成功!!!</h1>
</body>
</html>
application.yml中的配置
# mysql
spring:
datasource:
#MySQL配置
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/day8_7?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC
username: root
password: admin
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.rzy.pojo
server:
port: 9000
部分展示:
数据库:
图片位置: