/**
* 获取小程序码
*/
public static InputStream getWxacodeUnlimit(String accessToken, String path, int width) {
String url = String.format(WXACODE_UNLIMIT_URL, accessToken);
String[] str = path.split("[?]");
String pagePath = str[0];
String scene = str[1];
// 接收参数json列表
JSONObject jsonParam = new JSONObject();
jsonParam.put("scene", scene);
jsonParam.put("page", pagePath);
jsonParam.put("width", width);
jsonParam.put("auto_color", false);
Map line_color = new HashMap<>();
line_color.put("r", 0);
line_color.put("g", 0);
line_color.put("b", 0);
jsonParam.put("line_color", line_color);
jsonParam.put("is_hyaline", false);
InputStream inputStream = HttpClientUtils.post(url, jsonParam);
if (inputStream == null) {
throw new UserWebException("获取二维码失败!");
}
return inputStream;
}
// 单例,只需要建立一次链接
private static OSSClient client = null;
// 配置参数
static ClientConfiguration config() {
ClientConfiguration conf = new ClientConfiguration();
conf.setMaxConnections(100);
conf.setConnectionTimeout(5000);
conf.setMaxErrorRetry(3);
conf.setSocketTimeout(2000);
return conf;
}
// 客户端单例
public static OSSClient client() {
if (client == null) {
ClientConfiguration conf = config();
client = new OSSClient(endpoint, accessKeyId, accessKeySecret, conf);
makeBucket(client, bucketName);
}
return client;
}
// 创建Bucket
private static void makeBucket(OSSClient client, String bucketName) {
boolean exist = client.doesBucketExist(bucketName);
if (exist) {
return;
}
client.createBucket(bucketName);
}
// 上传文件流
private static String uploadInputStream(InputStream is, String key) {
try {
OSSClient client = client();
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentType("image/jpg");
PutObjectRequest putObjectRequest = new PutObjectRequest(
OssUtil.bucketName, key, is, objectMetadata);
client.putObject(putObjectRequest);
// 设置这个文件地址的有效时间
Date expiration = new Date(System.currentTimeMillis() + 3600L * 1000 * 24 * 365 * 10);
return client.generatePresignedUrl(bucketName, key, expiration).toString();
} finally {
IOUtils.closeQuietly(is);
}
}
/**
* post请求
*/
public static InputStream post(String url, JSONObject jsonParam) {
InputStream inputStream = null;
RestTemplate rest = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
org.springframework.http.HttpEntity requestEntity = new org.springframework.http.HttpEntity
(jsonParam.toJSONString(), headers);
ResponseEntity entity = rest.exchange(url, HttpMethod.POST,
requestEntity, byte[].class, new Object[0]);
byte[] result = entity.getBody();
if (entity.getHeaders().getContentType().includes(MediaType.APPLICATION_JSON)) {
log.info(" - post 返回值 - [{}]", new String(result));
return null;
}
inputStream = new ByteArrayInputStream(result);
return inputStream;
}
1,如果获取小程序码返回的是错误信息(json)而不是图片流,怎么判断出来呢?
判断小程序返回的是错误信息还是图片流,通过header的content-type,如果是"application/json",说明返回的就是错误信息。
2,小程序码返回的InputStream对象是EofSensorInputStream,这个上传到阿里云不能识别
3,图片上传到阿里云后,打开链接提示下载,解决方法:
objectMetadata.setContentType("image/jpg");
以上三个问题在上面代码都有体现。