微信公众号消息新增图片上传

master
星期八 6 years ago
parent c0044a14cc
commit 699ba0b7f6
  1. 29
      src/main/java/com/lq/cms/web/wechat/WechatMessageController.java
  2. 4
      src/main/webapp/WEB-INF/views/jsp/cms/wechat/message/edit.jsp
  3. 8
      src/main/webapp/WEB-INF/views/jsp/cms/wechat/message/index.jsp

@ -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;
}
}

@ -45,7 +45,7 @@
<tr id="tr_imageUrl">
<td>图片文件:</td>
<td><input class="easyui-textbox" type="file" id="imageUrl" data-options="required:true"/></td>
<td><input class="easyui-textbox" type="file" name="file" id="file" data-options="required:true"/></td>
</tr>
<tr id="tr_cropedBigImg">
@ -72,7 +72,7 @@
<script type="text/javascript" href="${ctx}/resources/cms/wechat/wechat.js"/>
<script type="text/javascript">
$(function () {
$('#imageUrl').on('change', function() {//当chooseImage的值改变时,执行此函数
$('#file').on('change', function() {//当chooseImage的值改变时,执行此函数
var filePath = $(this).val(), //获取到input的value,里面是文件的路径
fileFormat = filePath.substring(filePath.lastIndexOf(".")).toLowerCase(),
src = window.URL.createObjectURL(this.files[0]); //转成可以在本地预览的格式

@ -12,9 +12,10 @@
<title>微信公众号管理后台-消息</title>
<link rel="stylesheet" type="text/css" href="${ctx}/resources/code/easyui/themes/default/easyui.css" />
<link rel="stylesheet" type="text/css" href="${ctx}/resources/code/easyui/themes/icon.css" />
<script type="text/javascript" src="${ctx}/resources/code/js/jquery-2.2.4.min.js"></script>
<script type="text/javascript" src="${ctx}/resources/code/js/jquery-3.1.1.min.js"></script>
<script type="text/javascript" src="${ctx}/resources/code/easyui/jquery.easyui.min.1.2.2.js"></script>
<script type="text/javascript" src="${ctx}/resources/plugins/layer/layer.js"></script>
<script type="text/javascript" src="${ctx}/resources/code/js/ajaxfileupload.js"></script>
<script type="text/javascript" href="${ctx}/resources/cms/wechat/wechat.js"></script>
</head>
@ -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){

Loading…
Cancel
Save