From 7cc12f5bf6298dac2010c9dd9baf4ecd844fe378 Mon Sep 17 00:00:00 2001 From: Stormeye Wu Date: Tue, 28 May 2019 13:59:35 +0800 Subject: [PATCH] =?UTF-8?q?[=E6=B0=B8=E4=B9=85=E7=B4=A0=E6=9D=90][?= =?UTF-8?q?=E4=BF=AE=E6=94=B9]:=E4=B8=8A=E4=BC=A0=E5=9B=BE=E7=89=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/iformall/WechatOpenApplication.java | 8 +- .../controller/WechatMediaController.java | 73 +++++++++++++++++-- .../src/main/resources/application-dev.yml | 1 + .../src/main/resources/application-prod.yml | 1 + .../src/main/resources/application-test.yml | 1 + 5 files changed, 75 insertions(+), 9 deletions(-) diff --git a/mlWechatOpen/src/main/java/com/iformall/WechatOpenApplication.java b/mlWechatOpen/src/main/java/com/iformall/WechatOpenApplication.java index b7e7a29..c56bdde 100644 --- a/mlWechatOpen/src/main/java/com/iformall/WechatOpenApplication.java +++ b/mlWechatOpen/src/main/java/com/iformall/WechatOpenApplication.java @@ -31,6 +31,9 @@ public class WechatOpenApplication { @Value("${fm.deploy}") private Integer fmDeploy; + @Value("${fm.upload_dir}") + private String uploadDir; + @Bean public boolean isFmException() { return fmException; @@ -49,7 +52,10 @@ public class WechatOpenApplication { @Bean public Integer getFmDeploy() { return fmDeploy; } - + @Bean + public String fmUploadDir() { + return uploadDir; + } public static void main(String[] args) { SpringApplication.run(WechatOpenApplication.class, args); diff --git a/mlWechatOpen/src/main/java/com/iformall/controller/WechatMediaController.java b/mlWechatOpen/src/main/java/com/iformall/controller/WechatMediaController.java index ded2a4f..d6ad5ca 100644 --- a/mlWechatOpen/src/main/java/com/iformall/controller/WechatMediaController.java +++ b/mlWechatOpen/src/main/java/com/iformall/controller/WechatMediaController.java @@ -1,5 +1,6 @@ package com.iformall.controller; +import com.iformall.common.ResultData; import com.iformall.service.WxAuthorizerInfoService; import com.iformall.service.wechat.FmOpenService; import io.swagger.annotations.Api; @@ -8,16 +9,21 @@ import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.mp.api.WxMpService; +import me.chanjar.weixin.mp.bean.material.WxMediaImgUploadResult; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileOutputStream; import java.io.InputStream; +import java.util.UUID; /** * Stormeye Wu @@ -25,9 +31,11 @@ import java.io.InputStream; @Controller @RequestMapping("/material") @Api(description = "微信第三方开放平台素材文件") -public class WechatMediaController { +public class WechatMediaController extends BaseController { private final Logger logger = LoggerFactory.getLogger(getClass()); @Autowired + private String fmUploadDir; + @Autowired private FmOpenService openService; @Autowired @@ -39,12 +47,11 @@ public class WechatMediaController { @ApiImplicitParam(name = "appId", value = "appId", dataType = "String", paramType = "query", required = true), @ApiImplicitParam(name = "mediaId", value = "mediaId", dataType = "String", paramType = "query", required = true)}) public void getMediaInfo(String appId, String mediaId, HttpServletRequest req, HttpServletResponse response) { - InputStream inputStream = null; String filename = mediaId; try { - WxMpService maService = openService.getWxOpenComponentService().getWxMpServiceByAppid(appId); - inputStream = maService.getMaterialService().materialImageOrVoiceDownload(mediaId); + WxMpService mpService = openService.getWxOpenComponentService().getWxMpServiceByAppid(appId); + inputStream = mpService.getMaterialService().materialImageOrVoiceDownload(mediaId); } catch (WxErrorException e) { logger.error("获取永久素材失败", e); throw new RuntimeException(e); @@ -57,11 +64,11 @@ public class WechatMediaController { if (agent.contains("Firefox")) { response.setHeader("Content-disposition", "attachment; filename=" - + new String(filename.getBytes("GB2312"),"ISO-8859-1")); + + new String(filename.getBytes("GB2312"), "ISO-8859-1")); } else { response.setHeader("Content-disposition", "attachment; filename=" - + java.net.URLEncoder.encode(filename,"UTF-8")); + + java.net.URLEncoder.encode(filename, "UTF-8")); } // 循环取出流中的数据 byte[] b = new byte[1024]; @@ -73,4 +80,54 @@ public class WechatMediaController { logger.error(e.getMessage()); } } + + /** + * 图片上传 + * + * @param multiReq + * @return + * @throws Exception + */ + @PostMapping(value = "/imgUpload/{appId}", consumes = "multipart/*", headers = "content-type=multipart/form-data") + @ApiOperation("上传图片") + public ResultData imgUpload( + @PathVariable("appId") String appId, + @RequestParam("file") MultipartFile multiReq) throws Exception { + logger.info("[" + getIpAddr() + "] WechatMediaController::imgUpload"); + + String fileName = multiReq.getOriginalFilename(); + System.out.println(fileName); + + // 本地保存 + FileOutputStream fos = null; + String localImgFileName = fmUploadDir + fileName; + BufferedInputStream fs = null; + File localFile = new File(localImgFileName); + fos = new FileOutputStream(localFile); + fs = (BufferedInputStream) multiReq.getInputStream(); + byte[] buffer = new byte[1024]; + int len = 0; + while ((len = fs.read(buffer)) != -1) { + fos.write(buffer, 0, len); + } + fos.close(); + fs.close(); + + // 上传 微信 + try { + WxMpService mpService = openService.getWxOpenComponentService().getWxMpServiceByAppid(appId); + WxMediaImgUploadResult openResult = mpService.getMaterialService().mediaImgUpload(localFile); + if(openResult != null) { + return new ResultData(openResult.getUrl()); + } + } catch (WxErrorException e) { + logger.error("获取永久素材失败", e); + throw new RuntimeException(e); + } + + // 删除本地缓存 + localFile.delete(); + + return new ResultData(); + } } diff --git a/mlWechatOpen/src/main/resources/application-dev.yml b/mlWechatOpen/src/main/resources/application-dev.yml index a2edaeb..d7dbad5 100644 --- a/mlWechatOpen/src/main/resources/application-dev.yml +++ b/mlWechatOpen/src/main/resources/application-dev.yml @@ -90,6 +90,7 @@ fm: exception_emails: hupeng@iformall.com,wuguoqiang@iformall.com,gongbiao@iformall.com,luozukai@iformall.com audit_emails: wangmei@iformall.com,wuguoqiang@iformall.com,huanghui@iformall.com,sunmingming@iformall.com deploy: 1 + upload_dir: /home/test/server/uploads/ logging: level: diff --git a/mlWechatOpen/src/main/resources/application-prod.yml b/mlWechatOpen/src/main/resources/application-prod.yml index 89af6d0..48c46fc 100644 --- a/mlWechatOpen/src/main/resources/application-prod.yml +++ b/mlWechatOpen/src/main/resources/application-prod.yml @@ -84,6 +84,7 @@ fm: exception_emails: hupeng@iformall.com,wuguoqiang@iformall.com,gongbiao@iformall.com,luozukai@iformall.com,hanxueda@iformall.com audit_emails: wangmei@iformall.com,wuguoqiang@iformall.com,huanghui@iformall.com,sunmingming@iformall.com deploy: 3 + upload_dir: /home/ec2-user/server/uploads/ logging: level: diff --git a/mlWechatOpen/src/main/resources/application-test.yml b/mlWechatOpen/src/main/resources/application-test.yml index fe1ad85..f18f206 100644 --- a/mlWechatOpen/src/main/resources/application-test.yml +++ b/mlWechatOpen/src/main/resources/application-test.yml @@ -94,6 +94,7 @@ fm: exception_emails: hupeng@iformall.com,wuguoqiang@iformall.com,gongbiao@iformall.com,luozukai@iformall.com,hanxueda@iformall.com audit_emails: wangmei@iformall.com,wuguoqiang@iformall.com,huanghui@iformall.com,sunmingming@iformall.com deploy: 2 + upload_dir: /home/ec2-user/server/uploads/ ueditor: config: config.json