小程序向java后台发送图片_微信小程序在后台如何将二进制流转换成图片

我在前端请求了小程序码返回的是一堆乱码,

java不太熟网上找了一个方法可以将二进制流和图片互转,但是从微信小程序码接口获取的数据用这个方法无法获取正确的图片,不知道哪里有问题;

有没有只在前端就能获取小程序码的方法

@RequestMapping("/qrTest")

public Map qrTest(Long hotelId, String appId, String token) {

RestTemplate rest = new RestTemplate();

InputStream inputStream = null;

OutputStream outputStream = null;

try {

String url = "https://api.weixin.qq.com/wxa/getwxacode?access_token=" + token;

Map param = new HashMap<>();

param.put("page", "pages/home/home");

param.put("width", 430);

param.put("auto_color", false);

Map line_color = new HashMap<>();

line_color.put("r", 0);

line_color.put("g", 0);

line_color.put("b", 0);

param.put("line_color", line_color);

System.out.println("调用生成微信URL接口传参:" + param);

// MultiValueMap headers = new LinkedMultiValueMap<>();

HttpHeaders headers = new HttpHeaders();

headers.setContentType(MediaType.APPLICATION_JSON);

HttpEntity requestEntity = new HttpEntity(param, headers);

ResponseEntity entity = rest.exchange(url, HttpMethod.POST, requestEntity, byte[].class, new Object[0]);

System.out.println("调用小程序生成微信永久小程序码URL接口返回结果:" + entity.getBody());

byte[] result = entity.getBody();

System.out.println("\r\nold:" + Base64.encodeBase64String(result));

inputStream = new ByteArrayInputStream(result);

File file = new File("f:/abc.jpg");

if (!file.exists()) {

file.createNewFile();

}

outputStream = new FileOutputStream(file);

int len = 0;

byte[] buf = new byte[1024];

while ((len = inputStream.read(buf, 0, 1024)) != -1) {

outputStream.write(buf, 0, len);

}

outputStream.flush();

} catch (Exception e) {

System.out.println("调用小程序生成微信永久小程序码URL接口异常");

} finally {

if (inputStream != null) {

try {

inputStream.close();

} catch (IOException e) {

e.printStackTrace();

}

}

if (outputStream != null) {

try {

outputStream.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

return null;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值