给我一个以上步骤的代码
时间: 2024-03-21 20:39:27 浏览: 112
以下是一个基于 Easy Excel 和 Spring Boot 的数据库导入进度条的代码示例:
WebSocket 配置类:
```java
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
@Autowired
private DatabaseImportHandler databaseImportHandler;
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(databaseImportHandler, "/database-import").setAllowedOrigins("*");
}
@Bean
public HandlerInterceptor webSocketInterceptor() {
return new WebSocketInterceptor();
}
}
```
WebSocket 处理器类:
```java
@Component
public class DatabaseImportHandler extends TextWebSocketHandler {
private Map<String, WebSocketSession> sessions = new ConcurrentHashMap<>();
public void sendImportProgress(String sessionId, int progress) {
WebSocketSession session = sessions.get(sessionId);
if (session != null && session.isOpen()) {
try {
session.sendMessage(new TextMessage(String.valueOf(progress)));
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
sessions.put(session.getId(), session);
}
@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
sessions.remove(session.getId());
}
}
```
异步任务类:
```java
@Service
public class DatabaseImportTask {
@Autowired
private DatabaseImportHandler databaseImportHandler;
@Async
public void importData(WebSocketSession session, String filePath) {
try (InputStream inputStream = new FileInputStream(filePath)) {
ExcelReader reader = EasyExcel.read(inputStream).build();
ExcelListener listener = new ExcelListener(databaseImportHandler, session);
reader.read(new Sheet(1, 1), listener);
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
控制器类:
```java
@RestController
@RequestMapping("/database")
public class DatabaseController {
@Autowired
private DatabaseImportTask databaseImportTask;
@MessageMapping("/import")
public void startImport(WebSocketSession session, String filePath) {
databaseImportTask.importData(session, filePath);
}
}
```
Excel 监听器类:
```java
public class ExcelListener extends AnalysisEventListener<Object> {
private DatabaseImportHandler databaseImportHandler;
private WebSocketSession session;
private int totalCount;
private int currentCount;
public ExcelListener(DatabaseImportHandler databaseImportHandler, WebSocketSession session) {
this.databaseImportHandler = databaseImportHandler;
this.session = session;
}
@Override
public void invoke(Object object, AnalysisContext context) {
// 插入数据到数据库
// ...
// 更新导入进度
currentCount++;
int progress = (int) (((double) currentCount / totalCount) * 100);
databaseImportHandler.sendImportProgress(session.getId(), progress);
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
// 导入完成
}
@Override
public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
totalCount = context.readRowHolder().getApproximateTotalRowNumber();
}
}
```
客户端页面代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>Database Import Progress</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
.progress {
width: 50%;
height: 30px;
margin: 50px auto;
border: 1px solid #ccc;
border-radius: 15px;
overflow: hidden;
}
.bar {
height: 100%;
background: green;
transition: width 0.3s;
}
.text {
position: absolute;
width: 100%;
text-align: center;
line-height: 30px;
color: white;
}
</style>
</head>
<body>
<div class="progress">
<div class="bar"></div>
<div class="text">0%</div>
</div>
<script>
const webSocket = new WebSocket("ws://" + window.location.host + "/database-import");
webSocket.onmessage = function(event) {
const progress = parseInt(event.data);
const bar = document.querySelector(".bar");
const text = document.querySelector(".text");
bar.style.width = progress + "%";
text.innerHTML = progress + "%";
}
</script>
</body>
</html>
```
注意:以上代码仅为示例代码,实际使用时需要根据具体情况进行修改和适配。
阅读全文