在项目开发中用到的,随手记录一下。
实现步骤如下:
一、yml配置
# ftp服务器配置
ftp:
ip: 192.168.1.58
port: 21
username: zy
passwd: ok
filePath: /home/zh/video
二、控制器
@Value("${ftp.ip}")
private String ftpIp;
@Value("${ftp.port}")
private Integer ftpPort;
@Value("${ftp.username}")
private String username;
@Value("${ftp.passwd}")
private String passwd;
@Value("${ftp.filePath}")
private String filePath;
/**
* 直播视频回顾接口
*
* @param caseCode 案件标识
* @param times 庭次
*/
@GetMapping("/playback")
public AjaxResult playback(@RequestParam("caseCode") String caseCode, @RequestParam("times") Long times) {
BizCaseSchedule bizCaseSchedule = new BizCaseSchedule();
bizCaseSchedule.setCaseCode(caseCode);
bizCaseSchedule.setTimes(times);
List<BizCaseScheduleVO> scheduleList = bizCaseScheduleService.selectBizCaseScheduleList(bizCaseSchedule);
if (CollectionUtils.isEmpty(scheduleList)) {
log.error("没有这个案件排期!");
return AjaxResult.error("没有这个案件排期!");
}
BizTrial bizTrial = bizTrialService.selectBizTrialById(scheduleList.get(0).getArbitralCourtId());
//获取庭审主机IP
String arbitralCourtIp = bizTrial.getIp();
//创建ftp连接
FTPClient ftp = FtpUtil2.connectFTP(ftpIp, ftpPort, username, passwd);
//判断是否连接
if (!ftp.isConnected()) {
log.error("ftp连接失败");
return AjaxResult.error("ftp连接失败");
}
//ftp服务器视频文件夹路径
String path = filePath + "/" + caseCode + "_" + times + "_" + arbitralCourtIp + "_upending";
String urlPath = null;
try {
//变更工作文件夹
ftp.changeWorkingDirectory(path);
//设置被动模式,开通一个端口来传输数据
//ftp.enterLocalPassiveMode();
//获取当前工作文件名列表
FTPFile[] ftpFiles = ftp.listFiles();
//判断当前列表
if (ftpFiles != null && ftpFiles.length > 0) {
for (FTPFile ftpFile : ftpFiles) {
//判断文件名中是否“1_”开头的文件(“1_”开头的mp4文件为--合成流画面)
if (ftpFile.getName().contains("1_")) {
String name = ftpFile.getName();
//http://192.168.1.55:8080/video/17aabccb795b40c88903cc935f82175a_1_192.168.1.10_upending/1_20230313_160545.mp4
urlPath = ftpIp + ":8080/video/" + caseCode + "_" + times + "_" + arbitralCourtIp + "_upending/" + name;
return AjaxResult.success(urlPath);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return AjaxResult.success(urlPath);
}