|
- 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;
- import io.swagger.annotations.ApiImplicitParam;
- 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.*;
- 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
- */
- @Controller
- @RequestMapping("/material")
- @Api(description = "微信第三方开放平台素材文件")
- public class WechatMediaController extends BaseController {
- private final Logger logger = LoggerFactory.getLogger(getClass());
- @Autowired
- private String fmUploadDir;
- @Autowired
- private FmOpenService openService;
-
- @Autowired
- private WxAuthorizerInfoService authorizerInfoService;
-
- @ApiOperation(value = "获取永久素材", notes = "")
- @GetMapping("/getMedia")
- @ApiImplicitParams({
- @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 mpService = openService.getWxOpenComponentService().getWxMpServiceByAppid(appId);
- inputStream = mpService.getMaterialService().materialImageOrVoiceDownload(mediaId);
- } catch (WxErrorException e) {
- logger.error("获取永久素材失败", e);
- throw new RuntimeException(e);
- }
-
- try {
- response.reset();
- response.setContentType("application/octet-stream");
- String agent = req.getHeader("user-agent");
- if (agent.contains("Firefox")) {
- response.setHeader("Content-disposition",
- "attachment; filename="
- + new String(filename.getBytes("GB2312"), "ISO-8859-1"));
- } else {
- response.setHeader("Content-disposition",
- "attachment; filename="
- + java.net.URLEncoder.encode(filename, "UTF-8"));
- }
- // 循环取出流中的数据
- byte[] b = new byte[1024];
- int len;
- while ((len = inputStream.read(b)) > 0)
- response.getOutputStream().write(b, 0, len);
- inputStream.close();
- } catch (Exception e) {
- 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();
- }
- }
|