一、创建springboot项目
1、访问http://start.spring.io/
2、选择你需要的构建方式,填写项目基本信息
3、点击Generate Project下载项目压缩包
4、解压导入
二、添加相关依赖,修改配置文件
pom.xml
QRCode.jar 需要先下载到工程
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.payment</groupId>
<artifactId>VerifyPaymentCode</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>VerifyPaymentCode</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>QRCode</groupId>
<artifactId>QRCode</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/web/lib/QRCode.jar</systemPath>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties
# MULTIPART (MultipartProperties)
#默认支持文件上传
spring.servlet.multipart.enabled=true
#支持文件写入磁盘.
spring.servlet.multipart.file-size-threshold=0
# 上传文件的临时目录
spring.servlet.multipart.location=
# 最大支持文件大小
spring.servlet.multipart.max-file-size=10Mb
# 最大支持请求大小
spring.servlet.multipart.max-request-size=100Mb
三、编写MyQRCodeImage解析二维码
package com.payment.VerifyPaymentCode.util;
import com.payment.VerifyPaymentCode.pojo.Payment;
import jp.sourceforge.qrcode.QRCodeDecoder;
import jp.sourceforge.qrcode.data.QRCodeImage;
import jp.sourceforge.qrcode.exception.DecodingFailedException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.nio.charset.Charset;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class MyQRCodeImage implements QRCodeImage {
BufferedImage bufferedImage ;
public MyQRCodeImage(BufferedImage bufferedImage) {
super();
this.bufferedImage = bufferedImage;
}
@Override
public int getHeight() {
return bufferedImage.getHeight();
}
@Override
public int getPixel(int arg0, int arg1) {
return bufferedImage.getRGB(arg0, arg1);
}
@Override
public int getWidth() {
return bufferedImage.getWidth();
}
public static String decode(BufferedImage bufferedImage) throws Exception {
String result;
try{
if (bufferedImage == null) {
result = "图片解析失败";
return result;
}
QRCodeDecoder qrCodeDecoder = new QRCodeDecoder();
result = new String(qrCodeDecoder.decode(new MyQRCodeImage(bufferedImage)),"utf-8");
bufferedImage.flush();
}catch(DecodingFailedException dfe) {
Logger.getLogger(MyQRCodeImage.class.getName()).log(Level.INFO, "解析失败");
result = "二维码解析失败";
return result;
}
return result;
}
public static List<Payment> IdentifyQRCode(InputStream inputStream) throws Exception{
ZipInputStream zis=new ZipInputStream(inputStream,Charset.forName("GBK"));
BufferedImage bufferedImage = null;
ZipEntry entry;
List<Payment> plist=new ArrayList<>();
try {
while ((entry = zis.getNextEntry()) != null) {
if(!entry.isDirectory()){
//识别图片
bufferedImage=ImageIO.read(zis);
String name = new File(entry.getName()).getName();
plist.add(getPayment(name,bufferedImage));
zis.closeEntry();
}
}
}finally {
if(zis!=null){
zis.close();
}
}
return plist;
}
static Payment getPayment(String fileName,BufferedImage bufferedImage) throws Exception {
String str=decode(bufferedImage).toLowerCase();
String amountStr=fileName.substring(0,fileName.lastIndexOf("."));
int amount=0;
//包含小数的正整数,且不能大于11位 单位分
boolean result=amountStr.matches("^[+]?\\d+(\\.\\d+)?$");
if (result == true && amountStr.length()<=11) {
//是否只包含两位小数
if(amountStr.contains(".")){
if(amountStr.split("\\.")[1].length()==2){
BigDecimal b1=new BigDecimal(100);
BigDecimal a1=new BigDecimal(amountStr).multiply(b1);
amount=a1.intValue();
}else{
return new Payment(fileName,400,amount,null,"文件名不合法,只能是包含两位小数的正整数");
}
}else{
amount=Integer.parseInt(amountStr);
}
}else{
return new Payment(fileName,400,amount,null,"文件名不合法,只能是包含两位小数的正整数");
}
if(str.indexOf("http")!=-1){
return new Payment(fileName,200,amount,str,"");
}else{
return new Payment(fileName,400,amount,null,str);
}
}
}
四、编写Controller
package com.payment.VerifyPaymentCode.controller;
import com.payment.VerifyPaymentCode.pojo.ResultCode;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import static com.payment.VerifyPaymentCode.util.MyQRCodeImage.IdentifyQRCode;
@RestController
public class Upload {
@RequestMapping("/upload")
public ResultCode singleFileUpload(@RequestParam("file") MultipartFile file) throws Exception {
if (file.isEmpty()) {
return new ResultCode(500,"Please select a file to upload");
}if(!file.getContentType().equalsIgnoreCase("application/x-zip-compressed")){
return new ResultCode(500,"文件类型有误,只接受.zip");
}
return new ResultCode(200,IdentifyQRCode(file.getInputStream()),"You successfully uploaded '" + file.getOriginalFilename() + "'");
}
}
网友评论