FastDFS入门

java操作fastDFS

1、添加jar
  <dependency>
            <groupId>net.oschina.zcx7878</groupId>
            <artifactId>fastdfs-client-java</artifactId>
            <version>1.27.0.0</version>
 </dependency>
2、classpath下添加配置文件fdfs_client.conf
connect_timeout = 2
network_timeout = 30
charset = UTF-8
# tracker Http端口
http.tracker_http_port = 8888
# 暂无作用
http.anti_steal_token = no
# 暂无作用
http.secret_key = FastDFS1234567890
# tracker Server TCP通信地址信息
tracker_server = 124.0.0.0:22122
3、工具类
public class FastDFSClient {
    private static TrackerClient trackerClient = null;
    private static TrackerServer trackerServer = null;
    private static StorageServer storageServer = null;
    private static StorageClient1 storageClient = null;

    static {
        try {
            String fileName = new ClassPathResource("fdfs_client.conf").getPath();
            //加载 tracker 消息
            ClientGlobal.init(fileName);
            //创建TrackerClient客户端对象
            trackerClient = new TrackerClient();
            //通过TrackerClient对象获取TrackerServer信息
            trackerServer = trackerClient.getConnection();
            //storageServer 不需要配置
            //获取StorageClient对象
            storageClient = new StorageClient1(trackerServer, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @param fileContent 文件的内容,字节数组 (MultipartFile可获取)
     * @param extName     文件扩展名
     * @param metas       文件扩展信息 传null就行
     *                    NameValuePair[] meta_list = new NameValuePair[1];
     *                    meta_list[0] = new NameValuePair("author","小舞");
     * @return String[]s   s[1]文件上传所存储的组名 s[2]文件存储路径
     * @throws IOException
     */
    public static String[] uploadFile(byte[] fileContent, String extName, NameValuePair[] metas) throws IOException {
        try {
            //upload_file1 比 upload_file多封装一层
            return storageClient.upload_file(fileContent, extName, metas);
        } catch (MyException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 上传文件方法2
     *
     * @param fileName 文件全路径
     * @param extName  文件扩展名,不包含(.)
     * @param metas    文件扩展信息
     * @return
     * @throws Exception
     */
    public static String uploadFile(String fileName, String extName, NameValuePair[] metas) {
        try {
            //upload_file1 比 upload_file多封装一层
            return storageClient.upload_file1(fileName, extName, metas);
        } catch (IOException | MyException e) {
            e.printStackTrace();
        }
        return null;
    }

    /***
     * 获取文件信息
     * @param groupName:组名
     * @param remoteFileName:文件存储完整名
     */
    public static FileInfo getFile(String groupName, String remoteFileName) {
        try {
            //获取文件信息
            return storageClient.get_file_info(groupName, remoteFileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /***
     * 文件下载
     * @param groupName:组名
     * @param remoteFileName:文件存储完整名(去除组名)
     * @return
     */
    public static InputStream downFile(String groupName, String remoteFileName) {
        try {
            //通过StorageClient下载文件
            byte[] fileByte = storageClient.download_file(groupName, remoteFileName);
            //将字节数组转换成字节输入流
            return new ByteArrayInputStream(fileByte);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /***
     * 文件删除实现
     * @param groupName:组名
     * @param remoteFileName:文件存储完整名(去除组名)
     */
    public static void deleteFile(String groupName, String remoteFileName) {
        try {
            //通过StorageClient删除文件
            storageClient.delete_file(groupName, remoteFileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /***
     * 获取组信息
     * @param groupName :组名
     */
    public static StorageServer getStorage(String groupName) {
        try {
            //通过trackerClient获取Storage组信息
            return trackerClient.getStoreStorage(trackerServer, groupName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /***
     * 根据文件组名和文件存储路径获取Storage服务的IP、端口信息
     * @param groupName :组名
     * @param remoteFileName :文件存储完整名
     */
    public static ServerInfo[] getServerInfo(String groupName, String remoteFileName) {
        try {
            //获取服务信息
            return trackerClient.getFetchStorages(trackerServer, groupName, remoteFileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /***
     * 获取Tracker服务地址
     */
    public static String getTrackerUrl() {
        try {
            //获取Tracker地址
            return "http://" + trackerServer.getInetSocketAddress().getHostString() + ":" + ClientGlobal.getG_tracker_http_port();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
删除文件,文件还能显示,需要配置nginx的禁止缓存
location ~/group[0-9]/ {
   add_header Cache-Control no-store;
   ngx_fastdfs_module;
}
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页