当前位置:网站首页>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;
}
}
边栏推荐
- Why use MQ message oriented middleware? These questions must be solved
- Software test interview questions: the steps to write test cases by drawing cause and effect diagrams are___ And transforming the cause and effect diagram into a state diagram in five steps. What are
- [day_4-review, basic concepts of objects and arrays - 1]
- Microsoft store can't download apps, vs2019 can't download plug-ins solution
- Log4j 漏洞仍普遍存在?
- Zibbix installation and deployment
- 为什么服务端程序都需要先 listen 一下
- [2022 Niuke multi School Game 2] k-link with bracket sequence I
- 声扬科技正式上线闻声远程声纹健康回访服务系统!
- Can JVM tuning be done with single core CPU and 1G memory?
猜你喜欢

MySQL execution process and order

JVM-内存模型 面试总结

一口气学完 Redis 集群方案

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

如何实现一个好的知识管理系统?

一篇文章带你走进pycharm的世界----别再问我pycharm的安装和环境配置了!!!

8000字讲透OBSA原理与应用实践

Finish learning redis cluster solution at one go

Zibbix installation and deployment

Station B collapsed. If we were the developer responsible for the repair that night
随机推荐
In addition to "adding machines", in fact, your micro service can be optimized like this
Technical practice behind bloom model: how to refine 176billion parameter model?
How can anyone ask how MySQL archives data?
@RequestParam注解的详细介绍
2021-11-05 understand main method syntax, code block and final keyword
For 3nm and below processes, ASML new generation EUV lithography machine exposure
聊聊 MySQL 事务二阶段提交
递归/回溯刷题(上)
Recursion / backtracking (Part 1)
【2022牛客多校第二场】K-Link with Bracket Sequence I
An article takes you into the world of pycharm - stop asking me about pycharm installation and environment configuration!!!
Samsung's most advanced EUV production line has been put into operation: the 7Nm capacity this year will be three times that of last year
QT take out the input box string, lineedit
MySQL execution process and order
Ziguang zhanrui: dozens of 5g terminals based on chunteng 510 will be commercially available in 2020
软件测试面试题:系统测试的策略有多少种?
简单手动实现Map
Software testing interview question: what aspects should be considered when designing test cases, that is, which aspects should different test cases be tested for?
美司法部增加针对华为的指控,包括窃取商业秘密等16项新罪名
2021-11-05 understanding of class variables and class methods