This commit is contained in:
liuchengqian 2023-03-08 13:50:03 +08:00
parent ec296a3540
commit ea4f0a824b
8 changed files with 6 additions and 647 deletions

View File

@ -21,7 +21,7 @@ import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import static com.xkrs.straw.utilsold.EncryptDecryptUtil.encry256;
import static com.xkrs.straw.utilsnew.EncryptDecryptUtil.encry256;
/**
* 自定义认证Provider

View File

@ -14,7 +14,7 @@ import com.xkrs.straw.model.qo.SysUserVipLevelQo;
import com.xkrs.straw.model.vo.SysUserVo;
import com.xkrs.straw.service.SysUserService;
import com.xkrs.straw.utilsnew.*;
import com.xkrs.straw.utilsold.AliYunSmsUtils;
import com.xkrs.straw.utilsnew.AliYunSmsUtils;
import com.xkrs.straw.utilsnew.RandomUtil;
import org.apache.hc.core5.util.TextUtils;
import org.springframework.context.i18n.LocaleContextHolder;
@ -34,7 +34,7 @@ import java.util.concurrent.TimeUnit;
import static com.xkrs.common.encapsulation.OutputEncapsulation.outputEncapsulationErrorList;
import static com.xkrs.common.encapsulation.OutputEncapsulation.outputEncapsulationObject;
import static com.xkrs.straw.utilsold.EncryptDecryptUtil.encry256;
import static com.xkrs.straw.utilsnew.EncryptDecryptUtil.encry256;
/**
* 系统用户Controller

View File

@ -33,7 +33,7 @@ import java.util.Locale;
import static com.xkrs.common.encapsulation.OutputEncapsulation.outputEncapsulationObject;
import static com.xkrs.straw.utilsold.DateTimeUtil.dateTimeToString;
import static com.xkrs.straw.utilsold.EncryptDecryptUtil.encry256;
import static com.xkrs.straw.utilsnew.EncryptDecryptUtil.encry256;
/**
* 系统用户服务实现

View File

@ -1,4 +1,4 @@
package com.xkrs.straw.utilsold;
package com.xkrs.straw.utilsnew;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;

View File

@ -1,4 +1,4 @@
package com.xkrs.straw.utilsold;
package com.xkrs.straw.utilsnew;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

View File

@ -1,206 +0,0 @@
package com.xkrs.straw.utilsold;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* @author ZHY
* @date 2020/6/17 15:56
*/
public class ExcelImportUtil {
private Workbook wb;
private Sheet sheet;
private Row row;
String xls = ".xls";
String xlsx = ".xlsx";
/**
* 读取Excel
*
* @author ZHY
*/
public ExcelImportUtil(MultipartFile file) throws Exception {
String filename = file.getOriginalFilename();
String ext = filename.substring(filename.lastIndexOf("."));
InputStream is = file.getInputStream();
if (xls.equals(ext)) {
wb = new HSSFWorkbook(is);
} else if (xlsx.equals(ext)) {
wb = new XSSFWorkbook(is);
} else {
wb = null;
}
}
public ExcelImportUtil(File file) throws Exception {
String filename = file.getAbsolutePath();
String ext = filename.substring(filename.lastIndexOf("."));
InputStream is = new FileInputStream(file);
if (xls.equals(ext)) {
wb = new HSSFWorkbook(is);
} else if (xlsx.equals(ext)) {
wb = new XSSFWorkbook(is);
} else {
wb = null;
}
}
/**
* 读取Excel表格表头的内容输出
*/
public List<Map<String, Object>> readExcelTitleOut() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
if (wb != null) {
sheet = wb.getSheetAt(0);
row = sheet.getRow(0);
// 标题总列数
int colNum = row.getPhysicalNumberOfCells();
System.out.println("colNum:" + colNum);
Map<String, Object> map = new LinkedHashMap<String, Object>();
for (int i = 0; i < colNum; i++) {
String stringCellValue = row.getCell(i).getStringCellValue();
map.put(stringCellValue, null);
}
list.add(map);
return list;
}
return list;
}
/**
* 读取Excel表格表头
*/
public String[] readExcelTitle() {
String[] title = {};
if (wb != null) {
sheet = wb.getSheetAt(0);
row = sheet.getRow(0);
// 标题总列数
int colNum = row.getPhysicalNumberOfCells();
System.out.println("colNum:" + colNum);
title = new String[colNum];
for (int i = 0; i < colNum; i++) {
Cell cell = row.getCell(i);
title[i] = cell.getStringCellValue().replaceAll("\\s+", "");
}
}
return title;
}
/**
* 读取Excel表格的某一个数值
*
* @return
*/
public Map<String, Object> readExcelSomeTitle() {
Map<String, Object> map = new LinkedHashMap<>();
if (wb != null) {
sheet = wb.getSheetAt(0);
String title = parseExcel(sheet.getRow(2).getCell(1));
String remark = parseExcel(sheet.getRow(3).getCell(1));
map.put("date", title);
map.put("remark", remark);
}
return map;
}
/**
* 读取Excel数据内容
*/
public List<Map<String, String>> readExcelContent() {
List<Map<String, String>> list = new ArrayList<>();
if (wb != null) {
//获取sheet表
sheet = wb.getSheetAt(0);
// 得到总行数
int rowNum = sheet.getLastRowNum();
//获取表头的标题
String[] readExcelTitle = readExcelTitle();
// 正文内容应该从第二行开始,第一行为表头的标题
for (int i = 1; i <= rowNum; i++) {
row = sheet.getRow(i);
if (row == null) {
continue;
}
Map<String, String> map = new LinkedHashMap<>();
for (int j = 0; j < readExcelTitle.length; j++) {
//获取每一列的数据值
String str = parseExcel(row.getCell(j));
//判断对应行的列值是否为空
if (str != null || "".equals(str)) {
//表头的标题为键值列值为值
map.put(readExcelTitle[j], str);
}
}
//判段添加的对象是否为空
if (!map.isEmpty()) {
list.add(map);
}
}
}
return list;
}
/**
* 根据Cell类型设置数据
*/
int ss = 20;
int xx = 58;
private String parseExcel(Cell cell) {
String result = "";
if (cell != null) {
SimpleDateFormat sdf = null;
switch (cell.getCellType()) {
// 数字类型
case NUMERIC:
// 处理日期格式时间格式
if (DateUtil.isCellDateFormatted(cell)) {
if (cell.getCellStyle().getDataFormat() == ss) {
sdf = new SimpleDateFormat("HH:mm");
} else {// 日期
sdf = new SimpleDateFormat("yyyy-MM-dd");
}
String dateFormat = sdf.format(cell.getDateCellValue());
result = dateFormat;
} else if (cell.getCellStyle().getDataFormat() == xx) {
// 处理自定义日期格式m月d日(通过判断单元格的格式id解决id的值是58)
sdf = new SimpleDateFormat("yyyy-MM-dd");
double value = cell.getNumericCellValue();
Date date = DateUtil.getJavaDate(value);
result = sdf.format(date);
} else {
double value = cell.getNumericCellValue();
DecimalFormat format = new DecimalFormat("#.###########");
String strVal = format.format(value);
result = strVal;
}
break;
// String类型
case STRING:
result = cell.getRichStringCellValue().toString();
break;
default:
break;
}
}
return result;
}
}

