VehicleDataSyncController.java
3.2 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.bsth.controller.schedule.datasync;
import com.bsth.controller.schedule.BController;
import com.bsth.controller.schedule.datasync.common.ApiResult;
import com.bsth.entity.schedule.datasync.VehicleDataSyncLog;
import com.bsth.service.schedule.datasync.VehicleDataSyncService;
import com.bsth.service.schedule.datasync.task.VehicleDataSyncTaskFlag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
@RestController
@RequestMapping("dataSync/vehicle")
public class VehicleDataSyncController extends BController<VehicleDataSyncLog, Long> {
@Autowired
private VehicleDataSyncService vehicleDataSyncService;
/**
* 同步标识。
* @return
*/
@GetMapping(value = "/flag")
public ApiResult dataSyncFlag() {
try {
VehicleDataSyncTaskFlag taskFlag = VehicleDataSyncTaskFlag
.builder() // TODO:以后加别的参数
.build();
this.vehicleDataSyncService.addToDataSyncTaskQueue(taskFlag);
return ApiResult.success(
"200",
"添加同步任务成功",
"添加同步任务成功");
} catch (Exception exp) {
return ApiResult.failure(
"500",
"添加同步任务失败,内部错误:" + exp.getMessage());
}
}
/**
* 获取同步日志。
* @param response
* @param id 日志Id
*/
@GetMapping(value = "/logfile/{id}")
public void exportTaskLogFile(
HttpServletResponse response,
@PathVariable("id") long id) {
File file = this.vehicleDataSyncService.getTaskLogFile(id);
if (file == null || !file.exists()) {
throw new RuntimeException("日志文件不存在!");
}
try {
responseStreamFile(response, file);
} catch (Exception exp) {
exp.printStackTrace();
throw new RuntimeException("获取同步日志文件错误:" + exp.getMessage());
}
}
// 流输出文件
private void responseStreamFile(HttpServletResponse response, File file) throws IOException {
// 流输出导出文件
response.setHeader("content-type", "application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=" + file.getName());
response.setContentType("application/octet-stream");
try (
OutputStream os = response.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(os);
InputStream is = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(is)
) {
int length;
byte[] temp = new byte[1024 * 10];
while ((length = bis.read(temp)) != -1) {
bos.write(temp, 0, length);
}
bos.flush();
}
}
}