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