新增根据 指定时间范围的 recordInfo 查询, 预下载视频文件到本地指定路径

This commit is contained in:
shikong 2024-02-06 16:22:45 +08:00
parent 838efa92e2
commit b6217e54c5

View File

@ -12,9 +12,9 @@ import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.hc.client5.http.classic.HttpClient;
import org.apache.hc.client5.http.classic.methods.HttpGet; import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.config.ConnectionConfig; import org.apache.hc.client5.http.config.ConnectionConfig;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients; import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager; import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Qualifier;
@ -44,6 +44,8 @@ public class VideoCacheManager {
@PostConstruct @PostConstruct
private void init(){ private void init(){
manager.setMaxTotal(100);
manager.setDefaultMaxPerRoute(100);
manager.setDefaultConnectionConfig( manager.setDefaultConnectionConfig(
ConnectionConfig.custom() ConnectionConfig.custom()
.setConnectTimeout(5, TimeUnit.MINUTES) .setConnectTimeout(5, TimeUnit.MINUTES)
@ -100,19 +102,23 @@ public class VideoCacheManager {
FileChannel channel = outputStream.getChannel(); FileChannel channel = outputStream.getChannel();
FileLock lock = channel.lock(); FileLock lock = channel.lock();
try (CloseableHttpClient client = HttpClients.custom().setConnectionManager(manager).build()) { HttpClient client = HttpClients.custom()
HttpGet httpGet = new HttpGet(url); .setConnectionManager(manager)
client.execute(httpGet, response -> { .setConnectionManagerShared(true)
InputStream stream = response.getEntity().getContent(); .build();
IoUtil.copy(stream,outputStream);
return stream; HttpGet httpGet = new HttpGet(url);
}); InputStream execute = client.execute(httpGet, response -> {
log.info("视频下载完成 => {}", file.getAbsolutePath()); InputStream stream = response.getEntity().getContent();
log.info("文件 {}, 是否存在: {}", file.getAbsolutePath(), file.exists()); IoUtil.copy(stream, outputStream);
file.renameTo(realFile); return stream;
lock.release(); });
return JsonResponse.success(realFile.getAbsolutePath()); execute.close();
} log.info("视频下载完成 => {}", file.getAbsolutePath());
log.info("文件 {}, 是否存在: {}", file.getAbsolutePath(), file.exists());
file.renameTo(realFile);
lock.release();
return JsonResponse.success(realFile.getAbsolutePath());
} catch (Exception e) { } catch (Exception e) {
log.error("视频下载失败 => {}", e.getMessage()); log.error("视频下载失败 => {}", e.getMessage());
file.delete(); file.delete();