From ec296a3540ebf4697d9ba6f9c76f3fe2d7282baa Mon Sep 17 00:00:00 2001 From: liuchengqian Date: Wed, 8 Mar 2023 11:59:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/xkrs/common/StaticScheduleTask.java | 4 +- .../straw/controller/GaoDeApiController.java | 2 +- .../straw/controller/SysUserController.java | 2 +- .../straw/helper/FirePointPushManager.java | 6 +- .../straw/helper/FirePointQueryManager.java | 2 +- .../helper/FirePointSubscribeManager.java | 2 +- .../CommonConstant.java | 2 +- .../FirePointCodeUtils.java | 3 +- .../{utilsold => utilsnew}/RandomUtil.java | 2 +- .../WeiXinMessageUtils.java} | 9 +- .../com/xkrs/straw/utilsold/FileFastDfs.java | 93 +------- .../com/xkrs/straw/utilsold/FileUtil.java | 216 ------------------ .../com/xkrs/straw/utilsold/GaoDeApiUtil.java | 4 +- .../xkrs/straw/utilsold/LocalNullUtils.java | 16 -- .../com/xkrs/straw/utilsold/XlsxUtil.java | 152 ------------ 15 files changed, 19 insertions(+), 496 deletions(-) rename src/main/java/com/xkrs/straw/{utilsold => utilsnew}/CommonConstant.java (92%) rename src/main/java/com/xkrs/straw/{utilsold => utilsnew}/FirePointCodeUtils.java (97%) rename src/main/java/com/xkrs/straw/{utilsold => utilsnew}/RandomUtil.java (88%) rename src/main/java/com/xkrs/straw/{utilsold/WDWxSendMsgUtil.java => utilsnew/WeiXinMessageUtils.java} (87%) delete mode 100644 src/main/java/com/xkrs/straw/utilsold/FileUtil.java delete mode 100644 src/main/java/com/xkrs/straw/utilsold/LocalNullUtils.java delete mode 100644 src/main/java/com/xkrs/straw/utilsold/XlsxUtil.java diff --git a/src/main/java/com/xkrs/common/StaticScheduleTask.java b/src/main/java/com/xkrs/common/StaticScheduleTask.java index f9aadc6..2e5100b 100644 --- a/src/main/java/com/xkrs/common/StaticScheduleTask.java +++ b/src/main/java/com/xkrs/common/StaticScheduleTask.java @@ -2,7 +2,7 @@ package com.xkrs.common; import com.xkrs.straw.helper.FirePointSubscribeManager; import com.xkrs.straw.utilsnew.DateTimeUtils; -import com.xkrs.straw.utilsold.WDWxSendMsgUtil; +import com.xkrs.straw.utilsnew.WeiXinMessageUtils; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; @@ -30,7 +30,7 @@ public class StaticScheduleTask { @Scheduled(cron = "0 0 * * * ?") private void testPushWeiXinMessage() { try { - WDWxSendMsgUtil.sendMsg("18447024917@chatroom", "微信发消息测试 " + DateTimeUtils.localDateTimeToString(LocalDateTime.now()), 0); + WeiXinMessageUtils.sendMsg("18447024917@chatroom", "微信发消息测试 " + DateTimeUtils.localDateTimeToString(LocalDateTime.now()), 0); } catch (Exception e) { e.printStackTrace(); } diff --git a/src/main/java/com/xkrs/straw/controller/GaoDeApiController.java b/src/main/java/com/xkrs/straw/controller/GaoDeApiController.java index 5614dc6..2c8b7ef 100644 --- a/src/main/java/com/xkrs/straw/controller/GaoDeApiController.java +++ b/src/main/java/com/xkrs/straw/controller/GaoDeApiController.java @@ -16,7 +16,7 @@ import java.util.List; import java.util.Locale; import static com.xkrs.common.encapsulation.OutputEncapsulation.outputEncapsulationObject; -import static com.xkrs.straw.utilsold.CommonConstant.*; +import static com.xkrs.straw.utilsnew.CommonConstant.*; import static com.xkrs.straw.utilsold.HttpClientUtils.sendHttpsGet; /** diff --git a/src/main/java/com/xkrs/straw/controller/SysUserController.java b/src/main/java/com/xkrs/straw/controller/SysUserController.java index 872e053..8693135 100644 --- a/src/main/java/com/xkrs/straw/controller/SysUserController.java +++ b/src/main/java/com/xkrs/straw/controller/SysUserController.java @@ -15,7 +15,7 @@ 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.utilsold.RandomUtil; +import com.xkrs.straw.utilsnew.RandomUtil; import org.apache.hc.core5.util.TextUtils; import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.data.domain.Sort; diff --git a/src/main/java/com/xkrs/straw/helper/FirePointPushManager.java b/src/main/java/com/xkrs/straw/helper/FirePointPushManager.java index e5e8e39..8acf9c3 100644 --- a/src/main/java/com/xkrs/straw/helper/FirePointPushManager.java +++ b/src/main/java/com/xkrs/straw/helper/FirePointPushManager.java @@ -9,9 +9,9 @@ import com.xkrs.straw.model.entity.SysUserEntity; import com.xkrs.straw.model.helper.PushHelper; import com.xkrs.straw.model.vo.AllFirePointVo; import com.xkrs.straw.utilsnew.FirePointConvertUtils; -import com.xkrs.straw.utilsold.FirePointCodeUtils; +import com.xkrs.straw.utilsnew.FirePointCodeUtils; import com.xkrs.straw.utilsnew.SMSUtils; -import com.xkrs.straw.utilsold.WDWxSendMsgUtil; +import com.xkrs.straw.utilsnew.WeiXinMessageUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; @@ -122,7 +122,7 @@ public class FirePointPushManager { for (String weixinId : weixinIdList) { stringBuilder.append(weixinId).append("\n"); try { - WDWxSendMsgUtil.sendMsg(weixinId, messageContent, 0); + WeiXinMessageUtils.sendMsg(weixinId, messageContent, 0); } catch (Exception e) { e.printStackTrace(); } diff --git a/src/main/java/com/xkrs/straw/helper/FirePointQueryManager.java b/src/main/java/com/xkrs/straw/helper/FirePointQueryManager.java index 0845a86..0d19c4d 100644 --- a/src/main/java/com/xkrs/straw/helper/FirePointQueryManager.java +++ b/src/main/java/com/xkrs/straw/helper/FirePointQueryManager.java @@ -12,7 +12,7 @@ import com.xkrs.straw.utilsnew.FirePointConvertUtils; import com.xkrs.straw.utilsnew.JsonUtils; import com.xkrs.straw.utilsold.DateTimeUtil; import com.xkrs.straw.utilsnew.DateTimeUtils; -import com.xkrs.straw.utilsold.FirePointCodeUtils; +import com.xkrs.straw.utilsnew.FirePointCodeUtils; import org.apache.hc.core5.util.TextUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/src/main/java/com/xkrs/straw/helper/FirePointSubscribeManager.java b/src/main/java/com/xkrs/straw/helper/FirePointSubscribeManager.java index 94aabc3..01eced1 100644 --- a/src/main/java/com/xkrs/straw/helper/FirePointSubscribeManager.java +++ b/src/main/java/com/xkrs/straw/helper/FirePointSubscribeManager.java @@ -11,7 +11,7 @@ import com.xkrs.straw.model.entity.SysUserEntity; import com.xkrs.straw.utilsnew.JsonUtils; import com.xkrs.straw.utilsnew.VipTimeRangeUtils; import com.xkrs.straw.utilsnew.DateTimeUtils; -import com.xkrs.straw.utilsold.FirePointCodeUtils; +import com.xkrs.straw.utilsnew.FirePointCodeUtils; import com.xkrs.straw.utilsnew.ListUtils; import org.springframework.stereotype.Component; diff --git a/src/main/java/com/xkrs/straw/utilsold/CommonConstant.java b/src/main/java/com/xkrs/straw/utilsnew/CommonConstant.java similarity index 92% rename from src/main/java/com/xkrs/straw/utilsold/CommonConstant.java rename to src/main/java/com/xkrs/straw/utilsnew/CommonConstant.java index 3d711f2..9f775c9 100644 --- a/src/main/java/com/xkrs/straw/utilsold/CommonConstant.java +++ b/src/main/java/com/xkrs/straw/utilsnew/CommonConstant.java @@ -1,4 +1,4 @@ -package com.xkrs.straw.utilsold; +package com.xkrs.straw.utilsnew; public interface CommonConstant { diff --git a/src/main/java/com/xkrs/straw/utilsold/FirePointCodeUtils.java b/src/main/java/com/xkrs/straw/utilsnew/FirePointCodeUtils.java similarity index 97% rename from src/main/java/com/xkrs/straw/utilsold/FirePointCodeUtils.java rename to src/main/java/com/xkrs/straw/utilsnew/FirePointCodeUtils.java index 7fb829a..2fad39e 100644 --- a/src/main/java/com/xkrs/straw/utilsold/FirePointCodeUtils.java +++ b/src/main/java/com/xkrs/straw/utilsnew/FirePointCodeUtils.java @@ -1,7 +1,6 @@ -package com.xkrs.straw.utilsold; +package com.xkrs.straw.utilsnew; import com.xkrs.straw.model.entity.SysUserEntity; -import com.xkrs.straw.utilsnew.VipTimeRangeUtils; import org.apache.hc.core5.util.TextUtils; import java.util.ArrayList; diff --git a/src/main/java/com/xkrs/straw/utilsold/RandomUtil.java b/src/main/java/com/xkrs/straw/utilsnew/RandomUtil.java similarity index 88% rename from src/main/java/com/xkrs/straw/utilsold/RandomUtil.java rename to src/main/java/com/xkrs/straw/utilsnew/RandomUtil.java index f0f504c..b5856e7 100644 --- a/src/main/java/com/xkrs/straw/utilsold/RandomUtil.java +++ b/src/main/java/com/xkrs/straw/utilsnew/RandomUtil.java @@ -1,4 +1,4 @@ -package com.xkrs.straw.utilsold; +package com.xkrs.straw.utilsnew; import java.util.Random; diff --git a/src/main/java/com/xkrs/straw/utilsold/WDWxSendMsgUtil.java b/src/main/java/com/xkrs/straw/utilsnew/WeiXinMessageUtils.java similarity index 87% rename from src/main/java/com/xkrs/straw/utilsold/WDWxSendMsgUtil.java rename to src/main/java/com/xkrs/straw/utilsnew/WeiXinMessageUtils.java index fed4d60..11a1d2c 100644 --- a/src/main/java/com/xkrs/straw/utilsold/WDWxSendMsgUtil.java +++ b/src/main/java/com/xkrs/straw/utilsnew/WeiXinMessageUtils.java @@ -1,4 +1,4 @@ -package com.xkrs.straw.utilsold; +package com.xkrs.straw.utilsnew; import com.fasterxml.jackson.databind.ObjectMapper; @@ -7,12 +7,7 @@ import java.util.Map; import static com.xkrs.straw.utilsold.WDHttpClientUtils.sendHttpPost; -/** - * @author wudong - * @date 2022/6/2 9:25 - * @description - */ -public class WDWxSendMsgUtil { +public class WeiXinMessageUtils { /** * 通过udp通信,调用发送微信消息服务,发送微信消息 diff --git a/src/main/java/com/xkrs/straw/utilsold/FileFastDfs.java b/src/main/java/com/xkrs/straw/utilsold/FileFastDfs.java index 9a22437..7860b9d 100644 --- a/src/main/java/com/xkrs/straw/utilsold/FileFastDfs.java +++ b/src/main/java/com/xkrs/straw/utilsold/FileFastDfs.java @@ -51,7 +51,7 @@ public class FileFastDfs { * @param dir * @return */ - public String uploadFile(MultipartFile file, String dir) { + public String uploadsFile(MultipartFile file, String dir) { File file1 = null; InputStreamResource isr = null; try { @@ -103,94 +103,7 @@ public class FileFastDfs { return result; } - /** - * @description: 文件上传到dfs服务器 - * @params [file:File 文件, dir 文件存放路径] - * @author: wd - * @time: 2020/3/31 2020/3/31 - */ - public String uploadFile(File file, String dir) { - //文件地址 - //声明参数集合 - HashMap paramMap = new HashMap<>(7); - //文件 - paramMap.put("file", file); - //输出 - paramMap.put("output", "json"); - //自定义路径 - paramMap.put("path", dir); - //场景 -// System.err.println(file.getName()); - - // paramMap.put("fileName", System.currentTimeMillis() + "" + file.getName().substring(file.getName().lastIndexOf("."))); - paramMap.put("fileName", file.getName()); - 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 status = "status"; - final String statusFail = "fail"; - if (null != node) { - if (statusFail.equals(node.path(status).asText())) { - return null; - } - } - return result; - } - - /** - * @description: 文件上传到dfs服务器 - * @params [file:File 文件, dir 文件存放路径] - * @author: wd - * @time: 2020/3/31 2020/3/31 - */ - public String uploadFile(File file, String dir, String fileType) { - //文件地址 - //声明参数集合 - HashMap paramMap = new HashMap<>(7); - //文件 - paramMap.put("file", file); - //输出 - paramMap.put("output", "json"); - //自定义路径 - paramMap.put("path", dir); - //场景 - System.err.println(file.getName()); - - // paramMap.put("fileName", System.currentTimeMillis() + "" + file.getName().substring(file.getName().lastIndexOf("."))); - paramMap.put("fileName", file.getName()); - paramMap.put("scene", fileType); - 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 status = "status"; - final String statusFail = "fail"; - if (null != node) { - if (statusFail.equals(node.path(status).asText())) { - return null; - } - } - return result; - } - - public String uploadBigFile(File file, String dir, String fileType) { + public String upddloadBigFile(File file, String dir, String fileType) { // 下面这个一定要注意,如果不设置为true,将会直接返回301 System.setProperty("http.strictPostRedirect", Boolean.toString(true)); TusClient tusClient = new TusClient(); @@ -250,7 +163,7 @@ public class FileFastDfs { * @author: chqf * @time: 2020/3/31 2020/3/31 */ - public String getBase64(String fileName) { + public String geddtBase64(String fileName) { if (fileName == null || "".equals(fileName)) { return null; } diff --git a/src/main/java/com/xkrs/straw/utilsold/FileUtil.java b/src/main/java/com/xkrs/straw/utilsold/FileUtil.java deleted file mode 100644 index d576088..0000000 --- a/src/main/java/com/xkrs/straw/utilsold/FileUtil.java +++ /dev/null @@ -1,216 +0,0 @@ -package com.xkrs.straw.utilsold; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import static com.xkrs.straw.utilsold.DateTimeUtil.getNowTimeStr; - -public class FileUtil { - private static final Logger log = LoggerFactory.getLogger(FileUtil.class); - - /** - * 判断目录是否存在, 不存在创建 - * @param path 路径,目录最后带 File.separator - * @return - */ - public static boolean isFileExit(String path){ - path = path.replace("/",File.separator); - File file = new File(path); - if (file.exists()) { - log.info("文件已存在"); - return true; - } - if(path.endsWith(File.separator)) { - log.info("创建单个文件" + path + "失败,目标不能是目录!"); - return false; - } - if(!file.getParentFile().exists()) { - log.info("目标文件所在路径不存在,准备创建。。。"); - if (file.getParentFile().mkdirs()) { - if (file.mkdir()) { - log.info("创建目录文件成功!"); - } - } - } else { - log.info("目标文件所在路径存在"); - if (file.mkdir()) { - log.info("创建目录文件成功!"); - } - } - return true; - } - - /** - * 获取目录下的所有文件 - * @param filePath 目录 - * @return - * @throws Exception - */ - public static List showListFile(String filePath) { - File dir = new File(filePath); - List list = new ArrayList<>(); - // 查找参数文件是否存在,只检查第一个入参 - if(!dir.exists()) { - log.error("找不到文件"); - } - // 如果是目录那么进行递归调用 - if(dir.isDirectory()) { - // 获取目录下的所有文件 - File[] f = dir.listFiles(); - // 进行递归调用,最后总会返回一个list - assert f != null; - for (File file : f) { - list.addAll(showListFile(file.getPath())); - } - }else { - // 不是目录直接添加进去,判断是否为xml文件 - list.add(dir.getPath()); - } - return list; - } - - /** - * 读取txt文件内容 - * @param file - * @return - */ - public static String txt2String(File file){ - StringBuilder result = new StringBuilder(); - try{ - //构造一个BufferedReader类来读取文件 - BufferedReader br = new BufferedReader(new FileReader(file)); - String s = null; - //使用readLine方法,一次读一行 - while((s = br.readLine())!=null){ - result.append(System.lineSeparator()).append(s); - } - br.close(); - }catch(Exception e){ - e.printStackTrace(); - } - return result.toString(); - } - - /** - * 确定上传文件路径遥感数据 栅格 - * @param file 文件 - * @return - */ - public static String fileRsSaveType(File file) { - String name = file.getName(); - String suffix = name.substring(name.indexOf(".")); - // 判断是否为栅格数据 - if (!suffix.contains("tif")) { - return null; - } - // GF1_PMS1_E116.8_N36.6_20190528_L1A0004026837-MSS1_ORTHO_MS.tif - String[] s = name.split("_"); - // 卫星种类 - String satelliteType = s[0]; - // 传感器种类 - String sensorType = s[1]; - // 中心点经度 - String lat = s[2]; - // 中心点纬度 - String lon = s[3]; - // 时间 - String time = s[4]; - String year = time.substring(0, 4); - String month = time.substring(4, 6); - String day = time.substring(6); - // 产品号 - String product = s[5]; - String productLevel = product.substring(0, 3); - String productCode = product.substring(3); - // 种类 - String type = s[6]; - // 文件路径 - String filePath = "/" + satelliteType + "/" + sensorType + "/" + productLevel + "/" + year + "/" + month + "/" - + day + "/" + productCode + "/"; - - return filePath; - } - - /** - * 矢量数据路径 - * C:\Users\dong\Desktop\2018年山东省矢量数据\2018年山东省矢量数据\2018年动态\分县动态\山东.gdb\sa.gdb - * @param file - * @return - */ - public static String fileShpSavePath(File file) { - if (file.exists()) { - if (file.isFile()) { - String path = file.getPath(); - String[] dirNames = path.split("\\\\"); - StringBuffer sb = new StringBuffer(); - int length = dirNames.length; - final int minLength = 5; - if (length >= minLength) { - for (int i = 5; i > 1; i--) { - sb.append("/").append(dirNames[length - i]); - } - return sb.toString(); - } - } - } - return null; - } - - public static Map getUploadInfo(String info) { - final String statusFail = "fail"; - final String status = "status"; - ObjectMapper mapper = new ObjectMapper(); - JsonNode resultNode = null; - Map map = new HashMap<>(); - try { - resultNode = mapper.readTree(info); - } catch (JsonProcessingException e) { - e.printStackTrace(); - } - if (!statusFail.equals(resultNode.path(status).asText())) { - String path = resultNode.path("path").asText(); - String fileName = path.substring(path.lastIndexOf("/") + 1); - map.put("md5", resultNode.path("md5").asText()); - map.put("mtime", getNowTimeStr()); - map.put("path", path); - map.put("scene", resultNode.path("scene").asText()); - map.put("size", resultNode.path("size").asText()); - map.put("url", resultNode.path("url").asText()); - map.put("fileName", fileName); - } - return map; - } - - public static boolean checkFileSize(Long len, int size, String unit) { - double fileSize = 0; - if ("B".equals(unit.toUpperCase())) { - fileSize = (double) len; - } else if ("K".equals(unit.toUpperCase())) { - fileSize = (double) len / 1024; - } else if ("M".equals(unit.toUpperCase())) { - fileSize = (double) len / 1048576; - } else if ("G".equals(unit.toUpperCase())) { - fileSize = (double) len / 1073741824; - } - if (fileSize > size) { - return false; - } - return true; - } - - public static void main(String[] args) { - String s = "C:/Users/dong/Desktop/2018年山东省矢量数据/2018年山东省矢量数据/2018年动态/分县动态/山东.gdb/a0000000a.gdbindexes"; - fileShpSavePath(new File(s)); - } -} diff --git a/src/main/java/com/xkrs/straw/utilsold/GaoDeApiUtil.java b/src/main/java/com/xkrs/straw/utilsold/GaoDeApiUtil.java index f64eba5..c83834b 100644 --- a/src/main/java/com/xkrs/straw/utilsold/GaoDeApiUtil.java +++ b/src/main/java/com/xkrs/straw/utilsold/GaoDeApiUtil.java @@ -10,8 +10,8 @@ import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; -import static com.xkrs.straw.utilsold.CommonConstant.INVERSEGEOGRAPHY_KEY; -import static com.xkrs.straw.utilsold.CommonConstant.INVERSEGEOGRAPHY_URL; +import static com.xkrs.straw.utilsnew.CommonConstant.INVERSEGEOGRAPHY_KEY; +import static com.xkrs.straw.utilsnew.CommonConstant.INVERSEGEOGRAPHY_URL; import static com.xkrs.straw.utilsold.HttpClientUtils.sendHttpsGet; /** diff --git a/src/main/java/com/xkrs/straw/utilsold/LocalNullUtils.java b/src/main/java/com/xkrs/straw/utilsold/LocalNullUtils.java deleted file mode 100644 index 7db1044..0000000 --- a/src/main/java/com/xkrs/straw/utilsold/LocalNullUtils.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.xkrs.straw.utilsold; - -public class LocalNullUtils { - - private LocalNullUtils() { - } - - public static String formatNullValue(String value) { - return formatNullValue(value, ""); - } - - public static String formatNullValue(String value, String defaultValue) { - return null == value ? defaultValue : value; - } - -} diff --git a/src/main/java/com/xkrs/straw/utilsold/XlsxUtil.java b/src/main/java/com/xkrs/straw/utilsold/XlsxUtil.java deleted file mode 100644 index 8d0ea39..0000000 --- a/src/main/java/com/xkrs/straw/utilsold/XlsxUtil.java +++ /dev/null @@ -1,152 +0,0 @@ -package com.xkrs.straw.utilsold; - -import org.apache.poi.ss.usermodel.*; -import org.apache.poi.xssf.usermodel.XSSFWorkbook; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.text.DecimalFormat; -import java.text.SimpleDateFormat; -import java.util.*; - - -/** - * @author wudong - * @date 2021/12/22 17:47 - * @description - */ -public class XlsxUtil { - private static final Logger log = LoggerFactory.getLogger(XlsxUtil.class); - - /** - * 获取表格内容, 第一行为表头,第二行为数据,最少两行 - * - * @param is - * @return - */ - public static List> getXlsxData(InputStream is) { - List> list = new ArrayList<>(); - Workbook wb = null; - try { - wb = new XSSFWorkbook(is); - } catch (IOException e) { - log.error("xlsx,读取数据失败"); - return list; - } - Sheet sheet = wb.getSheetAt(0); - int physicalNumberOfRows = sheet.getPhysicalNumberOfRows(); - if (physicalNumberOfRows < 2) { - return list; - } - Row row = sheet.getRow(0); - int titNums = row.getPhysicalNumberOfCells(); - Object[] tit = new String[titNums]; - for (int j = 0; j < titNums; j++) { - Cell cell = row.getCell(j); - Object value = getValue(cell); - tit[j] = value; - } - for (int i = 1; i < physicalNumberOfRows; i++) { - Row contentRow = sheet.getRow(i); - int physicalNumberOfCells = contentRow.getPhysicalNumberOfCells(); - Map map = new HashMap<>(physicalNumberOfCells); - for (int j = 0; j < physicalNumberOfCells; j++) { - Cell cell = contentRow.getCell(j); - Object value = getValue(cell); - map.put(tit[j], value); - } - list.add(map); - } - return list; - } - - /** - * 获取值 - * - * @param cell - * @return - */ - private static Object getValue(Cell cell) { - CellType cellType = cell.getCellType(); - Object value; - switch (cellType) { - case STRING: - value = cell.getStringCellValue().trim(); - break; - case NUMERIC: - // 获取时间数据 - if (DateUtil.isCellDateFormatted(cell)) { - Date tempValue = cell.getDateCellValue(); - SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd"); - value = simpleFormat.format(tempValue); - } else { - // 防止出现科学计数法 - DecimalFormat df = new DecimalFormat("0"); - value = df.format(cell.getNumericCellValue()); - } - break; - case BOOLEAN: - value = cell.getBooleanCellValue(); - break; - default: - value = null; - } - return value; - } - - /** - * 按内容生成xlsx, 不关闭输出流 - * - * @param contextList 参数 - * @param os - */ - public static void createXml(List> contextList, OutputStream os) { - // 提取表头 - Set strings = contextList.get(0).keySet(); - Workbook wb = new XSSFWorkbook(); - // 创建一页sheet - Sheet sheet = wb.createSheet(); - // 创建表头 - Row row = sheet.createRow(0); - int i = 0; - for (String key : strings) { - row.createCell(i++).setCellValue(key); - } - // 填写内容 - for (int j = 0; j < contextList.size(); j++) { - Row ct = sheet.createRow(j + 1); - ct.setHeight((short) 1200); - Map map = contextList.get(j); - for (int k = 0; k < map.size(); k++) { - ct.createCell(k).setCellValue(String.valueOf(map.get(row.getCell(k).getStringCellValue()))); - } - } - try { - // 可以直接写入到zipOutputStream嘛,需要转一下,转为输入流,再进行写入,这样的话就需要把流输出,不能在该地方关闭 - wb.write(os); - wb.close(); - System.out.println("finish"); - } catch (IOException e) { - log.error("生成Xlsx失败,IO操作失败, 输出到IO失败", e); - } - } - - /** - * 下载文件 单个 - * - * @param contextList - * @param os - */ - public static void downloadXlsx(List> contextList, OutputStream os) { - createXml(contextList, os); - try { - os.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - -}