package cn.huge.module.wechat.miniapp.controller; import cn.binarywang.wx.miniapp.constant.WxMaConstants; import cn.huge.module.wechat.miniapp.service.WeixinMaService; import com.google.common.collect.Lists; import com.google.common.io.Files; import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; import me.chanjar.weixin.common.error.WxErrorException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; import org.springframework.web.multipart.commons.CommonsMultipartResolver; import javax.servlet.http.HttpServletRequest; import java.io.File; import java.io.IOException; import java.util.Iterator; import java.util.List; /** * @title: 小程序临时素材接口 * @description: 小程序临时素材接口 * @company: hugeinfo * @author: liyj * @time: 2021/02/02 * @version: 1.0.0 */ @RestController @RequestMapping("/wechat/ma/media") public class WxMaMediaController { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired private WeixinMaService service; /** * 上传临时素材 * * @return 素材的media_id列表,实际上如果有的话,只会有一个 */ @PostMapping("/upload") public List uploadMedia(HttpServletRequest request) throws WxErrorException { CommonsMultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext()); if (!resolver.isMultipart(request)) { return Lists.newArrayList(); } MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request; Iterator it = multiRequest.getFileNames(); List result = Lists.newArrayList(); while (it.hasNext()) { try { MultipartFile file = multiRequest.getFile(it.next()); File newFile = new File(Files.createTempDir(), file.getOriginalFilename()); this.logger.info("filePath is :" + newFile.toString()); file.transferTo(newFile); WxMediaUploadResult uploadResult = this.service.getMediaService().uploadMedia(WxMaConstants.KefuMsgType.IMAGE, newFile); this.logger.info("media_id : " + uploadResult.getMediaId()); result.add(uploadResult.getMediaId()); } catch (IOException e) { this.logger.error(e.getMessage(), e); } } return result; } /** * 下载临时素材 */ @GetMapping("/download/{mediaId}") public File getMedia(@PathVariable String mediaId) throws WxErrorException { return this.service.getMediaService().getMedia(mediaId); } } /** * -------------------_ooOoo_------------------- * ------------------o8888888o------------------ * ------------------88" . "88------------------ * ------------------(| -_- |)------------------ * ------------------O\ = /O------------------ * ---------------____/`---'\____--------------- * -------------.' \\| |// `.------------- * ------------/ \\||| : |||// \------------ * -----------/ _||||| -:- |||||- \----------- * -----------| | \\\ - /// | |----------- * -----------| \_| ''\---/'' | |----------- * -----------\ .-\__ `-` ___/-. /----------- * ---------___`. .' /--.--\ `. . __---------- * ------."" '< `.___\_<|>_/___.' >'"".------- * -----| | : `- \`.;`\ _ /`;.`/ - ` : | |----- * -----\ \ `-. \_ __\ /__ _/ .-` / /----- * ======`-.____`-.___\_____/___.-`____.-'====== * -------------------`=---=' * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ * ---------佛祖保佑---hugeinfo---永无BUG---------- */