【无标题】fastdfs 批量下载图片为压缩包
作者:互联网
/** * 下载文件 * 参数 [{图片路径(path),自定义的图片名字(name)}...] */
public byte[] download(ArrayList pathList) { TrackerServer trackerServer = null; StorageServer storageServer = null; try { ClientGlobal.init("fdfs_client.properties"); TrackerClient trackerClient = new TrackerClient(); trackerServer = trackerClient.getConnection(); storageServer = trackerClient.getStoreStorage(trackerServer); StorageClient storageClient = new StorageClient(trackerServer, storageServer); ArrayList list=pathList; for (int i = 0; i < list.size(); i++) { Map map = (Map) list.get(i); //获取 图片路径 String pathMap = (String) map.get("path"); //获取 图片名字 String fileName = (String) map.get("name"); //图片地址 例如:M00/00/01/wKgCYGDULuGATH2sAAD7vWJ6uiM234.jpg String path1 = pathMap.substring(7); //图片名字 String reName = fileName; //返回一个流 byte[] bytes = storageClient.download_file("group1", path1); String runPath = getRunPath(); //文件名字 String dirPath = runPath + "images"; File dirFile = new File(dirPath); //如果文件夹不存在 if (!dirFile.exists()) { //创建文件夹 dirFile.mkdir(); } //格式设置为UTF-8 避免出现乱码 String name = new String(reName.getBytes("UTF-8"), "UTF-8"); //生成文件路径 String path = dirPath + File.separator; saveFile(bytes, path, name + ".jpg"); } } catch (IOException e) { e.printStackTrace(); } catch (MyException e) { e.printStackTrace(); } return null; }
//将字节流写到磁盘生成文件 private void saveFile(byte[] b, String path, String fileName) { File file = new File(path + fileName); FileOutputStream fileOutputStream = null; try { fileOutputStream = new FileOutputStream(file); fileOutputStream.write(b); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (fileOutputStream != null) { try { fileOutputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
/** * 生成zip文件, * 生成zip文件之前,先把之前的zip文件删掉。 */ public long genZip() throws FileNotFoundException { String runPath = getRunPath(); String dirPath = runPath + "images"; String dirPathZip = runPath + "images.zip"; if (FileUtil.exist(dirPathZip)) {//如果存在,就删除 FileUtil.del(dirPathZip); } // 去压缩 Charset charset = Charset.forName("UTF-8"); File file = ZipUtil.zip(dirPath, charset); long length = file.length(); if (file.exists()) { // 删除压缩前的 FileUtil.del(dirPath); } return length; }
// 返回下载链接 public String downLoadUrl() throws FileNotFoundException { System.out.println("调用下载链接"); String runPath = getRunPath(); String dirPath = runPath + "images.zip"; System.out.println("要下载的文件链接为:"); System.out.println(dirPath); return dirPath; }
//执行完下载后,删除文件 public void deleteDic() throws FileNotFoundException { System.out.println("下载完成,开始删除"); String runPath = getRunPath(); String dirPath = runPath + "images.zip"; FileUtil.del(dirPath); }
public static String getRunPath() throws FileNotFoundException { String runPath = "/root/"; if (FileUtil.isWindows()) { runPath = ResourceUtils.getURL(ResourceUtils.CLASSPATH_URL_PREFIX).getPath(); } return runPath; }
标签:String,zip,fastdfs,无标题,runPath,dirPath,catch,new,压缩包 来源: https://blog.csdn.net/weixin_45705681/article/details/122040512