Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.dlshouwen.wzgl.picture.controller; import com.dlshouwen.core.base.config.CONFIG; import com.dlshouwen.core.base.extra.grid.model.Pager; import com.dlshouwen.core.base.extra.grid.utils.PagerPropertyUtils; import com.dlshouwen.core.base.http.FileUploadClient; import com.dlshouwen.core.base.model.AjaxResponse; import com.dlshouwen.core.base.model.OperationType; import com.dlshouwen.core.base.model.SessionUser; import com.dlshouwen.core.base.utils.GUID; import com.dlshouwen.core.base.utils.LogUtils; import com.dlshouwen.wzgl.album.dao.AlbumDao; import com.dlshouwen.wzgl.album.model.Album; import com.dlshouwen.wzgl.picture.dao.PictureDao; import com.dlshouwen.wzgl.picture.model.Picture; import java.io.File; import org.springframework.ui.Model; import java.io.FileOutputStream; import java.io.InputStream; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.validation.Valid; import net.sf.json.JSONObject; import org.apache.commons.lang.StringUtils; 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.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; /** * ? * * @author xlli */ @Controller @RequestMapping("/wzgl/picture/picture") public class PictureController { /** * */ private String basePath = "wzgl/picture/"; /** * ?? */ private PictureDao dao; //? private AlbumDao albumDao; @Resource(name = "albumDao") public void setAlbumDao(AlbumDao albumDao) { this.albumDao = albumDao; } /** * ?? * * @param dao ?? */ @Resource(name = "pictureDao") public void setDao(PictureDao dao) { this.dao = dao; } /** * ? * * @param request * @return basePath + "pictureList" * @throws Exception */ @RequestMapping(value = "", method = RequestMethod.GET) public String goPicturePage(HttpServletRequest request) throws Exception { // ? LogUtils.updateOperationLog(request, OperationType.VISIT, "??"); return basePath + "pictureList"; } /** * ?? * * @param gridPager ? * @param request * @param response ? * @throws Exception */ @RequestMapping(value = "/list", method = RequestMethod.POST) @ResponseBody public void getPictureList(String gridPager, HttpServletRequest request, HttpServletResponse response) throws Exception { // Pager Pager pager = PagerPropertyUtils.copy(JSONObject.fromObject(gridPager)); // ?? dao.getPictureList(pager, request, response); // ? LogUtils.updateOperationLog(request, OperationType.SEARCH, "????"); } /** * ? * * @param request * @param model * @return * @throws Exception */ @RequestMapping(value = "/add", method = RequestMethod.GET) public String addPicture(HttpServletRequest request, Model model) throws Exception { // Picture pic = new Picture(); // pic.setShow("1"); //?? model.addAttribute("picture", pic); // ? LogUtils.updateOperationLog(request, OperationType.VISIT, "?"); return basePath + "addPicture"; } /** * * * @param picture * @param bindingResult ? * @param request * @return ajax? * @throws Exception */ @RequestMapping(value = "/ajaxAdd", method = RequestMethod.POST) public void ajaxAddPicture(@Valid Picture picture, BindingResult bindingResult, HttpServletRequest request, HttpServletResponse response) throws Exception { // AJAX? AjaxResponse ajaxResponse = new AjaxResponse(); // ??zz if (bindingResult.hasErrors()) { ajaxResponse.bindingResultHandler(bindingResult); //? Map map = new HashMap(); map.put("URL", basePath + "picture"); ajaxResponse.setExtParam(map); // ? LogUtils.updateOperationLog(request, OperationType.INSERT, "???" + AjaxResponse.getBindingResultMessage(bindingResult) + ""); response.setContentType("text/html;charset=utf-8"); JSONObject obj = JSONObject.fromObject(ajaxResponse); response.getWriter().write(obj.toString()); return; } MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; MultipartFile multipartFile = multipartRequest.getFile("picture"); String fileName = multipartFile.getOriginalFilename(); //? String path = null; if (null != multipartFile && StringUtils.isNotEmpty(multipartFile.getOriginalFilename())) { JSONObject jobj = FileUploadClient.upFile(request, fileName, multipartFile.getInputStream()); if (jobj.getString("responseMessage").equals("OK")) { path = jobj.getString("fpath"); } } /* int pos = fileName.lastIndexOf("."); fileName = fileName.substring(pos); String fileDirPath = request.getSession().getServletContext().getRealPath("/"); String path = fileDirPath.substring(0, fileDirPath.lastIndexOf(File.separator)) + CONFIG.UPLOAD_PIC_PATH; Date date = new Date(); fileName = String.valueOf(date.getTime()) + fileName; File file = new File(path); if (!file.exists()) { file.mkdirs(); } file = new File(path + "/" + fileName); if (!file.exists()) { file.createNewFile(); } path = path + "/" + fileName; FileOutputStream fos = null; InputStream s = null; try { fos = new FileOutputStream(file); s = multipartFile.getInputStream(); byte[] buffer = new byte[1024]; int read = 0; while ((read = s.read(buffer)) != -1) { fos.write(buffer, 0, read); } fos.flush(); } catch (Exception e) { e.printStackTrace(); } finally { if (fos != null) { fos.close(); } if (s != null) { s.close(); } } */ if (StringUtils.isNotEmpty(path)) { // ???? SessionUser sessionUser = (SessionUser) request.getSession().getAttribute(CONFIG.SESSION_USER); String userId = sessionUser.getUser_id(); String userName = sessionUser.getUser_name(); Date nowDate = new Date(); // ? picture.setPicture_id(new GUID().toString()); picture.setCreate_time(nowDate); picture.setUser_id(userId); picture.setUser_name(userName); path = path.replaceAll("\\\\", "/"); picture.setPath(path); // dao.insertPicture(picture); // ???? ajaxResponse.setSuccess(true); ajaxResponse.setSuccessMessage("??"); //? Map map = new HashMap(); map.put("URL", basePath + "picture"); ajaxResponse.setExtParam(map); } else { // ???? ajaxResponse.setError(true); ajaxResponse.setErrorMessage("?"); } // ? LogUtils.updateOperationLog(request, OperationType.INSERT, "?" + picture.getPicture_id()); response.setContentType("text/html;charset=utf-8"); JSONObject obj = JSONObject.fromObject(ajaxResponse); response.getWriter().write(obj.toString()); return; } /** * ? * * @param pictureId ID * @param request * @param model ?? * @return basePath + "editPicture" * @throws Exception */ @RequestMapping(value = "/{pictureId}/edit", method = RequestMethod.GET) public String editPicture(@PathVariable String pictureId, HttpServletRequest request, Model model) throws Exception { // ?? Picture pic = dao.getPictureById(pictureId); // ?? model.addAttribute("picture", pic); // ? LogUtils.updateOperationLog(request, OperationType.VISIT, "??" + pic.getPicture_id() + "??" + pic.getPicture_name()); return basePath + "editPicture"; } /** * * * @param picture * @param bindingResult ? * @param request * @return ajax? * @throws Exception */ @RequestMapping(value = "/ajaxEdit", method = RequestMethod.POST) @ResponseBody public void ajaxEditPicture(@Valid Picture picture, HttpServletRequest request, BindingResult bindingResult, HttpServletResponse response) throws Exception { // AJAX? AjaxResponse ajaxResponse = new AjaxResponse(); // ?? if (bindingResult.hasErrors()) { ajaxResponse.bindingResultHandler(bindingResult); // ? LogUtils.updateOperationLog(request, OperationType.INSERT, "???" + AjaxResponse.getBindingResultMessage(bindingResult) + ""); response.setContentType("text/html;charset=utf-8"); JSONObject obj = JSONObject.fromObject(ajaxResponse); response.getWriter().write(obj.toString()); return; } MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; MultipartFile multipartFile = multipartRequest.getFile("picture"); String fileName = multipartFile.getOriginalFilename(); //? String path = null; if (null != multipartFile && StringUtils.isNotEmpty(multipartFile.getOriginalFilename())) { JSONObject jobj = FileUploadClient.upFile(request, fileName, multipartFile.getInputStream()); if (jobj.getString("responseMessage").equals("OK")) { path = jobj.getString("fpath"); } } Picture oldPicture = dao.getPictureById(picture.getPicture_id()); oldPicture.setDescription(picture.getDescription()); oldPicture.setFlag(picture.getFlag()); oldPicture.setOrder(picture.getOrder()); oldPicture.setPicture_name(picture.getPicture_name()); oldPicture.setShow(picture.getShow()); // ?? Date nowDate = new Date(); // picture.setUpdate_time(nowDate); if (null != path) { oldPicture.setPath(path); } try { // dao.updatePicture(picture); // ???? ajaxResponse.setSuccess(true); ajaxResponse.setSuccessMessage("??"); } catch (Exception e) { // ???? ajaxResponse.setError(true); ajaxResponse.setErrorMessage("?"); } //? Map map = new HashMap(); map.put("URL", basePath + "picture"); ajaxResponse.setExtParam(map); // ? LogUtils.updateOperationLog(request, OperationType.INSERT, "?" + picture.getPicture_id()); response.setContentType("text/html;charset=utf-8"); JSONObject obj = JSONObject.fromObject(ajaxResponse); response.getWriter().write(obj.toString()); } /** * * * @param pictureIds ? * @param request * @return ajax? * @throws Exception */ @RequestMapping(value = "/delete", method = RequestMethod.POST) @ResponseBody public AjaxResponse deletePicture(String pictureIds, HttpServletRequest request) throws Exception { // dao.deletePicture(pictureIds); // ? AjaxResponse ajaxResponse = new AjaxResponse(); ajaxResponse.setSuccess(true); ajaxResponse.setSuccessMessage("??"); // ? LogUtils.updateOperationLog(request, OperationType.DELETE, "?" + pictureIds); return ajaxResponse; } /** * * * @param pictureIds ? * @param request * @return ajax? * @throws Exception */ @RequestMapping(value = "/show", method = RequestMethod.POST) @ResponseBody public AjaxResponse showPicture(String pictureIds, HttpServletRequest request) throws Exception { // dao.showPicture(pictureIds); // ? AjaxResponse ajaxResponse = new AjaxResponse(); ajaxResponse.setSuccess(true); ajaxResponse.setSuccessMessage("??"); // ? LogUtils.updateOperationLog(request, OperationType.UPDATE, "?" + pictureIds); return ajaxResponse; } /** * ?? * * @param pictureIds ? * @param request * @return ajax? * @throws Exception */ @RequestMapping(value = "/hide", method = RequestMethod.POST) @ResponseBody public AjaxResponse hidePicture(String pictureIds, HttpServletRequest request) throws Exception { // ?? int i = dao.hidePicture(pictureIds); // ? AjaxResponse ajaxResponse = new AjaxResponse(); ajaxResponse.setSuccess(true); ajaxResponse.setSuccessMessage("????"); // ? LogUtils.updateOperationLog(request, OperationType.UPDATE, "???" + pictureIds); return ajaxResponse; } /** * ? * * @param request * @param Id id * @param model * @return basePath + "pictureList" * @throws Exception */ @RequestMapping(value = "/{albumId}/batchAdd", method = RequestMethod.GET) public String batchAdd(HttpServletRequest request, @PathVariable String albumId, Model model) throws Exception { //id model.addAttribute("albumId", albumId); // ? LogUtils.updateOperationLog(request, OperationType.VISIT, "??"); return basePath + "batchAdd"; } /** * * * @param picture * @param bindingResult ? * @param request * @return ajax? */ @RequestMapping(value = "/{albumId}/ajaxMultipartAdd", method = RequestMethod.POST) public void ajaxAddMultipartPictureAl(@Valid Picture picture, BindingResult bindingResult, HttpServletRequest request, HttpServletResponse response, @PathVariable String albumId) throws Exception { // AJAX? AjaxResponse ajaxResponse = new AjaxResponse(); String flag = request.getParameter("flag"); MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; MultipartFile multipartFile = multipartRequest.getFile("file"); String sname = multipartFile.getOriginalFilename(); //? String path = null; if (null != multipartFile && StringUtils.isNotEmpty(multipartFile.getOriginalFilename())) { JSONObject jobj = FileUploadClient.upFile(request, multipartFile.getOriginalFilename(), multipartFile.getInputStream()); if (jobj.getString("responseMessage").equals("OK")) { path = jobj.getString("fpath"); } } /* String fileName = multipartFile.getOriginalFilename(); int pos = fileName.lastIndexOf("."); fileName = fileName.substring(pos); String fileDirPath = request.getSession().getServletContext().getRealPath("/"); String path = fileDirPath.substring(0, fileDirPath.lastIndexOf(File.separator)) + CONFIG.UPLOAD_PIC_PATH; Date date = new Date(); fileName = String.valueOf(date.getTime()) + fileName; File file = new File(path); if (!file.exists()) { file.mkdirs(); } file = new File(path + "/" + fileName); if (!file.exists()) { file.createNewFile(); } path = path + "/" + fileName; FileOutputStream fos = null; InputStream s = null; try { fos = new FileOutputStream(file); s = multipartFile.getInputStream(); byte[] buffer = new byte[1024]; int read = 0; while ((read = s.read(buffer)) != -1) { fos.write(buffer, 0, read); } fos.flush(); } catch (Exception e) { e.printStackTrace(); } finally { if (fos != null) { fos.close(); } if (s != null) { s.close(); } } */ if (StringUtils.isNotEmpty(path)) { // ???? SessionUser sessionUser = (SessionUser) request.getSession().getAttribute(CONFIG.SESSION_USER); String userId = sessionUser.getUser_id(); String userName = sessionUser.getUser_name(); Date nowDate = new Date(); // ?? picture.setPicture_name(sname); // ? picture.setPicture_id(new GUID().toString()); picture.setCreate_time(nowDate); picture.setUser_id(userId); picture.setUser_name(userName); path = path.replaceAll("\\\\", "/"); picture.setPath(path); picture.setAlbum_id(albumId); picture.setFlag("1"); picture.setShow("1"); picture.setUpdate_time(new Date()); // dao.insertPicture(picture); //?? if (flag.equals("article")) { Album alb = albumDao.getAlbumById(albumId); if (StringUtils.isEmpty(alb.getAlbum_coverpath())) { albumDao.setAlbCover(albumId, picture.getPath()); } } // ???? ajaxResponse.setSuccess(true); ajaxResponse.setSuccessMessage("??"); } else { // ???? ajaxResponse.setError(true); ajaxResponse.setErrorMessage("?"); } // ? LogUtils.updateOperationLog(request, OperationType.INSERT, "?" + picture.getPicture_id()); response.setContentType("text/html;charset=utf-8"); JSONObject obj = JSONObject.fromObject(ajaxResponse); response.getWriter().write(obj.toString()); return; } /** * ? * * @param pictureId id * @param request * @param model * @return * @throws Exception */ @RequestMapping(value = "/{pictureId}/editAlbPic", method = RequestMethod.GET) public String editAlbumPicture(@PathVariable String pictureId, HttpServletRequest request, Model model) throws Exception { // ?? Picture pic = dao.getPictureById(pictureId); // ?? model.addAttribute("pic", pic); // ? LogUtils.updateOperationLog(request, OperationType.VISIT, "??" + pic.getPicture_id() + "??" + pic.getPicture_name()); return basePath + "editAlbPic"; } //?? @RequestMapping(value = "/editDescription", method = RequestMethod.POST) @ResponseBody public AjaxResponse editDescription(HttpServletRequest request) throws Exception { // String picId = request.getParameter("picId"); String pic_description = request.getParameter("pic_description"); dao.updatePicDescription(picId, pic_description); // ? AjaxResponse ajaxResponse = new AjaxResponse(); ajaxResponse.setSuccess(true); ajaxResponse.setSuccessMessage("????"); // ? LogUtils.updateOperationLog(request, OperationType.UPDATE, "???" + picId); return ajaxResponse; } //? @RequestMapping(value = "/updatePicOrder", method = RequestMethod.POST) @ResponseBody public AjaxResponse updatePicOrder(HttpServletRequest request, HttpServletResponse response) throws Exception { //AJAX? AjaxResponse ajaxResponse = new AjaxResponse(); String picIds = request.getParameter("picIds"); String parms = request.getParameter("order"); //?? if (picIds != null && !"".equals(picIds)) { String[] picId = picIds.split(","); String[] ord = parms.split(","); List<Object[]> list = new ArrayList(); for (int i = 0; i < ord.length; i++) { Object[] objs = { ord[i], picId[i] }; list.add(objs); } dao.updatePicOrder(list); } // ???? ajaxResponse.setSuccess(true); ajaxResponse.setSuccessMessage("??"); // ? LogUtils.updateOperationLog(request, OperationType.INSERT, "?" + picIds); response.setContentType("text/html;charset=utf-8"); JSONObject obj = JSONObject.fromObject(ajaxResponse); return ajaxResponse; } }