67,515
社区成员
发帖
与我相关
我的任务
分享
package mygirl.com.utils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.HashMap;
import java.util.Map;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.protocol.HTTP;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class CreateQrcode {
/**
* 生成带参小程序二维码
* @param access_token
* @param params
* @param filePath
* @return
* @throws Exception
*/
public static String getQrcode(String access_token, Map params,String filePath) throws Exception {
long time=System.currentTimeMillis();
File dirFile = new File(filePath+ymdString());
if (!dirFile.exists()) {
dirFile.mkdirs(); //如果目录不存在则创建此目录
}
String newFilePath = filePath+ymdString()+time+".JPG";
saveQrcode(access_token,params,newFilePath);
return newFilePath;
}
//获取当前日期 2019/10/12 建立三级目录
public static String ymdString() {
LocalDateTime ldt = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
String year = String.valueOf(ldt.getYear());
String month = String.valueOf(ldt.getMonthValue());
String day = String.valueOf(ldt.getDayOfMonth());
return year + "\\" + month + "\\" + day+"\\";
}
/**
* 二进制生成二维码图片
* @param access_token
* @param params
* @throws Exception
*/
public static void saveQrcode(String access_token, Map params,String filePath) throws Exception{
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+access_token);
httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json");
String body = JSON.toJSONString(params);
StringEntity entity;
entity = new StringEntity(body);
entity.setContentType("image/png");
httpPost.setEntity(entity);
HttpResponse response;
response = httpClient.execute(httpPost);
InputStream inputStream = response.getEntity().getContent();
// File targetFile = new File(filePath);
// if(!targetFile.exists()){
// targetFile.mkdirs();
// }
FileOutputStream out = new FileOutputStream(filePath);
byte[] buffer = new byte[8192];
int bytesRead = 0;
while((bytesRead = inputStream.read(buffer, 0, 8192)) != -1) {
out.write(buffer, 0, bytesRead);
}
out.flush();
out.close();
}
}
package com.wx.scooter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.protocol.HTTP;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wx.scooter.tool.CheckTool;
import com.wx.scooter.tool.Constant;
import com.wx.scooter.tool.HttpTool;
public class TestQR {
public static void main(String[] args) throws Exception {
//获取token
String result1 = HttpTool.get("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+Constant.NATIVE_APP_ID+"&secret="+Constant.NATIVE_APP_SECRET);
String access_token = JSONObject.parseObject(result1).getString("access_token");
if(CheckTool.isString(access_token)) {
System.out.println("token为");
System.out.println(access_token);
Map<String, Object> params = new HashMap<>();
params.put("scene", "test");
params.put("page", "pages/index/index");
params.put("width", 430);
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+access_token);
httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json");
String body = JSON.toJSONString(params);
StringEntity entity;
entity = new StringEntity(body);
entity.setContentType("image/png");
httpPost.setEntity(entity);
HttpResponse response;
response = httpClient.execute(httpPost);
InputStream inputStream = response.getEntity().getContent();
File targetFile = new File("D:\\");
if(!targetFile.exists()){
targetFile.mkdirs();
}
FileOutputStream out = new FileOutputStream("D:\\upload\\5.png");
byte[] buffer = new byte[8192];
int bytesRead = 0;
while((bytesRead = inputStream.read(buffer, 0, 8192)) != -1) {
out.write(buffer, 0, bytesRead);
}
out.flush();
out.close();
} else {
System.out.println("获取access_token错误");
}
}
}
//获取token
String result1 = HttpTool.get("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+Constant.NATIVE_APP_ID+"&secret="+Constant.NATIVE_APP_SECRET);
String access_token = JSONObject.parseObject(result1).getString("access_token");
if(CheckTool.isString(access_token)) {
System.out.println(access_token);
Map<String, Object> params = new HashMap<>();
params.put("scene", "test");
params.put("page", "pages/index/index");
params.put("width", 430);
StringHttpMessageConverter m = new StringHttpMessageConverter(Charset.forName("UTF-8"));
RestTemplate restTemplate=new RestTemplateBuilder().additionalMessageConverters(m).build();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.IMAGE_JPEG);
HttpEntity<String> entity = new HttpEntity<String>(JSON.toJSONString(params), headers);
ResponseEntity<String> result = restTemplate.exchange("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+access_token, HttpMethod.POST, entity, String.class);
try {
// System.out.println(result);
//将返回的数据保存到本地
File targetFile = new File("D:\\");
if(!targetFile.exists()){
targetFile.mkdirs();
}
FileOutputStream out = new FileOutputStream("D:\\upload\\1.png");
out.write(result.getBody().getBytes());
out.flush();
out.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
System.out.println("获取access_token错误");
}
[/quote]
URL url = null;
HttpURLConnection connection = null;
try {
url = new URL( "");
System.out.println(url);
connection = (HttpURLConnection) url.openConnection();// 新建连接实例
connection.setConnectTimeout(2000);// 设置连接超时时间,单位毫秒
connection.setReadTimeout(2000);// 设置读取数据超时时间,单位毫秒
connection.setDoInput(true);// 是否打开输出 true|false
connection.setRequestMethod("POST");// 提交方法POST|GET
connection.setUseCaches(false);// 是否缓存true|false
connection.connect();// 打开连接端口
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
reader.close();
System.out.println(buffer.toString());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();// 关闭连接
}
}
你试试这种写法,我的微信没有授权,所以图片那个请求过不去~你可以输出下看看他的返回内容格式[/quote]
问一下,这种方法怎么传参?//获取token
String result1 = HttpTool.get("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+Constant.NATIVE_APP_ID+"&secret="+Constant.NATIVE_APP_SECRET);
String access_token = JSONObject.parseObject(result1).getString("access_token");
if(CheckTool.isString(access_token)) {
System.out.println(access_token);
Map<String, Object> params = new HashMap<>();
params.put("scene", "test");
params.put("page", "pages/index/index");
params.put("width", 430);
StringHttpMessageConverter m = new StringHttpMessageConverter(Charset.forName("UTF-8"));
RestTemplate restTemplate=new RestTemplateBuilder().additionalMessageConverters(m).build();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.IMAGE_JPEG);
HttpEntity<String> entity = new HttpEntity<String>(JSON.toJSONString(params), headers);
ResponseEntity<String> result = restTemplate.exchange("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+access_token, HttpMethod.POST, entity, String.class);
try {
// System.out.println(result);
//将返回的数据保存到本地
File targetFile = new File("D:\\");
if(!targetFile.exists()){
targetFile.mkdirs();
}
FileOutputStream out = new FileOutputStream("D:\\upload\\1.png");
out.write(result.getBody().getBytes());
out.flush();
out.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
System.out.println("获取access_token错误");
}
[/quote]
URL url = null;
HttpURLConnection connection = null;
try {
url = new URL( "");
System.out.println(url);
connection = (HttpURLConnection) url.openConnection();// 新建连接实例
connection.setConnectTimeout(2000);// 设置连接超时时间,单位毫秒
connection.setReadTimeout(2000);// 设置读取数据超时时间,单位毫秒
connection.setDoInput(true);// 是否打开输出 true|false
connection.setRequestMethod("POST");// 提交方法POST|GET
connection.setUseCaches(false);// 是否缓存true|false
connection.connect();// 打开连接端口
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
reader.close();
System.out.println(buffer.toString());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();// 关闭连接
}
}
你试试这种写法,我的微信没有授权,所以图片那个请求过不去~你可以输出下看看他的返回内容格式//获取token
String result1 = HttpTool.get("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+Constant.NATIVE_APP_ID+"&secret="+Constant.NATIVE_APP_SECRET);
String access_token = JSONObject.parseObject(result1).getString("access_token");
if(CheckTool.isString(access_token)) {
System.out.println(access_token);
Map<String, Object> params = new HashMap<>();
params.put("scene", "test");
params.put("page", "pages/index/index");
params.put("width", 430);
StringHttpMessageConverter m = new StringHttpMessageConverter(Charset.forName("UTF-8"));
RestTemplate restTemplate=new RestTemplateBuilder().additionalMessageConverters(m).build();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.IMAGE_JPEG);
HttpEntity<String> entity = new HttpEntity<String>(JSON.toJSONString(params), headers);
ResponseEntity<String> result = restTemplate.exchange("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+access_token, HttpMethod.POST, entity, String.class);
try {
// System.out.println(result);
//将返回的数据保存到本地
File targetFile = new File("D:\\");
if(!targetFile.exists()){
targetFile.mkdirs();
}
FileOutputStream out = new FileOutputStream("D:\\upload\\1.png");
out.write(result.getBody().getBytes());
out.flush();
out.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
System.out.println("获取access_token错误");
}