当前位置:网站首页>微信小程序直播插件--获取临时文件(后台集成小程序直播)
微信小程序直播插件--获取临时文件(后台集成小程序直播)
2022-07-15 13:52:00 【飞飞翼】
package com.ruoyi.web.controller.api.liveimg_wx;
import com.fasterxml.jackson.core.JsonParser;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HttpContext;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
public class utils {
static String token="58_AzlN4Kc7ZcvNb6628Hwe7a8HeQD6xI2rW4xB16x4eTNfV8CTBop0_oGuWTHCH3m95voDRkhVTbfy5QTizijiZicKVI4X8nzMUFHicbYsT-_oQrLG-Jc3rRa8Ia2jPhCsWhJ9ooO4sqY0hcu-YBYhABALOL";
public static void main(String[] args) throws IOException {
String filePath = "D:\\Pictures\\logo.jpg";
String sendUrl = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token="+token+"&type=image";
String result = null;
utils fileUpload = new utils();
result = fileUpload.send(sendUrl, filePath);
System.out.println(result);
}
/**
* 模拟form表单的形式 ,上传文件 以输出流的形式把文件写入到url中,然后用输入流来获取url的响应
*
* @param url 请求地址 form表单url地址
* @param filePath 文件在服务器保存路径
* @return String url的响应信息返回值
*/
public String send(String url, String filePath) throws IOException {
String result = null;
File file = new File(filePath);
if (!file.exists() || !file.isFile()) {
throw new IOException("文件不存在");
}
/**
* 第一部分
*/
URL urlObj = new URL(url);
// 连接
HttpURLConnection con = (HttpURLConnection) urlObj.openConnection();
/**
* 设置关键值
*/
con.setRequestMethod("POST"); // 以Post方式提交表单,默认get方式
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false); // post方式不能使用缓存
// 设置请求头信息
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Charset", "UTF-8");
// 设置边界
String BOUNDARY = "----------" + System.currentTimeMillis();
con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
// 请求正文信息
// 第一部分:
StringBuilder sb = new StringBuilder();
sb.append("--"); // 必须多两道线
sb.append(BOUNDARY);
sb.append("\r\n");
sb.append("Content-Disposition: form-data;name=\"file\";filename=\""
+ file.getName() + "\"\r\n");
sb.append("Content-Type:application/octet-stream\r\n\r\n");
byte[] head = sb.toString().getBytes("utf-8");
// 获得输出流
OutputStream out = new DataOutputStream(con.getOutputStream());
// 输出表头
out.write(head);
// 文件正文部分
// 把文件已流文件的方式 推入到url中
DataInputStream in = new DataInputStream(new FileInputStream(file));
int bytes = 0;
byte[] bufferOut = new byte[1024];
while ((bytes = in.read(bufferOut)) != -1) {
out.write(bufferOut, 0, bytes);
}
in.close();
// 结尾部分
byte[] foot = ("\r\n--" + BOUNDARY + "--\r\n").getBytes("utf-8");// 定义最后数据分隔线
out.write(foot);
out.flush();
out.close();
StringBuffer buffer = new StringBuffer();
BufferedReader reader = null;
try {
// 定义BufferedReader输入流来读取URL的响应
reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
//System.out.println(line);
buffer.append(line);
}
if (result == null) {
result = buffer.toString();
}
} catch (IOException e) {
System.out.println("发送POST请求出现异常!" + e);
e.printStackTrace();
throw new IOException("数据读取异常");
} finally {
if (reader != null) {
reader.close();
}
}
return result;
}
}
边栏推荐
- Responsive user login form
- General business general waste packaging cases
- js图片裁剪及压缩整合插件
- leetcode 148. 排序链表
- Gradle packaging exclusion dependency exclusion file
- ReentranLock及源码解析(学思想,一步一步点进源码)
- Among the top 50 intelligent operation and maintenance enterprises in 2022, Borui data strength was selected
- MySQL8.0学习记录18 - Tablespaces
- mysql 传入List<String>进行查询的方法
- JS effect - table interlaced color change
猜你喜欢

OSPF knowledge point 1

模糊测试的简介

Among the top 50 intelligent operation and maintenance enterprises in 2022, Borui data strength was selected

从源码探究双亲委派机制

JVM garbage collection -- how to determine whether an object is garbage

MGRE comprehensive experiment

OSPF experiment in mGRE environment

Processes in Oracle

MGRE and OSPF

盘点波卡生态潜力项目 | 跨链特性促进多赛道繁荣
随机推荐
Midnight - customized version of Excel office automation
MGRE comprehensive experiment
关于hash和history的区别和使用
JVM垃圾收集之——怎样判定一个对象是不是垃圾
Very comprehensive use of ireport
强化学习(Q-Learning)与路径搜索(A*)的联系
[openfoam pre school preparation 3 - install openfoam-v8]
OSPF - irregular area, LSA
上传图片/文件
flink的测试sql怎么测试呢,不能每次都使用jar测试吧,那么sqk-client就来了
在 Polkadot 中进行创建的三种方式 —— 平行链、平行线程、智能合约
备忘录模式 - Unity
技术分享 | 常见接口协议解析
减淡背景的注册+登录表单页面
Application of Tupu web visualization engine in simulation analysis field
创意丝带样式登录页面
Flat rider registration form
手机怎样买股票开户流程 开户安全吗
【第二十二题】地下城与勇士(北理工/北京理工大学/程序设计方法与实践/小学期 )
Tab plus swiper long list scrolling