fastdfs java 下载_java FastDFS文件下载到本地

该代码段展示了一个Spring Boot控制器,用于根据指定时间范围从ClickHouse数据库中查询摄像头源,并进一步获取对应视频文件ID。通过FastDFS客户端下载文件到指定目录,同时处理文件保存。主要涉及数据库查询、文件操作和分布式存储。
摘要由CSDN通过智能技术生成

package cn.bywin.cbvsp.controller;

import cn.bywin.cbvsp.DAL.BO.po.clickhouse.BaseFrameCh;

import cn.bywin.cbvsp.DAL.DAO.clickhouse.ClickHouseBaseDao;

import com.github.tobato.fastdfs.domain.proto.storage.DownloadByteArray;

import com.github.tobato.fastdfs.service.FastFileStorageClient;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Component;

import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.imageio.stream.FileImageOutputStream;

import java.io.File;

import java.util.HashSet;

import java.util.List;

/**

* @author linbin

* @create 2019-07-25 15:37

*/

@RestController

@Component

@RequestMapping("linbin")

public class DownLoadController {

@Autowired

private ClickHouseBaseDao clickHouseBaseDao;

@Autowired

private FastFileStorageClient storageClient;

@PostMapping("/download")

public void test() {

//查找这个时间段的所有摄像头

List strings1 = clickHouseBaseDao.selectByTime("select video_source_id from cbvsp.base_fragment where real_time between '2019-07-24 20:00:00' and '2019-07-24 20:10:00'");

HashSet s = new HashSet<>();

strings1.forEach(e->{

s.add(e);

});

for (String s1 : s) {

List strings = clickHouseBaseDao.selectByTime("select file_id from cbvsp.base_fragment where real_time between '2019-07-24 20:00:00' and '2019-07-24 20:10:00' and video_source_id='"+s1+"';");

buildFolder("/home/deploy/191-20to820-video/"+s1+"");

strings.forEach(e -> {

if(e.contains("35.26.59.199")){

return;

}

testHome(e,s1);

});

}

}

public void testHome(String fileUrl,String dir) {

String path = fileUrl.substring(32);

int i = path.indexOf('/');

int i2 = path.indexOf("/", i + 1);

int i3 = path.indexOf("/", i2 + 1);

String ss = path.substring(i3 + 1);

DownloadByteArray downloadByteArray = new DownloadByteArray();

byte[] bytes = storageClient.downloadFile("group1", path, downloadByteArray);

byte2image(bytes, "/home/deploy/191-20to820-video/"+dir+"/" + ss);

}

//byte数组到图片到硬盘上

public void byte2image(byte[] data, String path) {

if (data.length < 3 || path.equals("")) return;//判断输入的byte是否为空

try {

FileImageOutputStream imageOutput = new FileImageOutputStream(new File(path));//打开输入流

imageOutput.write(data, 0, data.length);//将byte写入硬盘

imageOutput.close();

System.out.println("Make Picture success,Please find image in " + path);

} catch (Exception ex) {

System.out.println("Exception: " + ex);

ex.printStackTrace();

}

}

public static String buildFolder(String path) {

//读取目录路径

File file = new File(path);

//推断是否存在

if (!file.exists() && !file.isDirectory()) {

try {

System.out.println("目录不存在!");

//生成目录

file.mkdir();

} catch (Exception e) {

e.printStackTrace();

}

} else {

System.out.println("目录存在!");

}

return path;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值