提交 d63fe19c authored 作者: chenxinchang's avatar chenxinchang

--no commit message

上级 451f878b
package com.zrqx.core.enums;
public class EnumUtil {
}
package com.zrqx.core.model.file;
import java.util.Date;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
public class FileInfo {
@Id
@GeneratedValue(generator="JDBC")
@ApiModelProperty(value="文件ID")
private Integer id;
@ApiModelProperty(value="文件名")
private String fileName;
@ApiModelProperty(value="内容类型")
private String contentType;
@ApiModelProperty(value="后缀名")
private String suffixName;
@ApiModelProperty(value="原文件名")
private String originalFileName;
@ApiModelProperty(value="存储路径")
private String path;
@ApiModelProperty(value="文件大小")
private Long size;
@ApiModelProperty(value="文件时长")
private Long time;
@ApiModelProperty(value="创建时间")
private Date createTime;
}
package com.zrqx.core.util.datatype;
import java.util.Random;
/**
* TODO 功能整合到StringUtils中
* @author lpf
* @date 2018年12月26日下午5:26:40
*/
public class CreateRandomCode {
public static String create(){
String chars = "ABCDEFGHIGKLMNOPQRSTUVWXYZ";
char r1=chars.charAt((int)(Math.random() * 26));
char r2=chars.charAt((int)(Math.random() * 26));
StringBuilder sb=new StringBuilder();
Random random=new Random();
for(int i=0;i<13;i++){
sb.append(random.nextInt(10));
}
String str=String.valueOf(r1)+sb.toString();
return str;
}
}
package com.zrqx.core.util;
package com.zrqx.core.util.datatype;
import java.util.Random;
import com.zrqx.core.util.datatype.UUIDUtil;
/**
* 随意有规则的字符串
* @author Administrator
......
package com.zrqx.core.util;
package com.zrqx.core.util.datatype;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
......
package com.zrqx.core.util.erweima;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Hashtable;
import javax.imageio.ImageIO;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.ReaderException;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeReader;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
/**
* 二维码生成和读的工具类
*
*/
public class QrCodeCreateUtil {
private static String TYPE = "JPEG";
private static int SIZE = 300;
/**
* 生成包含字符串信息的二维码图片
*
* @param outputStream
* 文件输出流路径
* @param content
* 二维码携带信息
* @param qrCodeSize
* 二维码图片大小
* @param imageFormat
* 二维码的格式
* @throws WriterException
* @throws IOException
*/
public static boolean createQrCode(OutputStream outputStream, String content, int qrCodeSize, String imageFormat)
throws WriterException, IOException {
if(qrCodeSize < 130){
qrCodeSize = 130;
}
// 设置二维码纠错级别MAP
Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); // 矫错级别
QRCodeWriter qrCodeWriter = new QRCodeWriter();
// 创建比特矩阵(位矩阵)的QR码编码的字符串
BitMatrix byteMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, qrCodeSize, qrCodeSize, hintMap);
// 使BufferedImage勾画QRCode (matrixWidth 是行二维码像素点)
int matrixWidth = byteMatrix.getWidth();
BufferedImage image = new BufferedImage(matrixWidth - 50, matrixWidth - 50, BufferedImage.TYPE_INT_RGB);
image.createGraphics();
Graphics2D graphics = (Graphics2D) image.getGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, matrixWidth, matrixWidth);
// 使用比特矩阵画并保存图像
graphics.setColor(Color.BLACK);
for (int i = 0; i < matrixWidth; i++) {
for (int j = 0; j < matrixWidth; j++) {
if (byteMatrix.get(i, j)) {
graphics.fillRect(i-25, j-25, 1, 1);
}
}
}
boolean flag = ImageIO.write(image, imageFormat, outputStream);
outputStream.close();
return flag;
}
/**
* 生成包含字符串信息的二维码图片
*
* @param outputStream
* 文件输出流路径
* @param content
* 二维码携带信息
* @param qrCodeSize
* 二维码图片大小
* @throws WriterException
* @throws IOException
*/
public static boolean createQrCode(OutputStream outputStream, String content, int qrCodeSize)
throws WriterException, IOException {
return createQrCode(outputStream, content, qrCodeSize, TYPE);
}
/**
* 生成包含字符串信息的二维码图片
*
* @param outputStream
* 文件输出流路径
* @param content
* 二维码携带信息
* @param imageFormat
* 二维码的格式
* @throws WriterException
* @throws IOException
*/
public static boolean createQrCode(OutputStream outputStream, String content, String imageFormat)
throws WriterException, IOException {
return createQrCode(outputStream, content, SIZE, imageFormat);
}
/**
* 生成包含字符串信息的二维码图片
*
* @param outputStream
* 文件输出流路径
* @param content
* 二维码携带信息
* @throws WriterException
* @throws IOException
*/
public static boolean createQrCode(OutputStream outputStream, String content) throws WriterException, IOException {
return createQrCode(outputStream, content, SIZE, TYPE);
}
/**
* 生成包含字符串信息的二维码图片
*
* @param filePath
* 文件输出路径
* @param content
* 二维码携带信息
* @throws WriterException
* @throws IOException
*/
public static boolean createQrCode(String filePath, String content) throws WriterException, IOException {
return createQrCode(new FileOutputStream(new File(filePath)), content, SIZE, TYPE);
}
/**
* 生成包含字符串信息的二维码图片
*
* @param file
* 文件
* @param content
* 二维码携带信息
* @throws WriterException
* @throws IOException
*/
public static boolean createQrCode(File file, String content) throws WriterException, IOException {
return createQrCode(new FileOutputStream(file), content, SIZE, TYPE);
}
/**
* 读二维码并输出携带的信息
*/
public static String readQrCode(InputStream inputStream) throws IOException {
// 从输入流中获取字符串信息
BufferedImage image = ImageIO.read(inputStream);
// 将图像转换为二进制位图源
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
QRCodeReader reader = new QRCodeReader();
Result result = null;
try {
result = reader.decode(bitmap);
return result.getText();
} catch (ReaderException e) {
e.printStackTrace();
}
return null;
}
/**
* 测试代码
*
* @throws WriterException
*/
public static void main(String[] args) throws IOException, WriterException {
createQrCode(new FileOutputStream(new File("d:\\qrcode.jpg")), "WE11111111111111111111111111111111111111111111");
readQrCode(new FileInputStream(new File("d:\\qrcode.jpg")));
}
}
\ No newline at end of file
package com.zrqx.core.util.zip;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import com.zrqx.core.model.file.FileInfo;
import com.zrqx.core.util.datatype.UUIDUtil;
public class ZipUtil {
/**
*
* @param zipFileName
* 目的地Zip文件
* @param sourceFileName
* 源文件(待压缩的文件或文件夹)
* @throws Exception
*/
public static void zip(String zipFileName, String sourceFileName) throws Exception {
// File zipFile = new File(zipFileName);
System.out.println("压缩中...");
// 创建zip输出流
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFileName)));
File sourceFile = new File(sourceFileName);
// 调用函数
compress(out, sourceFile, sourceFile.getName());
out.close();
System.out.println("压缩完成");
}
/**
*
* @param rootPath 文件要压缩到的文件夹
* @param list 要压缩的文件对象
* @return String 文件路径
* @throws Exception
*/
public static String zip(String rootPath, List<FileInfo> list) throws Exception {
// File zipFile = new File(zipFileName);
System.out.println("压缩中...");
String zipFileName = rootPath + UUIDUtil.getUUID() + ".zip";
// 创建zip输出流
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFileName)));
for (FileInfo file : list) {
String filePath = rootPath + file.getPath() + "/" + file.getFileName() + file.getSuffixName();
File sourceFile = new File(filePath);
compress(out, sourceFile, sourceFile.getName());
}
out.close();
System.out.println("压缩完成");
return zipFileName;
}
public static void compress(ZipOutputStream out, File sourceFile, String base) throws Exception {
// 如果路径为目录(文件夹)
if (sourceFile.isDirectory()) {
// 取出文件夹中的文件(或子文件夹)
File[] flist = sourceFile.listFiles();
if (flist == null || flist.length == 0) {// 如果文件夹为空,则只需在目的地zip文件中写入一个目录进入点
out.putNextEntry(new ZipEntry(base + "/"));
} else {// 如果文件夹不为空,则递归调用compress,文件夹中的每一个文件(或文件夹)进行压缩
for (int i = 0; i < flist.length; i++) {
compress(out, flist[i], base + "/" + flist[i].getName());
}
}
} else {// 如果不是目录(文件夹),即为文件,则先写入目录进入点,之后将文件写入zip文件中
out.putNextEntry(new ZipEntry(base));
FileInputStream fos = new FileInputStream(sourceFile);
BufferedInputStream bis = new BufferedInputStream(fos);
int tag;
// 将源文件写入到zip文件中
while ((tag = bis.read()) != -1) {
out.write(tag);
}
bis.close();
fos.close();
}
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论