Java tutorial
package com.bvvy.photo.web.controller; import com.bvvy.basic.util.CommonUitl; import com.bvvy.basic.util.FileUploadUtil; import com.bvvy.photo.constent.PgerStatus; import com.bvvy.photo.constent.PhotoFinalValue; import com.bvvy.photo.dto.PgerFormDto; import com.bvvy.photo.iservice.IImageService; import com.bvvy.photo.iservice.IPgerService; import com.bvvy.photo.model.Image; import com.bvvy.photo.model.Pger; import com.bvvy.photo.util.PhotoUtil; import com.bvvy.sys.auth.AuthClass; import com.bvvy.sys.auth.AuthMethod; import com.bvvy.sys.model.User; import com.sun.org.apache.xpath.internal.operations.Mult; import org.apache.commons.compress.compressors.FileNameUtil; import org.apache.commons.io.FileUtils; import org.apache.commons.io.FilenameUtils; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import javax.servlet.http.HttpSession; import java.io.File; import java.io.IOException; /** * Created by ctt on 2017/3/26. * */ @Controller @AuthClass public class PgerContoller { @Resource private IPgerService pgerService; @Resource private IImageService imageService; @RequestMapping(value = "/pger", method = RequestMethod.GET) @AuthMethod(role = "ROLE_COMMON") public String add(Model model, HttpSession session) { User loginUser = (User) session.getAttribute("loginUser"); if (CommonUitl.isEmpty(loginUser)) return "redirect:/login"; model.addAttribute(new Pger()); return "front/pger/add"; } @RequestMapping(value = "/pger", method = RequestMethod.POST) @AuthMethod(role = "ROLE_COMMON") public String add(PgerFormDto pgerFormDto, MultipartFile idCardImg, MultipartFile shopImg, MultipartFile licenseImg, HttpSession session) { User loginUser = (User) session.getAttribute("loginUser"); Pger pger = new Pger(); pger.setUser(loginUser); pger.setStatus(PgerStatus.REVIEWING.ordinal()); pger.setIdCardImg(saveImage(idCardImg)); pger.setLicenseImg(saveImage(licenseImg)); pger.setShopImg(saveImage(shopImg)); pger.setWorkAddress(pgerFormDto.getWorkAddress()); pger.setLicenseNo(pgerFormDto.getLicenseNo()); pger.setTrueName(pgerFormDto.getTrueName()); pger.setPgerType(pgerFormDto.getPgerType()); pger.setIdCardNo(pgerFormDto.getIdCardNo()); pgerService.add(pger); // return "redirect:/user/" + loginUser.getId() + "/album"; return "front/pger/add"; } @RequestMapping("/pgers") public String find(Model model) { model.addAttribute("datas", pgerService.find()); return "front/pger/pgers"; } @RequestMapping("/admin/pger/pgers") public String findForAdmin(Model model) { model.addAttribute("datas", pgerService.find()); return "pger/pgers"; } @RequestMapping(value = "/admin/pger/update/{id}", method = RequestMethod.GET) public String update(Pger pger, @PathVariable int id) { Pger tpger = pgerService.load(id); return "pger/update"; } @RequestMapping(value = "/admin/pger/update/{id}", method = RequestMethod.POST) public String update(Pger pger, @PathVariable int id, MultipartFile idCardImg, MultipartFile licenseImg, MultipartFile shopImg) { return "redirect:/user/album"; } @RequestMapping("/admin/pger/{id}") public String show(@PathVariable int id, Model model) { model.addAttribute("pger", pgerService.load(id)); return "pger/show"; } @RequestMapping("/admin/pger/updateStatus/{id}/{status}") public String updateStatus(@PathVariable int id, @PathVariable int status) { pgerService.updateStatus(id, status); return "redirect:/admin/pger/pgers"; } private String saveImage(MultipartFile mf) { Image image = new Image(); image.setOldName(mf.getOriginalFilename()); image.setSuffix(FilenameUtils.getExtension(mf.getOriginalFilename())); image.setSize(mf.getSize()); image.setType(mf.getContentType()); String path = FileUploadUtil.getInstance().saveOriginFile(mf); image.setNewName(path); imageService.add(image); return path; } }