# hq-tools(工具类库) **Repository Path**: haioa/hq-tools ## Basic Information - **Project Name**: hq-tools(工具类库) - **Description**: Hqtool是一个工作中常用业务操作的经验积累,主要解决项目中一些时间格式化,办公文档word,excel等导入导出,验证码等功能集合 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2022-06-20 - **Last Updated**: 2023-08-28 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # hqTool静态工具库 ## 1.Excel表格相关操作(ExcelUtils) ### 表格数据导出操作 #### 1.1导出list数据到浏览器web文件流 ```java /** * 将list数据源的数据导入到excel表单,并返回输出流 * @param class 集合对应实体注解类 * @param list 数据源 * @param sheetName 工作表的名称 * @param title 表格标题 */ ExcelUtils.getListToExcel(SysUser.class,sysUsers,"系统用户","用户信息"); ``` #### 1.2导出list数据到固定文件夹 ```java /** * 将数据导出到Excel文件到固定路径文件下 * * @param clazz 类 * @param list 数据集合 * @param sheetName sheetName * @param exportPath 导出路径 * @param 范型 */ ExcelUtils.getListToExcelFile(SysUser.class,sysUsers,"系统用户","/Users/haiqiu/Desktop"); ``` #### 1.3导出map集合数据到浏览器web文件流 ```java /** * 根据 List> maps 数据格式进行导出 * @param maps 数据源 * @param sheetName 工作表的名称 * @param title 表格标题 */ ExcelUtils.getMapsToExcel(maps,"系统用户","用户信息"); ``` #### 1.4导出map集合数据到固定文件夹 ```java /** * 根据 List> maps 数据格式进行导出到文件 * * @param maps List> maps * @param sheetName sheetName * @param exportPath 导出路径 * @param 范型 */ ExcelUtils.getMapsToExcelFile(maps,"系统用户","/Users/haiqiu/Desktop"); ``` ### 表格数据导入操作 #### 1.5 excel导入数据list ```java /** * 将excel表单数据源的数据导入到list * * @param sheetName 工作表的名称 * @param file 输入 * @param clazz 要转换的对象类型 */ @NonNull public static List getExcelToList(@Nullable String sheetName, @NonNull MultipartFile file, @NonNull Class clazz) { return getExcelToList(sheetName, null, file, clazz); } ``` #### 1.6 excel导入数据maps ```java /** * 将excel表单数据源的数据导入到Map * * @param sheetName 工作表的名称 * @param file 输入 */ public static List> getExcelToMaps(@Nullable String sheetName, @NonNull MultipartFile file) { return getExcelToMaps(sheetName, null, file); } ``` ### 表格数据模板操作 #### 1.7 excel模板生成 ```java /** * 生成导入模版(带示例数据) * * @param templateName 模版名称 * @param sheetName sheetName * @param clazz 模版类 * @param list 模板数据 * @param title 表格名称 */ public static void createTemplate(@NonNull String templateName, String sheetName, String title, @NonNull Class clazz, List list) ``` ## 2.二维码(QRCodeUtils) ```java /** * 生成二维码(内嵌LOGO) * * @param content 内容 * @param imgPath LOGO地址 * @param destPath 存放目录 * @param needCompress 是否压缩LOGO * @throws Exception */ public static String encode(String content, String imgPath, String destPath, boolean needCompress) ``` ```java /** * 生成二维码并通过流返回到浏览器 */ public static void encodeExport(String content, String imgPath,boolean needCompress) ``` ## 2.Word文档(WordUtils) ```java /** * 导出word模板数据为pdf */ @SneakyThrows public static void exportPdf(String wordPath, Map parameters, String fileName) ``` ```java @SneakyThrows public static void exportDocx(String wordPath, Map parameters, String fileName) ``` ## 3.ftp连接池合成工具类 #### 使用方法 - 1.克隆本项目到idea本地,maven选择install - 2.在你的项目中引入依赖 ```java com.haiqiu.tools hq-ftp 1.1.3 ``` - 3.配置ftp连接信息 ```yaml ftp: server: #启用FTP服务 enable: true #ftp服务器ip地址或者域名 host: 127.0.0.1 #ftp服务端口 port: 21 #用户名 username: username #密码 password: password #开启被动模式 passive-mode: true ``` - 4.可以自己写上传或者下载接口测试 ```java package com.haiqiu.tools.test.controller; import com.haiqiu.file.ftp.utils.FtpUtils; import com.haiqiu.tools.captcha.entity.Captcha; import com.haiqiu.tools.captcha.utils.ImgVerifyCode; import com.haiqiu.tools.common.utils.HttpServletUtils; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.imageio.ImageIO; import javax.websocket.server.PathParam; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.util.HashSet; import java.util.List; import java.util.Set; /** * ftp文件上传测试 * * @author haiqiu * @date 2022/6/28 */ @RestController @RequestMapping("/ftp") public class FtpController { public static final String PATH = "/test/document"; /** * 单文件上传 * * @param file 文件 */ @RequestMapping(value = "upload") public Object uploads(@RequestParam("file") MultipartFile file) { String filePath = FtpUtils.uploadFile("/test/document", file); if (filePath != null) { System.out.println("文件:" + file.getOriginalFilename() + "上传成功,服务器路径:" + filePath); } return filePath; } /** * 多文件上传 * * @param files 文件批量上传 */ @RequestMapping(value = "uploads") public Object uploads(@RequestPart(value = "files") List files) { Set filePaths = new HashSet<>(); for (MultipartFile file : files) { String filePath = FtpUtils.uploadFile(PATH, file); if (filePath != null) { System.out.println("文件:" + file.getOriginalFilename() + "上传成功,服务器路径:" + filePath); filePaths.add(filePath); } } return filePaths; } /** * 单文件下载 * 此接口可以预览图片,文档,文本等常见的文件浏览器打开预览 * * @param fileName 文件 */ @RequestMapping(value = "download/{fileName}") public byte[] download(@PathVariable("fileName") String fileName) { String remoteFilePath = PATH + "/" + fileName; return FtpUtils.download(remoteFilePath); } /** * 单文件下载 * 返回文件二进制流 * * @param fileName 文件 */ @RequestMapping(value = "downloadFile/{fileName}") public void downloadFile(@PathVariable("fileName") String fileName) { String remoteFilePath = PATH + "/" + fileName; FtpUtils.downloadFile(remoteFilePath); } /** * 预览图片 * @param fileName 文件路径 * @return 文件流 * @throws Exception */ @GetMapping(value = "/img/{fileName}", produces = MediaType.IMAGE_PNG_VALUE) public byte[] getImage(@PathVariable("fileName") String fileName) throws Exception { String remoteFilePath = PATH + "/" + fileName; byte[] bytes = FtpUtils.download(remoteFilePath); assert bytes != null; BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(bytes)); ByteArrayOutputStream out = new ByteArrayOutputStream(); String type = fileName.substring(fileName.lastIndexOf(".") + 1); ImageIO.write(bufferedImage, type.toLowerCase(), out); return out.toByteArray(); } } ``` ## 3.阿里云oss工具类 #### 使用方法 - 1.克隆本项目到idea本地,maven选择install - 2.在你的项目中引入依赖 ```java com.haiqiu.tools hq-ftp 1.1.3 ``` - 3.配置ftp连接信息 ```yaml aliyun: oss: #开启oss文件上传存储 enable: true #oss密钥相关配置 access-key-id: xxxxxxxxxx access-key-secret: xxxxxxxx endpoint: oss-cn-xxxxxxx.com bucket-name: xxxxx host: https://xxxxxx.oss-cn-xxxxx.aliyuncs.com ``` - 4.可以自己写上传或者下载接口测试 ```java package com.haiqiu.tools.test.controller; import com.aliyun.oss.model.OSSObjectSummary; import com.haiqiu.file.ftp.utils.FtpUtils; import com.haiqiu.file.oss.utils.OssUtils; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.util.HashSet; import java.util.List; import java.util.Set; /** * oss文件上传测试 * * @author haiqiu * @date 2022/6/28 */ @RestController @RequestMapping("/oss") public class OssController { public static final String PATH = "/test/document"; /** * 单文件上传 * * @param file 文件 */ @PostMapping(value = "upload") public Object uploads(@RequestParam("file") MultipartFile file) { String filePath = OssUtils.uploadFile("/test/document", file); if (filePath != null) { System.out.println("文件:" + file.getOriginalFilename() + "上传成功,服务器路径:" + filePath); } return filePath; } /** * 多文件上传 * * @param files 文件批量上传 */ @PostMapping(value = "uploads") public Object uploads(@RequestPart(value = "files") List files) { Set filePaths = new HashSet<>(); for (MultipartFile file : files) { String filePath = OssUtils.uploadFile(PATH, file); if (filePath != null) { System.out.println("文件:" + file.getOriginalFilename() + "上传成功,服务器路径:" + filePath); filePaths.add(filePath); } } return filePaths; } /** * 单文件下载 * 此接口可以预览图片,文档,文本等常见的文件浏览器打开预览 * * @param fileName 文件 */ @GetMapping(value = "download/{fileName}") public byte[] download(@PathVariable("fileName") String fileName) { String remoteFilePath = PATH + "/" + fileName; return OssUtils.download(remoteFilePath); } /** * 单文件下载 * 返回文件二进制流 * * @param fileName 文件 */ @GetMapping(value = "downloadFile/{fileName}") public void downloadFile(@PathVariable("fileName") String fileName) { String remoteFilePath = PATH + "/" + fileName; OssUtils.downloadFile(remoteFilePath); } /** * 预览图片 * * @param fileName 文件路径 * @return 文件流 * @throws Exception */ @GetMapping(value = "/img/{fileName}", produces = MediaType.IMAGE_PNG_VALUE) public byte[] getImage(@PathVariable("fileName") String fileName) throws Exception { String remoteFilePath = PATH + "/" + fileName; byte[] bytes = FtpUtils.download(remoteFilePath); assert bytes != null; BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(bytes)); ByteArrayOutputStream out = new ByteArrayOutputStream(); String type = fileName.substring(fileName.lastIndexOf(".") + 1); ImageIO.write(bufferedImage, type.toLowerCase(), out); return out.toByteArray(); } /** * 查看oss集合列表 * * @param path 路径前缀 * @return 集合信息 */ @GetMapping(value = "list") public List list(@RequestParam(required = false) String path) { if (path != null && path.length() > 0) { return OssUtils.list(path); } return OssUtils.list(null); } /** * 删除oss文件 * * @param remotePath 文件地址 * @return 删除成功 */ @DeleteMapping public boolean delete(@RequestParam String remotePath) { return OssUtils.deleteFile(remotePath); } } ```