FastDFS 上传下载

FastDFS 是个常用的分布式存储,对于常用的上传、下载操作,写个通用的工具类,供参考。

1、工具类

package com.my.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.csource.common.MyException;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;

/**
 * @author 技术大咖秀
 *
 */
public class FastDFSUtil {
	
	static TrackerClient trackerClient = null;
	static TrackerServer trackerServer = null;
	static StorageClient1 storageClient1 = null;
	static StorageServer storageServer = null;
    
    /**
     * fastdfs 批量上传
     * @param files 待上传的本地文件
     */
    public static void batchUpload(File[] files) {	
    	if(files!=null && files.length>0){
    		for (int i=0; i<files.length; i++) {
	    		try {
	        		ClientGlobal.init("fastdfs-client-prod.conf"); // 初始化文件
	                trackerClient = new TrackerClient(); // 创建 tracker 客户端
	                trackerServer = trackerClient.getConnection(); // 与tracker server建立连接
	                storageClient1 = new StorageClient1(trackerServer, storageServer); // 获取storeage
	                String flag = storageClient1.upload_file1("待上传文件的本地路径", null, null); // 上传文件后返回文件标识
	                System.out.println(flag);
	    		} catch (Exception e) {
	    			e.printStackTrace();
	    		}
    		}
    	} else {
    		System.out.println("文件不存在");
    	}
    }
    
    /**
     * 单个文件下载
     * @param targetDir 下载目录
     * @param fileMap, map中包含file_name、file_path(来自db查询)
     */
    public static void sigleDownload(String targetDir, Map fileMap) {
    	File file = null;
    	String fileName = (String) fileMap.get("FILE_NAME");
        String filePath = (String) fileMap.get("FILE_PATH");
        filePath = filePath.substring(1); // filepath处理(去除掉开头的斜杠/)
        
    	try {
    		ClientGlobal.init("fastdfs-client-prod.conf"); // 初始化文件
            trackerClient = new TrackerClient();
            trackerServer = trackerClient.getConnection();
            storageClient1 = new StorageClient1(trackerServer, storageServer);
            byte[] by = storageClient1.download_file1(filePath); // 下载(注意开头没有/),group1/M00/01/84/CjRBZ16eo3-ECNtxAAAAALAVfnM04.docx
            
            String targetPath = targetDir + File.separatorChar + fileName; // 创建下载到的文件夹路径
            file = new File(targetPath);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            
            IOUtils.write(by, new FileOutputStream(targetPath)); // 把流写入文件
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 批量下载
     * @param targetDir
     * @param files 待下载的文件,map中包含file_name、file_path(来自db查询)
     */
    public static void batchDownload(String targetDir, List<Map> files) {
    	if(files!=null && files.size()>0){
    		for (int i=0; i<files.size(); i++) {
	    		try {
	        		ClientGlobal.init("fastdfs-client-prod.conf"); // 初始化文件
	                trackerClient = new TrackerClient();
	                trackerServer = trackerClient.getConnection();
	                storageClient1 = new StorageClient1(trackerServer, storageServer);
	                
	                String fileName = (String) files.get(i).get("FILE_NAME");
	                String filePath = (String) files.get(i).get("FILE_PATH");
	                filePath = filePath.substring(1); // filepath处理(去除掉开头的斜杠/)
	                
	                byte[] by = storageClient1.download_file1(filePath); // 下载(注意开头没有/),group1/M00/01/84/CjRBZ16eo3-ECNtxAAAAALAVfnM04.docx
	                
	                String targetPath = targetDir + File.separatorChar + fileName; // 创建下载到的文件夹路径
	                File file = new File(targetPath);
	                if (!file.getParentFile().exists()) {
	                    file.getParentFile().mkdirs();
	                }
	                
	                IOUtils.write(by, new FileOutputStream(targetPath)); // 把流写入文件
	            } catch (IOException e) {
	                e.printStackTrace();
	            } catch (MyException e) {
	                e.printStackTrace();
	            }
    		}
    	}else {
    		System.out.println("文件不存在");
    	}
    }
    
    public static void main(String[] args) {
    	List<Map> files = new ArrayList<>();
    	Map map = new HashMap();
    	map.put("FILE_NAME", "importData.xlsx");
    	map.put("FILE_PATH", "/group1/M00/01/84/CjRBZl6epCiEEU0_AAAAAMpXeHo90.xlsx");
    	files.add(map);
    	
    	batchDownload("download", files);
    	System.out.append("ok");
	}

}

2、fastdfs配置

connect_timeout = 60
network_timeout = 60
charset = UTF-8
http.tracker_http_port = 80
http.anti_steal_token = no
http.secret_key = password

tracker_server = xx.xx.xx.xx:22122

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值