温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

java FastDFS文件下载到本地的方法

发布时间:2021-07-02 16:45:47 来源:亿速云 阅读:468 作者:chen 栏目:大数据

本篇内容介绍了“java FastDFS文件下载到本地的方法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

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<BaseFrameCh> clickHouseBaseDao;

    @Autowired
    private FastFileStorageClient storageClient;

    @PostMapping("/download")
    public void test() {
        //查找这个时间段的所有摄像头
        List<String> 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<String> s = new HashSet<>();
        strings1.forEach(e->{
            s.add(e);
        });
        for (String s1 : s) {
            List<String> 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;
    }
}

“java FastDFS文件下载到本地的方法”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI