标签:String zip fastdfs 无标题 runPath dirPath catch new 压缩包
/** * 下载文件 * 参数 [{图片路径(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
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。