zoukankan      html  css  js  c++  java
  • FastDFS单机搭建以及java客户端Demo

    http://blog.csdn.net/u012453843/article/details/69951920

    http://blog.csdn.net/xyang81/article/details/52847311

    http://blog.csdn.net/kingboyworld/article/details/52299602

    参考了这几个搭建了FastDFS文件系统

    主要是fastDFS,nginx,以及在nginx中加入fastDFS模块;这里只有一台服务器,所以搭建的是单机版的。

    至于java客户端的包可以自己用maven编译,分分钟的事儿

    https://github.com/happyfish100/fastdfs-client-java

    编译完了打包到maven仓库中,直接用下面这个dependency

    <dependency>
        <groupId>org.csource</groupId>
        <artifactId>fastdfs-client-java</artifactId>
        <version>1.27-SNAPSHOT</version>
    </dependency>

    demo里面一共就引了这么几个:

    <dependencies>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>org.csource</groupId>
            <artifactId>fastdfs-client-java</artifactId>
            <version>1.27-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    客户端操作的Demo:

    public class FileUtilsDemo {
    
        private static StorageClient1 storageClient1 = null;
    
        // 初始化FastDFS Client
        static {
            try {
                ClassLoader classLoader = FileUtilsDemo.class.getClassLoader();
                URL resource = classLoader.getResource("fastdfs.conf");
                String path = resource.getPath();
                System.out.println(path);
                ClientGlobal.init(path);
                TrackerClient trackerClient = new TrackerClient(ClientGlobal.g_tracker_group);
                TrackerServer trackerServer = trackerClient.getConnection();
                if (trackerServer == null) {
                    throw new IllegalStateException("getConnection return null");
                }
                StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
                if (storageServer == null) {
                    throw new IllegalStateException("getStoreStorage return null");
                }
                storageClient1 = new StorageClient1(trackerServer,storageServer);
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 上传文件
         * @param file 文件对象
         * @param fileName 文件名
         * @return
         */
        public static String uploadFile(File file, String fileName) {
            return uploadFile(file,fileName,null);
        }
    
        /**
         * 上传文件
         * @param file 文件对象
         * @param fileName 文件名
         * @param metaList 文件元数据
         * @return
         */
        public static String uploadFile(File file, String fileName, Map<String,String> metaList) {
            try {
                byte[] buff = IOUtils.toByteArray(new FileInputStream(file));
                NameValuePair[] nameValuePairs = null;
                if (metaList != null) {
                    nameValuePairs = new NameValuePair[metaList.size()];
                    int index = 0;
                    for (Iterator<Map.Entry<String,String>> iterator = metaList.entrySet().iterator(); iterator.hasNext();) {
                        Map.Entry<String,String> entry = iterator.next();
                        String name = entry.getKey();
                        String value = entry.getValue();
                        nameValuePairs[index++] = new NameValuePair(name,value);
                    }
                }
    
    
                return storageClient1.upload_file1(buff, FilenameUtils.getExtension(fileName),nameValuePairs);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    
        /**
         * 获取文件元数据
         * @param fileId 文件ID
         * @return
         */
        public static Map<String,String> getFileMetadata(String fileId) {
            try {
                NameValuePair[] metaList = storageClient1.get_metadata1(fileId);
                if (metaList != null) {
                    HashMap<String,String> map = new HashMap<String, String>();
                    for (NameValuePair metaItem : metaList) {
                        map.put(metaItem.getName(),metaItem.getValue());
                    }
                    return map;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    
        /**
         * 删除文件
         * @param fileId 文件ID
         * @return 删除失败返回-1,否则返回0
         */
        public static int deleteFile(String fileId) {
            try {
                return storageClient1.delete_file1(fileId);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return -1;
        }
    
        /**
         * 下载文件
         * @param fileId 文件ID(上传文件成功后返回的ID)
         * @param outFile 文件下载保存位置
         * @return
         */
        public static int downloadFile(String fileId, File outFile) {
            FileOutputStream fos = null;
            try {
                byte[] content = storageClient1.download_file1(fileId);
                fos = new FileOutputStream(outFile);
                IOUtils.copy(new ByteInputStream(content, content.length),fos);
                return 0;
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (fos != null) {
                    try {
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return -1;
        }
    }

    客户端测试的Demo:

    public class FastDFSClientTest {
    
        /**
         * 文件上传测试
         */
        @Test
        public void testUpload() {
            File file = new File("C:\Users\023b5bb5c9ea15ce4ed7034ebc003af33a87b2b8.jpg");
            Map<String,String> metaList = new HashMap<String, String>();
            metaList.put("width","1024");
            metaList.put("height","768");
            metaList.put("date","20161018");
            String fid = FileUtilsDemo.uploadFile(file,file.getName(), metaList);
            System.out.println("upload local file " + file.getPath() + " ok, fileid=" + fid);
        }
    
        /**
         * 文件下载测试
         */
        @Test
        public void testDownload() {
            int r = FileUtilsDemo.downloadFile("group1/M00/00/00/cjdUkFo3fXeAAvUDAAFiCZy66os105.jpg", new File("DownloadFile_fid.jpg"));
            System.out.println(r == 0 ? "下载成功" : "下载失败");
        }
    
        /**
         * 获取文件元数据测试
         */
        @Test
        public void testGetFileMetadata() {
            Map<String,String> metaList = FileUtilsDemo.getFileMetadata("group1/M00/00/00/wKgAyVgFk9aAB8hwAA-8Q6_7tHw351.jpg");
            for (Iterator<Map.Entry<String,String>> iterator = metaList.entrySet().iterator(); iterator.hasNext();) {
                Map.Entry<String,String> entry = iterator.next();
                String name = entry.getKey();
                String value = entry.getValue();
                System.out.println(name + " = " + value );
            }
        }
    
        /**
         * 文件删除测试
         */
        @Test
        public void testDelete() {
            int r = FileUtilsDemo.deleteFile("group1/M00/00/00/wKgAyVgFk9aAB8hwAA-8Q6_7tHw351.jpg");
            System.out.println(r == 0 ? "删除成功" : "删除失败");
        }
    
    }

    配置文件:

    connect_timeout = 10
    network_timeout = 30
    charset = UTF-8
    http.tracker_http_port = 8999
    http.anti_steal_token = no
    http.secret_key = FastDFS1234567890
    tracker_server = 192.168.88.520:22122

    反正我这个文件里面写了注释,然后就报错了,我就把注释删除了就好了。一套下来接近2个小时可以搞定,搞不定问我,哈哈

    image

  • 相关阅读:
    图像处理基本算法(整理)
    Java 数据校验自动化(validation)
    Java Web文件上传
    JavaScript中call、apply、bind、slice的使用
    在不借助其他工具的情况下破解Windows开机密码
    【Docker】iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 8480 -j DNAT --to-destination 172.17.0.2:80 ! -i docker0: iptables: No chain/target/match by that name
    【异常】Caused by: java.lang.IllegalStateException: RequestParam.value() was empty on parameter 0
    【Docker】docker的安装和常用命令
    【监控】jvisualvm之jmx远程连接 jar启动应用
    【监控】jvisualvm之jmx远程连接 tomcat war启动应用
  • 原文地址:https://www.cnblogs.com/tuhooo/p/8058424.html
Copyright © 2011-2022 走看看