tao.mo

增加电子章需求

mt
2023年6月8日13:56:18
No preview for this file type
......@@ -15,7 +15,7 @@ import javax.servlet.http.HttpServletRequest;
import java.util.Date;
/**
* @author Administrator
* @author mt
* /hscode/hscodeRequest
*/
@RestController
......
package com.erry.iclear.dateInterface.api;
import com.erry.iclear.dateInterface.api.request.HsCodeParam;
import com.erry.iclear.dateInterface.api.response.ResultHsCode;
import com.erry.iclear.dateInterface.entity.IClearApiHsCodeLog;
import com.erry.iclear.dateInterface.enums.ErrorCodeEnum;
import com.erry.iclear.dateInterface.service.HsCodeService;
import com.erry.iclear.dateInterface.service.IClearApiHsCodeLogService;
import com.erry.iclear.dateInterface.util.StringUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
/**
* @author mt
* /iknowU/stampQuery
*/
@RestController
@RequestMapping("/iknowU")
@Slf4j
public class IKnowUInterface {
@Autowired
HsCodeService hsCodeService;
@Autowired
IClearApiHsCodeLogService iClearApiHsCodeLogService;
@ApiOperation(value = "抠图后的公章查询", httpMethod = "POST", notes = "ex: http://192.168.1.71:8080/iknowU/stampQuery")
@PostMapping("/stampQuery")
@ResponseBody
public ResultHsCode stampQuery(HttpServletRequest request
, @RequestBody String consignmentCode) {
log.info("接收到请求数据hscode:【"+consignmentCode+"】");
String appId = (String) request.getHeader("Appid");
String appkey = (String) request.getHeader("Appkey");
IClearApiHsCodeLog iclearApiHsCodeLog = new IClearApiHsCodeLog();
ResultHsCode resultHsCode = null;
long begin = System.currentTimeMillis();
try {
iclearApiHsCodeLog.setAppId(appId);
iclearApiHsCodeLog.setAppKey(appkey);
iclearApiHsCodeLog.setCreateTime(new Date());
iclearApiHsCodeLog.setCode(consignmentCode);
if (!StringUtil.isEmpty(consignmentCode)) {
resultHsCode = hsCodeService.findHsCodeListByCode(consignmentCode);
return resultHsCode;
} else {
//如果没有传hscode需要限制10分钟只能查询一次
boolean rs = iClearApiHsCodeLogService.findIClearApiHsCodeLog(appId);
if(rs == true){
resultHsCode = new ResultHsCode(Boolean.FALSE, ErrorCodeEnum.ERROR_10019.getCode(),ErrorCodeEnum.ERROR_10019.getName(),null);
}else{
resultHsCode = hsCodeService.findAllHsCodeList();
}
return resultHsCode;
}
}catch(Exception ex){
log.error(ex.getMessage(),ex);
resultHsCode = new ResultHsCode(Boolean.FALSE,ErrorCodeEnum.ERROR_30101.getCode(),ErrorCodeEnum.ERROR_30101.getName(),null);
return resultHsCode;
}finally {
iclearApiHsCodeLog.setReceiveResult(resultHsCode.getMsg());
iclearApiHsCodeLog.setErrorCode(resultHsCode.getCode());
iclearApiHsCodeLog.setResponseTime(new Date());
long end = System.currentTimeMillis();
iclearApiHsCodeLog.setSpendTime(end-begin);
//如果异常为hscode长度超过2000,将截取0到2000字符
if(resultHsCode.getCode().equals(ErrorCodeEnum.ERROR_30102.getCode())){
iclearApiHsCodeLog.setCode(consignmentCode.substring(0,2000));
}
iClearApiHsCodeLogService.saveIClearApiHsCodeLog(iclearApiHsCodeLog);
}
}
}
package com.erry.iclear.dateInterface.api.response;
import com.erry.iclear.dateInterface.entity.HsCode;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.io.Serializable;
import java.util.List;
@Data
@AllArgsConstructor
public class StampResult implements Serializable {
//成功标识
private Boolean success = true;
//结果编码
private String code;
//错误信息
private String msg;
}
package com.erry.iclear.dateInterface.util;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import java.io.*;
/**
* 文件处理工具类
* mt
* 2023年6月7日10:25:04
*/
public class FileUtils {
/**
* 本地文件(图片、excel等)转换成Base64字符串
* @param imgPath
* @return
* @throws Exception
*/
public static String convertFileToBase64(String imgPath) throws Exception{
byte[] data = null;
InputStream in = null;
// 读取图片字节数组
try {
in = new FileInputStream(imgPath);
data = new byte[in.available()];
in.read(data);
} catch (IOException e) {
e.printStackTrace();
}finally {
if(in != null){
in.close();
}
}
// 对字节数组进行Base64编码,得到Base64编码的字符串
BASE64Encoder encoder = new BASE64Encoder();
String base64Str = encoder.encode(data);
return base64Str;
}
/**
* 将base64字符串,生成文件
* @param fileBase64String
* @param filePath
* @param fileName
* @return
*/
public static File convertBase64ToFile(String fileBase64String, String filePath, String fileName) {
BufferedOutputStream bos = null;
FileOutputStream fos = null;
File file = null;
try {
File dir = new File(filePath);
if (!dir.exists() && dir.isDirectory()) {//判断文件目录是否存在
dir.mkdirs();
}
BASE64Decoder decoder = new BASE64Decoder();
byte[] bfile = decoder.decodeBuffer(fileBase64String);
file = new File(filePath + File.separator + fileName);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(bfile);
return file;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
}