(源代码见仓库:https://gitee.com/jianghao233/course)
修改代码:
修改:StudentManager.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
学生列表
学号
姓名
密码
班级
操作
当前没有数据
${s.stunum }
${s.stuname }
${s.password }
${s.classname }
编辑
删除
修改 courseAdd.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
课程添加
修改 StudentController.java
package com.neuedu.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import com.neuedu.po.TbClass;
import com.neuedu.po.TbStudent;
import com.neuedu.service.ClassService;
import com.neuedu.service.StudentService;
import com.neuedu.vo.StudentVO;
import oracle.net.aso.s;
@Controller
@RequestMapping("/student")
public class StudentController {
@Autowired
private ClassService classService;
@Autowired
private StudentService studentService;
@RequestMapping({"/","/list"})
//查询所有学生信息
public String list(Model model) {
List list = studentService.getList();
List classes = classService.getList();
model.addAttribute("list",list);
model.addAttribute("classes",classes);
return "admin/studentManager";
}
@RequestMapping("/save")
public String save(TbStudent tbStudent,@RequestParam("fileurl") MultipartFile multipartFile) { //修改代码
System.out.println(multipartFile); //测试代码
studentService.save(tbStudent);
return "redirect:/student/";
}
@RequestMapping("/modify")
public String modify(TbStudent tbStudent) {
studentService.update(tbStudent);
return "redirect:/student/";
}
@RequestMapping("/delete")
public String delete(Integer stuid) {
studentService.delete(stuid);
return "redirect:/student/";
}
}
修改 springmvc.xml
//新增代码
//新增代码
输出:点击添加信息---输入添加信息--提交---信息成功输出到前台,控制台输出测试代码
但这只是对象传递成功,数据库并没有保存到图片信息,继续修改代码
修改 StudentController.java
package com.neuedu.controller;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import com.neuedu.po.TbClass;
import com.neuedu.po.TbStudent;
import com.neuedu.service.ClassService;
import com.neuedu.service.StudentService;
import com.neuedu.vo.StudentVO;
import oracle.net.aso.s;
@Controller
@RequestMapping("/student")
public class StudentController {
@Autowired
private ClassService classService;
@Autowired
private StudentService studentService;
@RequestMapping({"/","/list"})
//查询所有学生信息
public String list(Model model) {
List list = studentService.getList();
List classes = classService.getList();
model.addAttribute("list",list);
model.addAttribute("classes",classes);
return "admin/studentManager";
}
@RequestMapping("/save") //新增代码
public String save(TbStudent tbStudent,HttpServletRequest request,@RequestParam("fileurl") MultipartFile multipartFile) throws Exception {
String oldname = multipartFile.getOriginalFilename();
/*获取真实路径*/
String basePath = request.getRealPath("/static/upload");
System.out.println("basePath==========="+basePath);
/*创建 file 对象,以 basePath 作为目录,以 oldname 作为文件名,但这是虚拟的,只存在于内存中*/
File file = new File(basePath,oldname);
/*将上传来的图片写入到 file 中*/
multipartFile.transferTo(file); //新增代码
studentService.save(tbStudent);
return "redirect:/student/";
}
@RequestMapping("/modify")
public String modify(TbStudent tbStudent) {
studentService.update(tbStudent);
return "redirect:/student/";
}
@RequestMapping("/delete")
public String delete(Integer stuid) {
studentService.delete(stuid);
return "redirect:/student/";
}
}
输出:经过与上述步骤相同的提交后,图片被成功保存到真实路径之中,但数据库中还是没有图片信息
还存在一个问题,可能会有重名的图片导致之前已保存的图片被覆盖,解决这个问题,修改代码:
新建 UploadUtils.java
package com.neuedu.utils;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.multipart.MultipartFile;
public class UploadUtils {
/**
* 获取文件真实名称
* 由于浏览器的不同获取的名称可能为:c12312231abd:/upload/1.jpg或者1.jpg
* 最终获取的为 1.jpg
* @param name 上传上来的文件名称
* @return 真实名称
*/
public static String getRealName(String name){
//获取最后一个"/"
int index = name.lastIndexOf("\\");
return name.substring(index+1);
}
/**
* 获取随机名称
* @param realName 真实名称
* @return uuid 随机名称
*/
public static String getUUIDName(String realName){
//realname 可能是 1.jpg 也可能是 1
//获取后缀名
int index = realName.lastIndexOf(".");
if(index==-1){
return UUID.randomUUID().toString().replace("-", "").toUpperCase();
}else{
return UUID.randomUUID().toString().replace("-", "").toUpperCase()+realName.substring(index);
}
}
}
修改 StudentController.java
package com.neuedu.controller;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import com.neuedu.po.TbClass;
import com.neuedu.po.TbStudent;
import com.neuedu.service.ClassService;
import com.neuedu.service.StudentService;
import com.neuedu.utils.UploadUtils;
import com.neuedu.vo.StudentVO;
import oracle.net.aso.s;
@Controller
@RequestMapping("/student")
public class StudentController {
@Autowired
private ClassService classService;
@Autowired
private StudentService studentService;
@RequestMapping({"/","/list"})
//查询所有学生信息
public String list(Model model) {
List list = studentService.getList();
List classes = classService.getList();
model.addAttribute("list",list);
model.addAttribute("classes",classes);
return "admin/studentManager";
}
@RequestMapping("/save")
public String save(TbStudent tbStudent,HttpServletRequest request,@RequestParam("fileurl") MultipartFile multipartFile) throws Exception {
String oldname = multipartFile.getOriginalFilename();
/*为避免重名带来的图片覆盖问题,修改保存图片的名字 */ //新增代码
/*1.去掉文件名中的路径信息*/
String realName = UploadUtils.getRealName(oldname);
/*2.获取一个UUID的名字*/
String uuidName = UploadUtils.getUUIDName(realName);
System.out.println(uuidName);
//新增代码
/*获取真实路径*/
String basePath = request.getRealPath("/static/upload");
/*创建 file 对象,以 basePath 作为目录,以 uuidName 作为文件名,但这是虚拟的,只存在于内存中*/
File file = new File(basePath,uuidName); //修改代码
/*将上传来的图片写入到 file 虚拟路径中*/
multipartFile.transferTo(file);
studentService.save(tbStudent);
return "redirect:/student/";
}
@RequestMapping("/modify")
public String modify(TbStudent tbStudent) {
studentService.update(tbStudent);
return "redirect:/student/";
}
@RequestMapping("/delete")
public String delete(Integer stuid) {
studentService.delete(stuid);
return "redirect:/student/";
}
}
输出:继续新建一个 小E,头像使用和小D同文件名的图片,但在真实路径中,名称会被随机数代替,不会发生覆盖现象
但这样产生的问题是,查找图片会因为随机名称而导致十分困难,继续修改代码
修改 UploadUtils.java
package com.neuedu.utils;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.multipart.MultipartFile;
public class UploadUtils {
/**
* 获取文件真实名称
* 由于浏览器的不同获取的名称可能为:c12312231abd:/upload/1.jpg或者1.jpg
* 最终获取的为 1.jpg
* @param name 上传上来的文件名称
* @return 真实名称
*/
public static String getRealName(String name){
//获取最后一个"/"
int index = name.lastIndexOf("\\");
return name.substring(index+1);
}
/**
* 获取随机名称
* @param realName 真实名称
* @return uuid 随机名称
*/
public static String getUUIDName(String realName){
//realname 可能是 1.jpg 也可能是 1
//获取后缀名
int index = realName.lastIndexOf(".");
if(index==-1){
return UUID.randomUUID().toString().replace("-", "").toUpperCase();
}else{
return UUID.randomUUID().toString().replace("-", "").toUpperCase()+realName.substring(index);
}
}
/**
* 获取文件目录,可以获取256个随机目录 //新增代码
* @return 随机目录
*
*
*/
public static String getDir(){
String s="0123456789ABCDEF";
Random r = new Random();
return "/"+s.charAt(r.nextInt(16))+"/"+s.charAt(r.nextInt(16));
}
public static void main(String[] args) {
String realName = getRealName(s);
System.out.println(realName);
String uuidName = getUUIDName(realName);
System.out.println(uuidName);
String dir = getDir();
System.out.println(dir); //新增代码
}
}
修改 StudentController.java
package com.neuedu.controller;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import com.neuedu.po.TbClass;
import com.neuedu.po.TbStudent;
import com.neuedu.service.ClassService;
import com.neuedu.service.StudentService;
import com.neuedu.utils.UploadUtils;
import com.neuedu.vo.StudentVO;
import oracle.net.aso.s;
@Controller
@RequestMapping("/student")
public class StudentController {
@Autowired
private ClassService classService;
@Autowired
private StudentService studentService;
@RequestMapping({"/","/list"})
//查询所有学生信息
public String list(Model model) {
List list = studentService.getList();
List classes = classService.getList();
model.addAttribute("list",list);
model.addAttribute("classes",classes);
return "admin/studentManager";
}
@RequestMapping("/save")
public String save(TbStudent tbStudent,HttpServletRequest request,@RequestParam("fileurl") MultipartFile multipartFile) throws Exception {
String oldname = multipartFile.getOriginalFilename();
/*为避免重名带来的图片覆盖问题,修改保存图片的名字 */
/*1.去掉文件名中的路径信息*/
String realName = UploadUtils.getRealName(oldname);
/*2.获取一个UUID的名字*/
String uuidName = UploadUtils.getUUIDName(realName);
System.out.println(uuidName);
/*获取真实路径*/
String basePath = request.getRealPath("/static/upload");
/*获取目录*/
String dir = UploadUtils.getDir(); //新增代码
File filedir = new File(basePath + dir);
if(!filedir.exists()) {
filedir.mkdirs();
} //新增代码
/*创建 file 对象,以 basePath 作为目录,以 uuidName 作为文件名,但这是虚拟的,只存在于内存中*/
File file = new File(filedir,uuidName); //修改代码
/*将上传来的图片写入到 file 虚拟路径中*/
multipartFile.transferTo(file);
studentService.save(tbStudent);
return "redirect:/student/";
}
@RequestMapping("/modify")
public String modify(TbStudent tbStudent) {
studentService.update(tbStudent);
return "redirect:/student/";
}
@RequestMapping("/delete")
public String delete(Integer stuid) {
studentService.delete(stuid);
return "redirect:/student/";
}
}
输出:继续新建一个 小F,头像使用和小D同文件名的图片,但在真实路径中,名称会被随机数代替,不会发生覆盖现象 ,且存放在随机文件夹中