前端页面,使用thymeleaf模板引擎
<div>
<ul class="nav navbar-nav">
<li><a href="/toStuMainPage">查询证书信息</a></li>
<li class="active"><a href="#">上传证书</a></li>
<li><a href="/exit">退出</a></li>
</ul>
</div>
将图片保存到服务器(图片上传功能)
以登陆账号+学生名创建一个独立的文件夹
/**
* MultipartFile 自动封装上传过来的证书图片
*
* @return
*/
@PostMapping("/uploadCertificateImage")
public String uploadCertificateImage(@RequestPart("certificateImage") MultipartFile[] certificateImage,
HttpServletRequest request, HttpSession session, Model model) throws IOException {
String sessionName = (String) session.getAttribute("welcomeName");
log.info("sessionName:"+sessionName);
String sessionIdentity = (String) session.getAttribute("identity");
log.info("sessionIdentity:"+sessionIdentity);
for (MultipartFile cf : certificateImage) {
if (!cf.isEmpty()) {
//保存到文件服务器,OSS服务器
String originalFilename = cf.getOriginalFilename();
// String substring = originalFilename.substring(0, originalFilename.length() - 4);
//文件目录名,以学生名+学生身份证号拼接
String substring = sessionName+sessionIdentity;
log.info("以学生名+学生身份证号拼接substring:" + substring);
String url = this.getClass().getResource("/static/savePhotos/").getPath();//D:\JavaWebLocal\IdeaProjects\examinationSystem\target\classes\2/2.PNG
// String url = request.getSession().getServletContext().getRealPath("/");//C:\Users\17763\AppData\Local\Temp\tomcat-docbase.8080.1075427637439030012\
String saveFilePath = url +substring;
File file = new File(saveFilePath);
if (!file.exists()) {
file.mkdir();
}
cf.transferTo(new File(file + "/" + originalFilename));
log.info("存放地址:" + file + "/" + originalFilename);
//将图片的路径保存到path字段,等待审核
String savePath=file+"/"+originalFilename;//存放这个目录到数据库的path字段
String insertPath="UPDATE inputexcel SET path=? WHERE `name`=? AND identityCode=?";
jdbcTemplate.update(insertPath,savePath,sessionName,sessionIdentity);
}
}
return "success";
}
下载功能....未完待续