当前位置:网站首页>Wechat applet live broadcast plug-in -- get temporary files (background integrated applet live broadcast)
Wechat applet live broadcast plug-in -- get temporary files (background integrated applet live broadcast)
2022-07-27 21:52:00 【Flying wings】
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);
}
/**
* simulation form The form of the form , Upload files Write the file as an output stream to url in , Then use the input stream to get url Response
*
* @param url Request address form Forms url Address
* @param filePath The file is saved in the server path
* @return String url Response information return value
*/
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(" file does not exist ");
}
/**
* The first part
*/
URL urlObj = new URL(url);
// Connect
HttpURLConnection con = (HttpURLConnection) urlObj.openConnection();
/**
* Set key value
*/
con.setRequestMethod("POST"); // With Post Submit form by , Default get The way
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false); // post Method cannot use cache
// Set the request header information
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Charset", "UTF-8");
// Set boundaries
String BOUNDARY = "----------" + System.currentTimeMillis();
con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
// Request body information
// The first part :
StringBuilder sb = new StringBuilder();
sb.append("--"); // There must be two more lines
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");
// Get the output stream
OutputStream out = new DataOutputStream(con.getOutputStream());
// Output header
out.write(head);
// The main body of the document
// The way the file has been streamed Push to url in
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();
// The ending part
byte[] foot = ("\r\n--" + BOUNDARY + "--\r\n").getBytes("utf-8");// Define the last data separator
out.write(foot);
out.flush();
out.close();
StringBuffer buffer = new StringBuffer();
BufferedReader reader = null;
try {
// Definition BufferedReader Input stream to read URL Response
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(" send out POST Exception in request !" + e);
e.printStackTrace();
throw new IOException(" Data reading exception ");
} finally {
if (reader != null) {
reader.close();
}
}
return result;
}
}
边栏推荐
- It seems to be a bug of thread pool, but I think the source code design is unreasonable.
- Excalidraw: an easy-to-use online, free "hand drawn" virtual whiteboard tool
- 软件测试面试题:软件测试项目从什么时候开始?为什么?
- Simple manual implementation of map
- 一文读懂Plato Farm的ePLATO,以及其高溢价缘由
- Box model and element positioning
- How to deal with high concurrency deadlock?
- 美司法部增加针对华为的指控,包括窃取商业秘密等16项新罪名
- 2019Q4内存厂商营收排名:三星下滑5%,仅SK海力士、美光维持增长
- Station B collapsed. If we were the developer responsible for the repair that night
猜你喜欢
![[2022 Niuke multi School Game 2] k-link with bracket sequence I](/img/95/9d6710bfb7b9282b4a06a5f61a1f08.png)
[2022 Niuke multi School Game 2] k-link with bracket sequence I

Analysis of STL source code

@Component可以和@Bean 用在同一个类上吗?

监听服务器jar运行,及重启脚本

对象在内存中存在形式&内存分配机制

腾讯云[HiFlow】| 自动化 -------HiFlow:还在复制粘贴?

Pytest failed and rerun

聊聊 MySQL 事务二阶段提交

Talk about MySQL transaction two-phase commit

【2022牛客多校第二场】K-Link with Bracket Sequence I
随机推荐
8000字讲透OBSA原理与应用实践
day 1 - day 4
Is log4j vulnerability still widespread?
技术管理 - 一定要抓大放小
LM NAV: robot navigation method based on large models of language, vision and behavior
软件测试面试题:软件验收测试包括正式验收测试、alpha测试、beta测试三种测试?
Form of objects in memory & memory allocation mechanism
Log4j 漏洞仍普遍存在?
聊聊 MySQL 事务二阶段提交
MySQL data recovery process is based on binlog redolog undo
How to deal with high concurrency deadlock?
Tencent cloud [hiflow] | automation --------- hiflow: still copying and pasting?
Enumeration and annotation
零钱通项目(两个版本)含思路详解
华为成立全球生态发展部:全力推进HMS全球生态建设
一口气学完 Redis 集群方案
B站崩了,那晚负责修复的开发人员做了什么?
为什么要使用MQ消息中间件?这几个问题必须拿下
2021-11-05 understanding of class variables and class methods
First zhanrui 5g chip! Exposure of Hisense F50, a pure domestic 5g mobile phone: equipped with Huben T710 + chunteng 510