微信公众号获取token,上传素材
public static JSONObject getToken(String appId,String appSecret){ String url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appId+"&secret="+appSecret; HttpURLConnection con = null; BufferedReader bufferedReader=null; try { URL urlObj = new URL(url); // 打开连接 con = (HttpURLConnection) urlObj.openConnection(); // 设置请求通用参数 con.setRequestProperty("accept", "*/*"); con.setRequestProperty("connection", "Keep-Alive"); con.connect(); bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream())); StringBuffer buffer = new StringBuffer(); String line = null; while ((line = bufferedReader.readLine()) != null) { buffer.append(line); } String result = buffer.toString(); JSONObject resultJSON = new JSONObject(result); if (resultJSON != null) { System.out.println(resultJSON.toString()); if(null!=resultJSON.get("access_token")) { return resultJSON; }else { throw new Exception("获取微信token失败,APPID="+appId+";微信消息:"+resultJSON.getString("errmsg")+",code="+resultJSON.getString("errcode")); } } } catch (Exception e) { e.printStackTrace(); } finally { if (bufferedReader != null) { try { bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } } if(null!=con) { con.disconnect(); } } return null; } /** * 发送请求 * @Desc */ public static String connectHttpsByPost(String url,String token,String type, String KK, File file) throws Exception { if(token==null) { token = getToken("1111", "1111").get("access_token").toString(); } String path = "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=" + token; path = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=" + token; URL urlObj = new URL(path); //连接 HttpURLConnection con = (HttpURLConnection) urlObj.openConnection(); String result = null; 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=\"media\";filelength=\"" + file.length() + "\";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(); // 结尾部分 if(null!=KK) { out.write(("--" + BOUNDARY + "\r\n").getBytes()); out.write("Content-Disposition: form-data; name=\"description\";\r\n\r\n".getBytes("utf-8")); out.write(String.format("{\"title\":\"%s\", \"introduction\":\"%s\"}",file.getName(),file.getName()).getBytes("utf-8")); } 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) { buffer.append(line); } if (result == null) { result = buffer.toString(); } } catch (IOException e) { System.out.println("发送POST请求出现异常!" + e); e.printStackTrace(); } finally { if (reader != null) { reader.close(); } } return result; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
2015-07-17 Apache CXF实现WebService发布和调用