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

master
星期八 6 years ago
parent c0044a14cc
commit 699ba0b7f6
  1. 29
      src/main/java/com/lq/cms/web/wechat/WechatMessageController.java
  2. 10
      src/main/resources/spring-mybatis-config.xml
  3. 4
      src/main/webapp/WEB-INF/views/jsp/cms/wechat/message/edit.jsp
  4. 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.cms.web.AdminBaseController;
import com.lq.code.entity.AjaxResult; import com.lq.code.entity.AjaxResult;
import com.lq.code.util.Constant; import com.lq.code.util.Constant;
import com.lq.code.util.FileUtil;
import com.lq.entity.SysUser; import com.lq.entity.SysUser;
import com.lq.entity.WechatMessage; import com.lq.entity.WechatMessage;
import org.apache.shiro.authz.annotation.RequiresPermissions; 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.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.Map; 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){ public Object save(WechatMessageVo vo,@RequestParam(value = "file",required = false) MultipartFile file){
AjaxResult ajaxResult = new AjaxResult(); AjaxResult ajaxResult = new AjaxResult();
try { try {
String filePath = upLoadFile(file);
vo.setImageUrl(filePath);
wechatMessageService.save(vo); wechatMessageService.save(vo);
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
e.printStackTrace(); e.printStackTrace();
@ -86,6 +92,8 @@ public class WechatMessageController {
@ResponseBody @ResponseBody
public Object update(WechatMessageVo vo,@RequestParam(value = "file",required = false) MultipartFile file){ public Object update(WechatMessageVo vo,@RequestParam(value = "file",required = false) MultipartFile file){
AjaxResult ajaxResult = new AjaxResult(); AjaxResult ajaxResult = new AjaxResult();
String filePath = upLoadFile(file);
vo.setImageUrl(filePath);
wechatMessageService.update(vo); wechatMessageService.update(vo);
return ajaxResult; return ajaxResult;
} }
@ -113,4 +121,25 @@ public class WechatMessageController {
return ajaxResult; 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;
}
} }

@ -62,14 +62,14 @@
</bean> </bean>
<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 --> <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" /> <property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/> <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
<!-- 自动扫描mapping.xml文件 --> <!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:mybatis/mapping/*.xml"></property> <property name="mapperLocations" value="classpath:mybatis/mapping/*.xml"></property>
</bean> </bean>
<!-- DAO接口所在包名,Spring会自动查找其下的类 --> <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>

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

@ -12,9 +12,10 @@
<title>微信公众号管理后台-消息</title> <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/default/easyui.css" />
<link rel="stylesheet" type="text/css" href="${ctx}/resources/code/easyui/themes/icon.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/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/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> <script type="text/javascript" href="${ctx}/resources/cms/wechat/wechat.js"></script>
</head> </head>
@ -101,7 +102,7 @@
obj.messageType = $("#messageType").val(); obj.messageType = $("#messageType").val();
obj.title = $("#title").val(); obj.title = $("#title").val();
obj.content = $("#content").val(); obj.content = $("#content").val();
obj.imageUrl = $("#imageUrl").val(); // obj.imageUrl = $("#imageUrl").val();
obj.mediaId = $("#mediaId").val(); obj.mediaId = $("#mediaId").val();
obj.toUrl = $("#toUrl").val(); obj.toUrl = $("#toUrl").val();
return obj; return obj;
@ -131,10 +132,11 @@
} }
function post(url,obj) { function post(url,obj) {
$.ajax({ $.ajaxFileUpload({
url: url, url: url,
dataType:"json", dataType:"json",
type:"post", type:"post",
fileElementId:"file",
data:obj, data:obj,
success: function(result){ success: function(result){
if(result.success==true){ if(result.success==true){

Loading…
Cancel
Save