Commit dab0c2c4 by jscat

nyx_app, 添加二维码生成及测试程序

btw: 为什么不放到weapp上,因为目前没有一个完美的qrcode lib支持各种机型
parent 3717e29c
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
......@@ -25,6 +25,12 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Junit依赖 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!--
MyBatis-Spring-Boot-Starter will:
* Autodetect an existing DataSource.
......@@ -178,6 +184,18 @@
<scope>compile</scope>
</dependency>
<!--二维码-->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.3</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.3.3</version>
</dependency>
</dependencies>
<build>
......
package cn.com.fun.nyxkey.api.service;
package cn.com.fun.nyxkey.api.service;
......@@ -15,5 +15,6 @@ import java.util.List;
public interface Rockwell_ossService {
JSONResult Rockwell_ossServiceGetToken(String userName, String tokenName);
void testQrcode();
}
package cn.com.fun.nyxkey.api.service.impl;
package cn.com.fun.nyxkey.api.service.impl;
......@@ -13,6 +13,8 @@ import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.utils.BinaryUtil;
import com.aliyun.oss.model.MatchMode;
import com.aliyun.oss.model.PolicyConditions;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.auth.sts.AssumeRoleRequest;
import com.aliyuncs.auth.sts.AssumeRoleResponse;
......@@ -21,6 +23,11 @@ import com.aliyuncs.http.MethodType;
import com.aliyuncs.http.ProtocolType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.K;
import org.codehaus.jettison.json.JSONObject;
......@@ -33,6 +40,7 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
......@@ -285,4 +293,100 @@ public class Rockwell_ossServiceImpl implements Rockwell_ossService {
return data.toString();
}
/*
java boot 生成二维码并上传到阿里云oss
https://blog.csdn.net/tang05709/article/details/107136388?utm_medium=distribute.pc_relevant.none-task-blog-baidujs-2&spm=1001.2101.3001.4242
*/
public String ossUpload(MultipartFile file, String fileKey) throws IOException {
OSS ossClient = new OSSClientBuilder().build(aliyunOssEndpoint, aliyunOssAccessKeyID, aliyunOssAccessKeySecret);
PutObjectResult res = ossClient.putObject(aliyunOssBucket, fileKey, new ByteArrayInputStream(file. getBytes()));
ossClient.shutdown();
if (res != null) {
return fileKey;
}
return null;
}
public String ossUploadByteStream(String fileKey, byte[] bytes)
{
OSS ossClient = new OSSClientBuilder().build(aliyunOssEndpoint, aliyunOssAccessKeyID, aliyunOssAccessKeySecret);
PutObjectRequest putObjectRequest = new PutObjectRequest(aliyunOssBucket, fileKey, new ByteArrayInputStream(bytes));
PutObjectResult res = ossClient.putObject(putObjectRequest);
ossClient.shutdown();
if (res != null) {
return fileKey;
}
return null;
}
public Map<String, String> getPolicy(String dir) throws UnsupportedEncodingException {
OSS ossClient = new OSSClientBuilder().build(aliyunOssEndpoint, aliyunOssAccessKeyID, aliyunOssAccessKeySecret);
PolicyConditions policyConds = new PolicyConditions();
policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 1048576000);
policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, dir);
String endpoint = aliyunOssEndpoint; // 请填写您的 endpoint。
String bucket = aliyunOssBucket; // 请填写您的 bucketname 。
String host = "https://" + bucket + "." + endpoint; // host的格式为 bucketname.endpoint
long expireTime = 30;
long expireEndTime = System.currentTimeMillis() + expireTime * 1000;
Date expiration = new Date(expireEndTime);
String postPolicy = ossClient.generatePostPolicy(expiration, policyConds);
byte[] binaryData = postPolicy.getBytes("utf-8");
String encodedPolicy = BinaryUtil.toBase64String(binaryData);
String postSignature = ossClient.calculatePostSignature(postPolicy);
Map<String, String> respMap = new LinkedHashMap<String, String>();
respMap.put("accessid", aliyunOssAccessKeyID);
respMap.put("policy", encodedPolicy);
respMap.put("signature", postSignature);
respMap.put("dir", dir);
respMap.put("fileName", UUID.randomUUID().toString());
respMap.put("host", host);
respMap.put("expire", String.valueOf(expireEndTime / 1000));
respMap.put("callback", "");
ossClient.shutdown();
return respMap;
}
public String getBucketUrl() {
String endpoint = aliyunOssEndpoint; // 请填写您的 endpoint。
String bucket = aliyunOssBucket; // 请填写您的 bucketname 。
String host = "https://" + bucket + "." + endpoint; // host的格式为 bucketname.endpoint
return host;
}
public String getQrcode(String text, int width, int height, String filekey) {
try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
QRCodeWriter qrCodeWriter = new QRCodeWriter();
BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);
MatrixToImageWriter.writeToStream(bitMatrix, "png", os);
byte[] bytes = os.toByteArray();
String url = ossUploadByteStream(filekey, bytes);
if (url == null) {
return null;
}
return url;
} catch (WriterException | IOException e) {
e.printStackTrace();
return null;
}
}
public void testQrcode(){
String filekey = aliyunOssDir + "this_is_ohlkw.png";
String text = "this_is_ohlkw";
getQrcode(text, 200, 200, filekey);
}
}
package cn.com.fun.nyxkey.api;
package cn.com.fun.nyxkey.api;
import cn.com.fun.nyxkey.api.service.Rockwell_keyService;
import cn.com.fun.nyxkey.api.service.Rockwell_ossService;
import javafx.application.Application;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/*
javax.websocket.server.ServerContainer not available
在SpringBoot项目中集成了WebSocket,在进行单元测试的时候,出现了以下错误:
javax.websocket.server.ServerContainer not available
https://blog.csdn.net/chunjusu2447/article/details/100820520
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ServiceTest{
@Autowired
Rockwell_ossService rockwell_ossService;
@Test
public void test(){
rockwell_ossService.testQrcode();
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论