我是靠谱客的博主 儒雅蜜蜂,最近开发中收集的这篇文章主要介绍生成二维码并上传七牛云依赖导入自动生成二维码链接七牛云测试单元接口,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

依赖导入

 <!--生成二维码-->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>com.yunpian.sdk</groupId>
<artifactId>yunpian-java-sdk</artifactId>
<version>1.2.7</version>
</dependency>

自动生成二维码

package com.ujiol.common.util;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.HashMap;
import java.util.UUID;
//生成二维码
/**
* content 二维码内容
*/
public class CreateQRCode {
public static MultipartFile doCreateQRCode(String content) {
int width = 300;
//定义图片宽度
int height = 300;
//定义图片高度
String format = "png";
//定义图片格式
//定义二维码的参数
HashMap hashMap = new HashMap();
hashMap.put(EncodeHintType.CHARACTER_SET, "utf-8");
//设置编码
hashMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
//设置容错等级,等级越高,容量越小
hashMap.put(EncodeHintType.MARGIN, 2);
//设置边距
//生成二维码
try {
//生成矩阵
// 内容格式宽、高
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hashMap);
System.out.println(bitMatrix);
BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(bitMatrix);//转换成为bufferimage
//创建一个ByteArrayOutputStream
ByteArrayOutputStream os = new ByteArrayOutputStream();
//把BufferedImage写入ByteArrayOutputStream
ImageIO.write(bufferedImage, format, os);
//ByteArrayOutputStream转成InputStream
InputStream input = new ByteArrayInputStream(os.toByteArray());
//InputStream转成MultipartFile
UUID uuid = UUID.randomUUID();
MultipartFile multipartFile = new MockMultipartFile(uuid + "", input);
System.out.println(multipartFile);
return multipartFile;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}

链接七牛云

package com.ujiol.common.util;
import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;
/**
* @Description: 七牛工具
* @author liyijun
* @date 2018/03/13
*/
@Component
public class QiniuUtils {
private static final String ACCESS_KEY = "*****************************";
private static final String SECRET_KEY = "********************";
public static final String ZTCM_ADVIDEO_BUCKET = "******";
public static final String ZTCM_ADVIDEO_BUCKET_DOMAIN = "*******************";
// 构造一个带指定Zone对象的配置类
private static Configuration cfg = new Configuration(Zone.zone1());
private static UploadManager uploadManager = new UploadManager(cfg);
/**
* 创建七牛上传令牌
*
* @param bucket
*
仓库名称
* @return String 上传令牌
*/
public static String buildUptoken(String bucket) {
Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
String uptoken = auth.uploadToken(bucket);
return uptoken;
}
/**
* 上传文件
*
* @param file
*
文件对象
* @return String 文件链接(失败则返回null)
*/
public static String upload(MultipartFile file) {
if (file != null && !file.isEmpty()) {
try {
InputStream stream = file.getInputStream();
String key = UUID.randomUUID().toString() + getExtensionWithDot(file);
String token = buildUptoken(ZTCM_ADVIDEO_BUCKET);
String mime = file.getContentType();
Response response = uploadManager.put(stream, key, token, null, mime);
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
String url = ZTCM_ADVIDEO_BUCKET_DOMAIN + putRet.key;
return url;
} catch (QiniuException ex) {
ex.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* 上传图片
*
* @param file
*
文件对象
* @return String 文件链接(失败则返回null)
*/
public static String uploadImg(MultipartFile file) {
if (file != null && !file.isEmpty()) {
try {
InputStream stream = file.getInputStream();
String key = UUID.randomUUID().toString() + getExtensionWithDot(file);
String token = buildUptoken(ZTCM_ADVIDEO_BUCKET);
String mime = file.getContentType();
Response response = uploadManager.put(stream, key, token, null, mime);
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
String url = ZTCM_ADVIDEO_BUCKET + putRet.key;
return url;
} catch (QiniuException ex) {
ex.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public static String uploadImg(File file) {
if (file != null) {
try {
String dot = file.getName().substring(file.getName().lastIndexOf("."));
String key = UUID.randomUUID().toString() + dot;
String token = buildUptoken(ZTCM_ADVIDEO_BUCKET);
Response response = uploadManager.put(file, key, token);
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
String url = ZTCM_ADVIDEO_BUCKET + putRet.key;
return url;
} catch (QiniuException ex) {
ex.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* 删除图片文件
*
* @param key
*
图片Key
* @return boolean 是否成功删除
* @throws IOException
*/
public static boolean delete(String key) {
Response response = null;
try {
response = new BucketManager(Auth.create(ACCESS_KEY, SECRET_KEY), cfg).delete(ZTCM_ADVIDEO_BUCKET, key);
} catch (QiniuException e) {
e.printStackTrace();
}
return response != null ? response.isOK() : false;
}
/**
* 获取文件扩展名
*
* @param file
*
文件对象
* @return String 扩展名(无效文件返回null)
*/
public static String getExtension(MultipartFile file) {
if (file != null && !file.isEmpty()) {
String extension = "";
String fileName = file.getOriginalFilename();
if (fileName.contains(".")) {
extension = fileName.substring(fileName.lastIndexOf(".") + 1);
}
return extension;
}
return null;
}
/**
* 获取文件扩展名加Dot
*
* @param file
*
文件对象
* @return String 扩展名(无效文件返回null)
*/
public static String getExtensionWithDot(MultipartFile file) {
String extension = getExtension(file);
if (extension != null && extension.length() > 0) {
return "." + extension;
}
return extension;
}
}

测试单元接口

package com.ujiol.system.controller;
import com.ujiol.common.util.CreateQRCode;
import com.ujiol.common.util.QiniuUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.util.UUID;
@RestController
@RequestMapping("/refuel")
public class TestImage {
@RequestMapping("/zz")
public String doUpload(){
MultipartFile multipartFile = CreateQRCode.doCreateQRCode(UUID.randomUUID()+"");
String upload = QiniuUtils.upload(multipartFile);
return upload;
}
}

最后

以上就是儒雅蜜蜂为你收集整理的生成二维码并上传七牛云依赖导入自动生成二维码链接七牛云测试单元接口的全部内容,希望文章能够帮你解决生成二维码并上传七牛云依赖导入自动生成二维码链接七牛云测试单元接口所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(39)

评论列表共有 0 条评论

立即
投稿
返回
顶部