View File

@ -1,383 +0,0 @@
package com.xkrs.straw.utilsold;
import cn.hutool.core.io.resource.InputStreamResource;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.tus.java.client.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.HashMap;
import java.util.Objects;
/**
* @author dong
* @date 2020/12/31
*/
@Component
public class FileFastDfs {
@Value("${dfs.ip}")
public String ip;
@Value("${dfs.port}")
public String port;
@Resource
private KafkaTemplate<String, Object> kafkaTemplate;
private static final Logger log = LoggerFactory.getLogger(FileFastDfs.class);
private static final String STATUS = "status";
private static final String UPLOAD_BIG_PATH = "http://192.168.2.166:2001/group1/big/upload";
private static final String UPLOAD_PATH = "http://192.168.2.166:2001/group1/upload";
/**
* 文件上传到dfs服务器
*
* @param file
* @param dir
* @return
*/
public String uploadsFile(MultipartFile file, String dir) {
File file1 = null;
InputStreamResource isr = null;
try {
file1 = multipartFileToFile(file);
isr = new InputStreamResource(file.getInputStream(), file.getOriginalFilename());
} catch (IOException e) {
log.info("IO错误{}", (Object) e.getStackTrace());
} catch (Exception e) {
e.printStackTrace();
}
//文件地址
//声明参数集合
HashMap<String, Object> paramMap = new HashMap<>(7);
//文件
paramMap.put("file", isr);
//输出
paramMap.put("output", "json");
//自定义路径
paramMap.put("path", dir);
//场景 文件分类
if (null == file1) {
return null;
}
String name = file1.getName();
System.err.println("file:" + file1.getName());
paramMap.put("scene", name.substring(name.lastIndexOf(".") + 1));
paramMap.put("fileName", System.currentTimeMillis() + "" + Objects.requireNonNull(file.getOriginalFilename()).substring(file.getOriginalFilename().lastIndexOf(".")));
System.err.println(paramMap);
//
String result = HttpUtil.post("http://" + ip + ":" + port + "/group1/upload", paramMap);
System.err.println(result);
// 拿出状态码判断上传状态
ObjectMapper mapper = new ObjectMapper();
JsonNode node = null;
try {
node = mapper.readTree(result);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
final String fail = "fail";
if (null != node) {
if (fail.equals(node.path(STATUS).asText())) {
file1.delete();
return null;
}
}
file1.delete();
return result;
}
public String upddloadBigFile(File file, String dir, String fileType) {
// 下面这个一定要注意如果不设置为true将会直接返回301
System.setProperty("http.strictPostRedirect", Boolean.toString(true));
TusClient tusClient = new TusClient();
try {
tusClient.setUploadCreationURL(new URL(UPLOAD_BIG_PATH));
// tusClient.enableResuming(new TusRedisUrlStrore());
tusClient.enableResuming(new TusURLMemoryStore());
final TusUpload upload = new TusUpload(file);
System.out.println("start upload......");
TusExecutor executor = new TusExecutor() {
@Override
protected void makeAttempt() throws ProtocolException, IOException {
TusUploader uploader = tusClient.resumeOrCreateUpload(upload);
uploader.setChunkSize(1024 * 1024);
long start = System.currentTimeMillis();
do {
long totalBytes = upload.getSize();
long bytesUploaded = uploader.getOffset();
double progress = (double) bytesUploaded / totalBytes * 100;
System.out.printf("Upload at %06.2f%%.\n", progress);
} while (uploader.uploadChunk() > -1);
uploader.finish();
String uploadUrl = uploader.getUploadURL().toString();
System.out.println("Upload finished.");
System.out.format("Upload available at: %s\n", uploadUrl);
long end = System.currentTimeMillis();
System.out.println((end - start) + "ms");
// 使用hutool进行秒传置换url
String fileId = StrUtil.subAfter(uploadUrl, UPLOAD_BIG_PATH + "/", true);
System.out.println("fileId: " + fileId);
String url = StrUtil.format("{}?md5={}&output=json", UPLOAD_PATH, fileId);
System.out.println("url: " + url);
// 上传大文件的时候1.xG需要sleep一下要不然会有问题
ThreadUtil.sleep(5000);
String result = HttpUtil.get(url);
}
};
executor.makeAttempts();
} catch (IOException | ProtocolException e) {
e.printStackTrace();
}
return null;
}
/**
* @description: 文件base64流获取
* @params [fileName 文件路径]
* @return: java.lang.String
* @author: chqf
* @time: 2020/3/31 2020/3/31
*/
public String geddtBase64(String fileName) {
if (fileName == null || "".equals(fileName)) {
return null;
}
InputStream fis = null;
URL url = null;
try {
url = new URL("http://" + ip + ":" + port + "/" + URLEncoder.encode(fileName, StandardCharsets.UTF_8));
System.err.println(url);
fis = url.openStream();
} catch (IOException e) {
log.info("IO错误{}", (Object) e.getStackTrace());
}
final ByteArrayOutputStream data = new ByteArrayOutputStream();
String imgStr = "";
try {
if (fis == null) {
return null;
}
int len = -1;
byte[] buf = new byte[2048];
while (-1 != (len = fis.read(buf, 0, buf.length))) {
data.write(buf, 0, len);
}
Base64.Encoder encoder = Base64.getEncoder();
imgStr = encoder.encodeToString(data.toByteArray());
} catch (IOException e) {
log.info("IO错误{}", (Object) e.getStackTrace());
} finally {
try {
if (null != fis) {
fis.close();
}
data.close();
} catch (IOException e) {
log.info("IO错误{}", (Object) e.getStackTrace());
}
}
return "data:image/jpeg;base64," + imgStr;
}
/**
* @description: 文件格式转换multipartFil 转为 File 格式
* @params [file]
* @return: java.io.File
* @author: dong
* @time: 2020/3/31 2020/3/31
*/
public File multipartFileToFile(MultipartFile file) throws Exception {
File toFile = null;
if ("".equals(file) || file.getSize() <= 0) {
file = null;
} else {
InputStream ins;
ins = file.getInputStream();
toFile = new File(Objects.requireNonNull(file.getOriginalFilename()));
inputStreamToFile(ins, toFile);
ins.close();
}
return toFile;
}
private static void inputStreamToFile(InputStream ins, File file) {
OutputStream os = null;
try {
os = new FileOutputStream(file);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
} catch (IOException e) {
log.info("IO错误{}", (Object) e.getStackTrace());
} finally {
try {
if (null != os) {
os.close();
}
if (null != ins) {
ins.close();
}
} catch (IOException e) {
log.info("IO错误{}", (Object) e.getStackTrace());
}
}
}
/**
* @description: 文件base64流获取
* @params [fileName 文件路径]
* @return: java.lang.String
* @author: dong
* @time: 2020/3/31 2020/3/31
*/
public Boolean getFile(String fileName, HttpServletResponse response) {
if (fileName == null || "".equals(fileName)) {
return null;
}
InputStream fis = null;
URL url = null;
try {
url = new URL("http://" + ip + ":" + port + "/" + URLEncoder.encode(fileName, StandardCharsets.UTF_8));
System.err.println(url);
fis = url.openStream();
response.reset();
response.setContentType("bin");
response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
} catch (IOException e) {
log.info("IO错误{}", (Object) e.getStackTrace());
}
//
byte[] b = new byte[256];
int len;
try {
assert fis != null;
while ((len = fis.read(b)) > 0) {
response.getOutputStream().write(b, 0, len);
}
} catch (IOException e) {
log.info("IO错误{}", (Object) e.getStackTrace());
} finally {
try {
if (null != fis) {
fis.close();
}
} catch (IOException e) {
log.info("IO错误{}", (Object) e.getStackTrace());
}
}
return true;
}
/**
* 删除文件 path
*
* @param filePath 文件路径 上传返回的path
* @return
*/
public Boolean deleteFileByPath(String filePath) {
if (filePath == null || "".equals(filePath)) {
return false;
}
HashMap<String, Object> paramMap = new HashMap<>(1);
// 参数
paramMap.put("path", filePath);
String result = HttpUtil.post("http://" + ip + ":" + port + "/group1/delete", paramMap);
System.out.println(result);
ObjectMapper mapper = new ObjectMapper();
JsonNode node = null;
try {
node = mapper.readTree(result);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
final String statusOk = "ok";
if (null == node) {
log.error("返回结果为空");
return false;
}
if (!statusOk.equals(node.path(STATUS).asText())) {
log.error("未删除成功");
return false;
}
return true;
}
/**
* 删除文件 path
*
* @param md5 上传返回的md5
* @return
*/
public Boolean deleteFileByMd5(String md5) {
if (md5 == null || "".equals(md5)) {
return false;
}
HashMap<String, Object> paramMap = new HashMap<>(1);
// 参数
paramMap.put("md5", md5);
String result = HttpUtil.post("http://" + ip + ":" + port + "/group1/delete", paramMap);
ObjectMapper mapper = new ObjectMapper();
JsonNode node = null;
try {
node = mapper.readTree(result);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
final String status = "status";
final String statusOk = "ok";
if (null == node) {
log.error("返回结果为空");
return false;
}
if (!statusOk.equals(node.path(status).asText())) {
log.error("未删除成功");
return false;
}
return true;
}
public static void main(String[] args) {
File file = new File("C:\\Users\\dong\\Desktop\\遥感影像\\遥感影像\\GF1\\GF1_PMS1_E116.8_N36.6_20190528_L1A0004026837-MSS1_ORTHO_MS.tif");
FileFastDfs fileFastDfs = new FileFastDfs();
}
}

View File

@ -1,52 +0,0 @@
package com.xkrs.straw.utilsold;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class FileUploadUtils {
public static final boolean release = false;
private FileUploadUtils() {
}
/**
* 上传单个文件
*/
public static String uploadFile(MultipartFile file) throws Exception {
String uploadDirPath = "/Users/liuchengqian/Desktop/DaJiang/";
String originalFilename = file.getOriginalFilename();
int lastPointIndex = originalFilename.lastIndexOf(".");
String extensionName = originalFilename.substring(lastPointIndex);
String newFileName = UUID.randomUUID().toString() + extensionName;
File uploadDir = new File(uploadDirPath);
if ((!uploadDir.exists()) || (!uploadDir.isDirectory())) {
uploadDir.mkdirs();
}
String uploadFilePath = uploadDirPath + newFileName;
//实现上传
file.transferTo(new File(uploadFilePath));
return uploadFilePath;
}
/**
* 上传多个文件
*/
public static List<String> uploadFileArray(MultipartFile[] fileArray) throws Exception {
List<String> pathList = new ArrayList<>();
if (fileArray == null || fileArray.length == 0) {
return pathList;
}
for (MultipartFile file : fileArray) {
if (file != null) {
pathList.add(uploadFile(file));
}
}
return pathList;
}
}