diff --git a/src/main/java/com/lq/cms/web/wechat/WechatMessageController.java b/src/main/java/com/lq/cms/web/wechat/WechatMessageController.java index 30d9c9f..a937b8d 100755 --- a/src/main/java/com/lq/cms/web/wechat/WechatMessageController.java +++ b/src/main/java/com/lq/cms/web/wechat/WechatMessageController.java @@ -9,6 +9,7 @@ import com.lq.cms.vo.WechatMessageVo; import com.lq.cms.web.AdminBaseController; import com.lq.code.entity.AjaxResult; import com.lq.code.util.Constant; +import com.lq.code.util.FileUtil; import com.lq.entity.SysUser; import com.lq.entity.WechatMessage; import org.apache.shiro.authz.annotation.RequiresPermissions; @@ -21,8 +22,11 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; +import java.io.File; +import java.io.IOException; import java.util.List; import java.util.Map; +import java.util.UUID; /** * 微信公众号消息管理后台控制类 @@ -72,6 +76,8 @@ public class WechatMessageController { public Object save(WechatMessageVo vo,@RequestParam(value = "file",required = false) MultipartFile file){ AjaxResult ajaxResult = new AjaxResult(); try { + String filePath = upLoadFile(file); + vo.setImageUrl(filePath); wechatMessageService.save(vo); } catch (IllegalAccessException e) { e.printStackTrace(); @@ -86,6 +92,8 @@ public class WechatMessageController { @ResponseBody public Object update(WechatMessageVo vo,@RequestParam(value = "file",required = false) MultipartFile file){ AjaxResult ajaxResult = new AjaxResult(); + String filePath = upLoadFile(file); + vo.setImageUrl(filePath); wechatMessageService.update(vo); return ajaxResult; } @@ -113,4 +121,25 @@ public class WechatMessageController { return ajaxResult; } + //内部方法 ,上传文件,并返回文件路径 + private String upLoadFile(MultipartFile multipartFile){ + String newFileName = null; + if (multipartFile!=null && multipartFile.getSize()>0){ + UUID uuid = UUID.randomUUID(); + String fileType = FileUtil.fileFormat(multipartFile.getOriginalFilename()); + newFileName = "wechat/"+uuid.toString()+"."+fileType; + File newFile = new File(FILE_LOAD_PATH +newFileName); + + if (!newFile.exists()){ + newFile.mkdirs(); + } + try { + multipartFile.transferTo(newFile); + } catch (IOException e) { + e.printStackTrace(); + } + } + return newFileName; + } + } diff --git a/src/main/resources/spring-mybatis-config.xml b/src/main/resources/spring-mybatis-config.xml index 67bfd02..1413276 100644 --- a/src/main/resources/spring-mybatis-config.xml +++ b/src/main/resources/spring-mybatis-config.xml @@ -62,14 +62,14 @@ - - - + + + - + - + diff --git a/src/main/webapp/WEB-INF/views/jsp/cms/wechat/message/edit.jsp b/src/main/webapp/WEB-INF/views/jsp/cms/wechat/message/edit.jsp index 293579f..c01cbe4 100755 --- a/src/main/webapp/WEB-INF/views/jsp/cms/wechat/message/edit.jsp +++ b/src/main/webapp/WEB-INF/views/jsp/cms/wechat/message/edit.jsp @@ -45,7 +45,7 @@ 图片文件: - + @@ -72,7 +72,7 @@ + + @@ -101,7 +102,7 @@ obj.messageType = $("#messageType").val(); obj.title = $("#title").val(); obj.content = $("#content").val(); - obj.imageUrl = $("#imageUrl").val(); + // obj.imageUrl = $("#imageUrl").val(); obj.mediaId = $("#mediaId").val(); obj.toUrl = $("#toUrl").val(); return obj; @@ -131,10 +132,11 @@ } function post(url,obj) { - $.ajax({ + $.ajaxFileUpload({ url: url, dataType:"json", type:"post", + fileElementId:"file", data:obj, success: function(result){ if(result.success==true){