首先发个链接下个demo,链接https://github.com/Wechat-Group/weixin-java-tools/wiki/%E5%85%AC%E4%BC%97%E5%8F%B7%E5%BC%80%E5%8F%91%E6%96%87%E6%A1%A3
进入DEMO代码下载 这样后来开发会省好多代码
点进去下载
点击clone or download
然后POM配置微信公众号依赖
<!--公众号相关--> <dependency> <groupId>com.github.binarywang</groupId> <artifactId>weixin-java-mp</artifactId> <version>2.9.0</version> </dependency>
下面开始开发
参数 File传图片 type 类型
下面上代码
@PostMapping("/creat/upload") @ApiOperation("上传其他永久素材") public Message uploadMater( MultipartFile file, @RequestParam String type ){ WxMpMaterial material = new WxMpMaterial(); WxMpMaterialUploadResult result; File file1 =this.filetoMultipartFile(file); material.setFile(file1); material.setName(file.getName()); material.setVideoTitle(""); material.setVideoIntroduction(""); try { result = wxMpService.getMaterialService().materialFileUpload(type, material); File del = new File(file1.toURI()); del.delete(); result.getMediaId(); logger.info(result.getUrl()); return MessageUtil.success(result); } catch (WxErrorException e) { e.printStackTrace(); } return MessageUtil.success(); }
参数MultipartFile file 用来接收前端传的图片
下面是转换MultipartFile 转换File的接口
public static void inputStreamToFile(InputStream ins,File file) { try { OutputStream os = new FileOutputStream(file); int bytesRead = 0; byte[] buffer = new byte[8192]; while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) { os.write(buffer, 0, bytesRead); } os.close(); ins.close(); } catch (Exception e) { e.printStackTrace(); } } public File filetoMultipartFile(MultipartFile file){ File f = null; try { InputStream ins = file.getInputStream(); f = new File(file.getOriginalFilename()); inputStreamToFile(ins, f); } catch (IOException e){ e.printStackTrace(); } return f; }
网上都是内容太繁琐 还不太好使 下载个Demo好多东西直接用 微信帮你处理 逼格高 还容易