/**
* 保存添加
*
* @return
*/
@RequestMapping(value = "taizhang/add.action", method = { RequestMethod.POST })
public String taizhangAdd(Equipment equipment, @RequestParam("photo") MultipartFile file,
HttpServletRequest request) {
// 添加图片
// 1.判断头像是否为空
String n = null;
if (file != null || file.equals("")) {
// 文件上传处理
// 1.获取上传路径
String str = request.getServletContext().getRealPath("//upload//equipViews");
// 2.如果该路径不存在,创建此路径
File path = new File(str);
if (!path.exists()) {
path.mkdirs();
}
// 3.获取名字 拼接路径
String name = file.getOriginalFilename();
n = name;
// 根据是否有name来判断是否有图片
/*
* if(name.equals("")){
*
* }
*/
// 4.对名字进行处理
name = UUID.randomUUID() + name;
// 5.拼接路径
File desPath = new File(path, name);
// 6.文件上传
try {
// writeByteArrayToFile(要上传的路径, 文件---字节数组)
FileUtils.writeByteArrayToFile(desPath, file.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
// 7.把路径放到数据库里 把该路径设置到user里面
equipment.setPic("equipViews//" + name);
}
/**
* 如果文件上传了,修改数据库 把原图片給替代 如果文件沒有上传 1.把原来的给他 2.不修改图片的路径 没有 --改 没有值 --不改 有值
* --不改
*/
if (n == null || n.equals("")) {
equipment.setPic(null);
}
equipmentService.insertSelective(equipment);
return "redirect:/equipViews/taizhang.action";
}
// 跳转到查看详情页面
@RequestMapping("taizhang/fingById.action")
public String taizhangFingById(Model model, Integer id) {
Equipment equipment = equipmentService.selectByPrimaryKey(id);
model.addAttribute("equipmentList", equipment);
return "equipViews/detailsParameter";
}