java FastDFS文件下载到本地

雨点打透心脏的1/2处 2024-04-18 17:50 142阅读 0赞
  1. package cn.bywin.cbvsp.controller;
  2. import cn.bywin.cbvsp.DAL.BO.po.clickhouse.BaseFrameCh;
  3. import cn.bywin.cbvsp.DAL.DAO.clickhouse.ClickHouseBaseDao;
  4. import com.github.tobato.fastdfs.domain.proto.storage.DownloadByteArray;
  5. import com.github.tobato.fastdfs.service.FastFileStorageClient;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Component;
  8. import org.springframework.web.bind.annotation.PostMapping;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RestController;
  11. import javax.imageio.stream.FileImageOutputStream;
  12. import java.io.File;
  13. import java.util.HashSet;
  14. import java.util.List;
  15. /**
  16. * @author linbin
  17. * @create 2019-07-25 15:37
  18. */
  19. @RestController
  20. @Component
  21. @RequestMapping("linbin")
  22. public class DownLoadController {
  23. @Autowired
  24. private ClickHouseBaseDao<BaseFrameCh> clickHouseBaseDao;
  25. @Autowired
  26. private FastFileStorageClient storageClient;
  27. @PostMapping("/download")
  28. public void test() {
  29. //查找这个时间段的所有摄像头
  30. 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'");
  31. HashSet<String> s = new HashSet<>();
  32. strings1.forEach(e->{
  33. s.add(e);
  34. });
  35. for (String s1 : s) {
  36. 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+"';");
  37. buildFolder("/home/deploy/191-20to820-video/"+s1+"");
  38. strings.forEach(e -> {
  39. if(e.contains("35.26.59.199")){
  40. return;
  41. }
  42. testHome(e,s1);
  43. });
  44. }
  45. }
  46. public void testHome(String fileUrl,String dir) {
  47. String path = fileUrl.substring(32);
  48. int i = path.indexOf('/');
  49. int i2 = path.indexOf("/", i + 1);
  50. int i3 = path.indexOf("/", i2 + 1);
  51. String ss = path.substring(i3 + 1);
  52. DownloadByteArray downloadByteArray = new DownloadByteArray();
  53. byte[] bytes = storageClient.downloadFile("group1", path, downloadByteArray);
  54. byte2image(bytes, "/home/deploy/191-20to820-video/"+dir+"/" + ss);
  55. }
  56. //byte数组到图片到硬盘上
  57. public void byte2image(byte[] data, String path) {
  58. if (data.length < 3 || path.equals("")) return;//判断输入的byte是否为空
  59. try {
  60. FileImageOutputStream imageOutput = new FileImageOutputStream(new File(path));//打开输入流
  61. imageOutput.write(data, 0, data.length);//将byte写入硬盘
  62. imageOutput.close();
  63. System.out.println("Make Picture success,Please find image in " + path);
  64. } catch (Exception ex) {
  65. System.out.println("Exception: " + ex);
  66. ex.printStackTrace();
  67. }
  68. }
  69. public static String buildFolder(String path) {
  70. //读取目录路径
  71. File file = new File(path);
  72. //推断是否存在
  73. if (!file.exists() && !file.isDirectory()) {
  74. try {
  75. System.out.println("目录不存在!");
  76. //生成目录
  77. file.mkdir();
  78. } catch (Exception e) {
  79. e.printStackTrace();
  80. }
  81. } else {
  82. System.out.println("目录存在!");
  83. }
  84. return path;
  85. }
  86. }

转载于:https://my.oschina.net/u/4131327/blog/3079134

发表评论

表情:
评论列表 (有 0 条评论,142人围观)

还没有评论,来说两句吧...

相关阅读