前端代码为:
<form action="/userSave" method="post" enctype ="multipart/form-data">
用户名:<input type="text" name="userName" autofocus required placeholder="NAME">
密码:<input type="password" name="userPwd" autofocus required placeholder="PASSWORD">
邮箱:<input type="email" name="userEmail" autofocus required placeholder="Email,Address">
选择头像:<input type="file" name="file" id="file">
<input type="submit" value="注册" style="color: white; background: black;font-size: 20px">
</form>
后端代码:
//上传保存课程图片,上传课程
@RequestMapping(value = "/CourseSave",method = RequestMethod.POST)
//上传的feil参数 传到这里
public String addCourse(@RequestParam(value = "file",required = false) MultipartFile file
, CourseTable table) throws IOException {
//图片上传成功后,将图片的地址写到数据库D://images
String filePath="D://images";
File pathFile=new File(filePath);
if(pathFile.exists()){
pathFile.mkdirs();
}
//获得原始图片的拓展名
String originalFileName=file.getOriginalFilename();
//新的文件名字
String newfileName= UUID.randomUUID()+originalFileName;
//封装上传文件位置的全路径
File targetFile=new File(filePath,newfileName);
//把本地文件上传到封装上传文件位置的全路径
file.transferTo(targetFile);
//设置图片路径,为字符串类型
table.setCoursePic(newfileName);
Service.insertCourse(table);
return "index";
}
配置文件中需要更改的内容为:
# 数据库连接驱动,4大金刚
datasource:
url: jdbc:mysql://127.0.0.1:3306/bolg?characterEncoding=UTF-8
username: root
password: root123
driver-class-name: com.mysql.cj.jdbc.Driver
#mapper xml 文件扫描
mybatis:
typeAliasesPackage: com.bolgs.pojo
mapperLocations: classpath:mapper/*.xml
# 分页依赖
hepagehelper:
lperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
# mvc 视图解析器
mvc:
view:
prefix: /templates/*
suffix: .
static-location: assets/**
# web 网页静态资源加载路径
web:
resources:
static-locations: file:d:/images,classpath:/META-INF/resources/,classpath:/resources/,classpath:static/,classpath:/public/
server:
port: 8080
servlet:
session:
timeout: 10s