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