微信媒体工具类(临时图片上传)
This commit is contained in:
parent
273d0abbc7
commit
cd60eba7a4
@ -1,5 +1,7 @@
|
||||
package cn.lili.modules.broadcast.serviceimpl;
|
||||
|
||||
import cn.lili.common.enums.ResultCode;
|
||||
import cn.lili.common.exception.ServiceException;
|
||||
import cn.lili.common.security.context.UserContext;
|
||||
import cn.lili.modules.broadcast.entity.dos.Studio;
|
||||
import cn.lili.modules.broadcast.entity.dos.StudioCommodity;
|
||||
@ -28,11 +30,17 @@ public class StudioServiceImpl extends ServiceImpl<StudioMapper, Studio> implem
|
||||
|
||||
@Override
|
||||
public Boolean create(Studio studio) {
|
||||
//创建小程序直播
|
||||
Integer roomId=wechatLivePlayerUtil.create(studio);
|
||||
studio.setRoomId(roomId);
|
||||
studio.setStoreId(UserContext.getCurrentUser().getStoreId());
|
||||
return this.save(studio);
|
||||
try {
|
||||
//创建小程序直播
|
||||
Integer roomId=wechatLivePlayerUtil.create(studio);
|
||||
studio.setRoomId(roomId);
|
||||
studio.setStoreId(UserContext.getCurrentUser().getStoreId());
|
||||
return this.save(studio);
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
throw new ServiceException(ResultCode.ERROR);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -28,6 +28,8 @@ public class WechatLivePlayerUtil {
|
||||
|
||||
@Autowired
|
||||
private WechatAccessTokenUtil wechatAccessTokenUtil;
|
||||
@Autowired
|
||||
private WechatMediaUtil wechatMediaUtil;
|
||||
|
||||
/**
|
||||
* 创建小程序直播间
|
||||
@ -35,7 +37,7 @@ public class WechatLivePlayerUtil {
|
||||
* @param studio 小程序直播
|
||||
* @return 房间ID
|
||||
*/
|
||||
public Integer create(Studio studio) {
|
||||
public Integer create(Studio studio) throws Exception{
|
||||
//获取token
|
||||
String token = wechatAccessTokenUtil.cgiAccessToken(ClientTypeEnum.WECHAT_MP);
|
||||
//发送url
|
||||
@ -43,12 +45,11 @@ public class WechatLivePlayerUtil {
|
||||
|
||||
Map<String, String> map = new HashMap<>();
|
||||
// 背景图
|
||||
map.put("coverImg", studio.getCoverImg());
|
||||
map.put("coverImg", wechatMediaUtil.uploadMedia(token,"image",studio.getCoverImg()));
|
||||
// 分享图
|
||||
map.put("shareImg", studio.getShareImg());
|
||||
map.put("shareImg", wechatMediaUtil.uploadMedia(token,"image",studio.getShareImg()));
|
||||
// 购物直播频道封面图
|
||||
map.put("feedsImg", studio.getFeedsImg());
|
||||
|
||||
map.put("feedsImg", wechatMediaUtil.uploadMedia(token,"image",studio.getFeedsImg()));
|
||||
// 直播间名字
|
||||
map.put("name", studio.getName());
|
||||
// 直播计划开始时间
|
||||
|
@ -0,0 +1,123 @@
|
||||
package cn.lili.modules.broadcast.util;
|
||||
|
||||
import cn.hutool.json.JSONObject;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 微信媒体工具
|
||||
*
|
||||
* @author Bulbasaur
|
||||
* @date: 2021/5/19 8:02 下午
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class WechatMediaUtil {
|
||||
|
||||
/**
|
||||
* 上传多媒体数据到微信服务器
|
||||
* @param accessToken 从微信获取到的access_token
|
||||
* @param mediaFileUrl 来自网络上面的媒体文件地址
|
||||
* @return
|
||||
*/
|
||||
public String uploadMedia(String accessToken, String type, String mediaFileUrl) {
|
||||
/*
|
||||
* 上传媒体文件到微信服务器需要请求的地址
|
||||
*/
|
||||
String MEDIA_URL = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE";
|
||||
|
||||
StringBuffer resultStr = null;
|
||||
//拼装url地址
|
||||
String mediaStr = MEDIA_URL.replace("ACCESS_TOKEN", accessToken).replace("TYPE", type);
|
||||
URL mediaUrl;
|
||||
try {
|
||||
String boundary = "----WebKitFormBoundaryOYXo8heIv9pgpGjT";
|
||||
URL url = new URL(mediaStr);
|
||||
HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();
|
||||
//让输入输出流开启
|
||||
urlConn.setDoInput(true);
|
||||
urlConn.setDoOutput(true);
|
||||
//使用post方式请求的时候必须关闭缓存
|
||||
urlConn.setUseCaches(false);
|
||||
//设置请求头的Content-Type属性
|
||||
urlConn.setRequestProperty("Content-Type", "multipart/form-data; boundary="+boundary);
|
||||
urlConn.setRequestMethod("POST");
|
||||
//获取输出流,使用输出流拼接请求体
|
||||
OutputStream out = urlConn.getOutputStream();
|
||||
|
||||
//读取文件的数据,构建一个GET请求,然后读取指定地址中的数据
|
||||
mediaUrl = new URL(mediaFileUrl);
|
||||
HttpURLConnection mediaConn = (HttpURLConnection)mediaUrl.openConnection();
|
||||
//设置请求方式
|
||||
mediaConn.setRequestMethod("GET");
|
||||
//设置可以打开输入流
|
||||
mediaConn.setDoInput(true);
|
||||
//获取传输的数据类型
|
||||
String contentType = mediaConn.getHeaderField("Content-Type");
|
||||
//将获取大到的类型转换成扩展名
|
||||
String fileExt = judgeType(contentType);
|
||||
//获取输入流,从mediaURL里面读取数据
|
||||
InputStream in = mediaConn.getInputStream();
|
||||
BufferedInputStream bufferedIn = new BufferedInputStream(in);
|
||||
//数据读取到这个数组里面
|
||||
byte[] bytes = new byte[1024];
|
||||
int size = 0;
|
||||
//使用outputStream流输出信息到请求体当中去
|
||||
out.write(("--"+boundary+"\r\n").getBytes());
|
||||
out.write(("Content-Disposition: form-data; name=\"media\";\r\n"
|
||||
+ "filename=\""+(new Date().getTime())+fileExt+"\"\r\n"
|
||||
+ "Content-Type: "+contentType+"\r\n\r\n").getBytes());
|
||||
while( (size = bufferedIn.read(bytes)) != -1) {
|
||||
out.write(bytes, 0, size);
|
||||
}
|
||||
//切记,这里的换行符不能少,否则将会报41005错误
|
||||
out.write(("\r\n--"+boundary+"--\r\n").getBytes());
|
||||
|
||||
bufferedIn.close();
|
||||
in.close();
|
||||
mediaConn.disconnect();
|
||||
|
||||
InputStream resultIn = urlConn.getInputStream();
|
||||
InputStreamReader reader = new InputStreamReader(resultIn);
|
||||
BufferedReader bufferedReader = new BufferedReader(reader);
|
||||
String tempStr = null;
|
||||
resultStr = new StringBuffer();
|
||||
while((tempStr = bufferedReader.readLine()) != null) {
|
||||
resultStr.append(tempStr);
|
||||
}
|
||||
bufferedReader.close();
|
||||
reader.close();
|
||||
resultIn.close();
|
||||
urlConn.disconnect();
|
||||
} catch (MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
JSONObject jsonObject=new JSONObject(resultStr.toString());
|
||||
return jsonObject.get("media_id").toString();
|
||||
}
|
||||
/**
|
||||
* 通过传过来的contentType判断是哪一种类型
|
||||
* @param contentType 获取来自连接的contentType
|
||||
* @return
|
||||
*/
|
||||
public String judgeType(String contentType) {
|
||||
String fileExt = "";
|
||||
switch (contentType){
|
||||
case "image/png":
|
||||
fileExt = ".png";
|
||||
break;
|
||||
case "image/jpeg":
|
||||
fileExt = ".jpg";
|
||||
break;
|
||||
}
|
||||
return fileExt;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user