场景说明:
基于hutool 生成csv文件 随机文件名【getRandomFileName()】 指定路径【path】下生成csv 这里引用了一个循环模拟数据接收 这个数据来源可以是rabbitMQ、redis等 一次性获取多条数据推送 每条数据生成一行csv 并且csv有自己的表头 当读取csv的行量【readSize()】超过最大值【csvMaxSize】时 重新生成一个带表头的文件createCsv(); 如果行量没超过最大值时 继续追加数据 由于hutool没有提供直接的接口方法做追加数据:这里采纳了hutool读取csv数据【readData()】 合并新数据模式写入文件 循环推送 做了34次推送 每次推两条 由于推送每次是多条 判断当前如果小于最大值【csvMaxSize】时 就会当前量【readSize()】+ 每次推送量 会使文件实际行量大于等于最大值【csvMaxSize】 下一次推送时才能发现行量超过最大值【csvMaxSize】 出发生成新文件
测试方法:
搞一个空maven项目 放上我的pom 直接放 不用的也可以自己删吧删吧 把java代码建一个TestController 为了测试方便 一大坨都给你放这里头了 运行 访问 http://127.0.0.1:8080/swagger-ui.html
具体代码如下:
代码:
package com.dragon.controller;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.text.csv.*;
import cn.hutool.core.util.CharsetUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.io.*;
import java.lang.reflect.Field;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
@Api(value = "测试")
@RestController
@RequestMapping("/test")
public class TestController {
private String csvName;
private String path= "D:\\cscFile\\";
private int csvMaxSize=20;
@Data
@Setter
@Getter
public class User {
private String id;
private String name;
private String nickname;
private String sex;
private String age;
}
@ApiOperation(value = "测试连通性", httpMethod = "GET")
@ResponseBody
@RequestMapping(value = "/p", method = RequestMethod.GET)
public void getUserList() {
createCsv();
for(int i=1;i<35;i++){
List<User> userList=new ArrayList<>();
User tempA = new User();
tempA.setId("AID-A:"+Integer.toString(i));
tempA.setSex("A男");
tempA.setName("Aname:wanger");
tempA.setNickname("Anickname:Jay");
tempA.setAge("A30");
userList.add(tempA);
User tempB = new User();
tempB.setId("BID-B:"+Integer.toString(i));
tempB.setSex("B男");
tempB.setName("Bname:wanger");
tempB.setNickname("Bnickname:Jay");
tempB.setAge("B30");
userList.add(tempB);
List result= new ArrayList<String[]>();
if(readSize(path+csvName) >csvMaxSize){
createCsv();
}
List<CsvRow> fileCsvRows = new ArrayList<>();
fileCsvRows = readData(path+csvName);
if(fileCsvRows.size()>0){
for(int lineKey=0;lineKey<fileCsvRows.size();lineKey++){
result.add(fileCsvRows.get(lineKey));
}
}
for(User user:userList){
Field[] declaredFields =new Field[4];
try {
declaredFields[0]=user.getClass().getDeclaredField("name");
declaredFields[1]=user.getClass().getDeclaredField("nickname");
declaredFields[2]=user.getClass().getDeclaredField("sex");
declaredFields[3]=user.getClass().getDeclaredField("id");
String[] listValue = new String[declaredFields.length];
for(int x=0;x<listValue.length;x++){
declaredFields[x].setAccessible(true);
listValue[x] = String.valueOf(declaredFields[x].get(user)) ;
}
result.add(listValue);
} catch (Exception e) {
e.printStackTrace();
}
}
this.createCsvFile(result,csvName);
}
}
public void createCsv(){
csvName=getRandomFileName("dzbs-")+".csv";
String[] listName = new String[4];
listName[0]="用户名";
listName[1]="用户昵称";
listName[2]="性别";
listName[3]="id";
List newResult= new ArrayList<String[]>();
newResult.add(listName);
this.createCsvFile(newResult,csvName);
}
public void createCsvFile(List result,String fileName){
try {
File csvFile = new File(fileName);
CsvWriter writer = CsvUtil.getWriter(csvFile, CharsetUtil.CHARSET_GBK);
writer.write(result);
writer.close();
FileInputStream fileInputStream=new FileInputStream(csvFile);
this.saveFile(fileInputStream,csvFile.getName());
}
catch (Exception e){
System.out.println(e);
}
}
private void saveFile(InputStream inputStream, String fileName) {
OutputStream os = null;
try {
byte[] bs = new byte[1024000];
int len;
File tempFile = new File(path);
if (!tempFile.exists()) {
tempFile.mkdirs();
}
os = new FileOutputStream(tempFile.getPath() + File.separator + fileName);
while ((len = inputStream.read(bs)) != -1) {
os.write(bs, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
os.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private int readSize(String fileName) {
CsvReader reader = CsvUtil.getReader();
int size = 0;
try {
CsvData data = reader.read(FileUtil.file(fileName), CharsetUtil.CHARSET_GBK);
size = data.getRowCount();
}catch (Exception e){
System.out.println("尚未生成:"+fileName+ " --即将创建 ");
}
return size;
}
private List<CsvRow> readData(String fileName) {
CsvReader reader = CsvUtil.getReader();
List<CsvRow> scvData = new ArrayList<>();
try {
CsvData data = reader.read(FileUtil.file(fileName), CharsetUtil.CHARSET_GBK);
scvData = data.getRows();
}catch (Exception e){
}
return scvData;
}
public static String getRandomFileName(String pre) {
SimpleDateFormat simpleDateFormat;
simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
Date date = new Date();
String str = simpleDateFormat.format(date);
Random random = new Random();
int rannum = (int) (random.nextDouble() * (99999 - 10000 + 1)) + 10000;
return pre+ str + rannum;
}
}
maven依赖:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.0</version>
<relativePath/>
</parent>
<groupId>com.dragon</groupId>
<artifactId>springboot_rabbitmq_consumer</artifactId>
<version>v1.3</version>
<packaging>jar</packaging>
<name>springboot_rabbitmq_consumer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<artifactId>log4j-api</artifactId>
<groupId>org.apache.logging.log4j</groupId>
</exclusion>
<exclusion>
<artifactId>log4j-to-slf4j</artifactId>
<groupId>org.apache.logging.log4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.16.0</version>
<exclusions>
<exclusion>
<artifactId>log4j-api</artifactId>
<groupId>org.apache.logging.log4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.16.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
<version>2.16.0</version>
<exclusions>
<exclusion>
<artifactId>log4j-api</artifactId>
<groupId>org.apache.logging.log4j</groupId>
</exclusion>
</exclusions>
</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-data-jdbc</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.0</version>
<exclusions>
<exclusion>
<artifactId>mybatis-plus-core</artifactId>
<groupId>com.baomidou</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.6.5</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-core</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.2.3</version>
<classifier>jdk15</classifier>
<exclusions>
<exclusion>
<artifactId>commons-lang</artifactId>
<groupId>commons-lang</groupId>
</exclusion>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.78</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>lib</directory>
<targetPath>BOOT-INF/lib/</targetPath>
<includes>
<include>**/*.jar</include>
</includes>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
</project>
|