您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“java FastDFS文件下載到本地的方法”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!
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數(shù)組到圖片到硬盤上 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文件下載到本地的方法”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。