上传永久素材详细步骤,自写 亲测可用

首先发个链接下个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好多东西直接用 微信帮你处理  逼格高  还容易

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值