【FastDFS】FastDFS java客户端实现文件上传下载删除

首先引入相关jar包。pom文件的相关依赖如下所示。


    
    
      commons-io
      commons-io
      2.0.1
    

    
      org.csource
      fastdfs-client-java
      1.27-SNAPSHOT
    

配置文件client.conf如下所示

charset = UTF-8
http.secret_key = FastDFS1234567890
tracker_server=192.168.224.200:22122
tracker_server=192.168.224.201:22122

加载配置文件:

// 初始化FastDFS Client,静态代码块,只加载一次
    static {
        try {
            //ClientGlobal.init(CONFIG_FILENAME);
            //clientGloble读配置文件
            ClassPathResource resource = new ClassPathResource("client.conf");
            ClientGlobal.init(resource.getClassLoader().getResource("client.conf").getPath());
            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();
        }
    }

上传文件:

public static String uploadFileByStream(FileInputStream fileInputStream, String fileName, Map metaList) {
        try {
            //把file转化成byte数组
            byte[] buff = IOUtils.toByteArray(fileInputStream);
            NameValuePair[] nameValuePairs = null;
            if (metaList != null) {
                nameValuePairs = new NameValuePair[metaList.size()];
                int index = 0;
                for (Iterator> iterator = metaList.entrySet().iterator(); iterator.hasNext();) {
                    Map.Entry entry = iterator.next();
                    String name = entry.getKey();
                    String value = entry.getValue();
                    nameValuePairs[index++] = new NameValuePair(name,value);
                }
            }
            return storageClient1.upload_file1(buff,  fileName.substring(fileName.lastIndexOf(".") + 1),nameValuePairs);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
public static String uploadFileByStream(FileInputStream fileInputStream, String fileName) {
       return uploadFileByStream(fileInputStream,fileName,null);
    }

下载(浏览)文件

1、内置WebServer

/**
     * 把输出流返回给前端
     * @param fileId 文件ID(上传文件成功后返回的ID)
     * @return
     */
    public static void  downloadFile(HttpServletResponse response,String fileId) {
        try {
            byte[] content = storageClient1.download_file1(fileId);//得到文件的字节数组
            OutputStream outputStream = response.getOutputStream();
            outputStream.write(content); // 输出数据
            outputStream.flush();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

2、外部webServer

拼接URL:vip+映射路径+fileId(组名+文件名)

删除文件:

 //删除失败返回-1,否则返回0    
    public static int deleteFile(String fileId) {
        try {
            return storageClient1.delete_file1(fileId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(-1);
        return -1;
    }

获取文件源数据

科普一把:什么是元数据,官方解释为描述数据的数据。比如查看某人的信息。姓名、性别、出生日期、电子照片等都是数据。其中电子照片的width、height、dpi都是用来描述电子照片的。因此width、height、dpi称为元数据。

public static Map getFileMetadata(String fileId) {
        try {
            NameValuePair[] metaList = storageClient1.get_metadata1(fileId);
            if (metaList != null) {
                HashMap map = new HashMap();
                for (NameValuePair metaItem : metaList) {
                    map.put(metaItem.getName(),metaItem.getValue());
                }
                return map;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


你可能感兴趣的:(FastDFS)