0%

POI操作EXCEL增加下拉框

依赖

1
2
3
4
5
6
7
8
9
10
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>

有时候通过excel将数据批量导入到系统,而业务操作人员对于一些列不想手动输入,而是采用下拉框的方式来进行选择

image-20230324161922014

采用隐藏sheet页的方式来进行操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
String sheetName = "supplier_hidden_sheet";

HSSFSheet supplierSheet = workbook.createSheet(sheetName);//隐藏的sheet,用于存放下拉框的限定值
int count = 0;
for(String supplierName : suppliers){
supplierSheet.createRow(count++).createCell(0).setCellValue(supplierName);
}

String col = "A";

//设置数据有效性加载在哪个单元格上,四个参数分别是:起始行、终止行、起始列、终止列
CellRangeAddressList regions = new CellRangeAddressList(1, dataList.size(), supplierColumn, supplierColumn);
// 这是表示从隐藏sheet页的哪列哪行到哪列哪行 如该格式 supplier_hidden_sheet!$A$1:$A$73
String cell = "\""+sheetName +"!$"+col+"$1:$"+col+"$"+suppliers.size()+"\"";
// 引用ShtDictionary 的单元格
DVConstraint constraint = DVConstraint.createFormulaListConstraint("INDIRECT("+cell+ ")");
// 数据检验为该格式 =INDIRECT("supplier_hidden_sheet!$A$1:$A$73")
HSSFDataValidation dataValidate = new HSSFDataValidation(regions, constraint);
sheet.addValidationData(dataValidate);
workbook.setSheetHidden(1,true);

可以在EXCEL中的数据有效性看到数据来源

image-20230324162821672

xuggle操作视频

有个需求是要读取视频的宽高,找到了Xuggle和FFmpeg两种方式,FFmpeg很强大,但是我并不需要那些功能,所以使用了轻量一点的Xuggle

引入依赖

1
2
3
4
5
<dependency>
<groupId>xuggle</groupId>
<artifactId>xuggle-xuggler</artifactId>
<version>5.4</version>
</dependency>

可能maven仓库中没有该依赖,可以下载后放到本地仓库或私服

xuggle-xuggler-5.4.jar

操作示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
String fileName = "/Users/zhanghe/Desktop/1.mp4";
IContainer container = IContainer.make();
IContainerFormat format = IContainerFormat.make();

int result = container.open(fileName, IContainer.Type.READ, null);
if(result < 0){
throw new RuntimeException("不能打开该文件");
}
int num = container.getNumStreams();
for(int i = 0;i<num;i++){
IStream stream = container.getStream(i);
IStreamCoder coder = stream.getStreamCoder();
if(coder.getCodecType() == ICodec.Type.CODEC_TYPE_VIDEO){
System.out.println(coder.getWidth());
System.out.println(coder.getHeight());
}

}

// 还可以获取
// 时长 单位是μs
long duration = container.getDuration();
System.out.println("duration:"+duration);
// 文件大小
long fileSize = container.getFileSize();
System.out.println("fileSize:"+fileSize);
// 码率
int bitRate = container.getBitRate();
System.out.println("bitRate:"+bitRate);

服务获取和服务续约

eureka客户端通过定时任务的方式进行服务获取和服务续约,在com.netflix.discovery.DiscoveryClient类中,启动了两个定时任务来进行处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
private void initScheduledTasks() {
// 是否需要拉取
if (clientConfig.shouldFetchRegistry()) {
// registry cache refresh timer
int registryFetchIntervalSeconds = clientConfig.getRegistryFetchIntervalSeconds();
int expBackOffBound = clientConfig.getCacheRefreshExecutorExponentialBackOffBound();
// 拉取的定时任务时间间隔是registryFetchIntervalSeconds
scheduler.schedule(
new TimedSupervisorTask(
"cacheRefresh",
scheduler,
cacheRefreshExecutor,
registryFetchIntervalSeconds,
TimeUnit.SECONDS,
expBackOffBound,
new CacheRefreshThread()
),
registryFetchIntervalSeconds, TimeUnit.SECONDS);
}
// 是否需要注册
if (clientConfig.shouldRegisterWithEureka()) {
int renewalIntervalInSecs = instanceInfo.getLeaseInfo().getRenewalIntervalInSecs();
int expBackOffBound = clientConfig.getHeartbeatExecutorExponentialBackOffBound();
logger.info("Starting heartbeat executor: " + "renew interval is: {}", renewalIntervalInSecs);

// Heartbeat timer
scheduler.schedule(
new TimedSupervisorTask(
"heartbeat",
scheduler,
heartbeatExecutor,
renewalIntervalInSecs,
TimeUnit.SECONDS,
expBackOffBound,
new HeartbeatThread()
),
renewalIntervalInSecs, TimeUnit.SECONDS);

// InstanceInfo replicator
instanceInfoReplicator = new InstanceInfoReplicator(
this,
instanceInfo,
clientConfig.getInstanceInfoReplicationIntervalSeconds(),
2); // burstSize

statusChangeListener = new ApplicationInfoManager.StatusChangeListener() {
@Override
public String getId() {
return "statusChangeListener";
}

@Override
public void notify(StatusChangeEvent statusChangeEvent) {
if (InstanceStatus.DOWN == statusChangeEvent.getStatus() ||
InstanceStatus.DOWN == statusChangeEvent.getPreviousStatus()) {
// log at warn level if DOWN was involved
logger.warn("Saw local status change event {}", statusChangeEvent);
} else {
logger.info("Saw local status change event {}", statusChangeEvent);
}
instanceInfoReplicator.onDemandUpdate();
}
};

if (clientConfig.shouldOnDemandUpdateStatusChange()) {
applicationInfoManager.registerStatusChangeListener(statusChangeListener);
}

instanceInfoReplicator.start(clientConfig.getInitialInstanceInfoReplicationIntervalSeconds());
} else {
logger.info("Not registering with Eureka server per configuration");
}
}

来分别看一下两个定时逻辑

阅读全文 »