淘先锋技术网

首页 1 2 3 4 5 6 7

 

maven依赖

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

fastDFS配置文件(本示例放在resources目录下文件名叫fdfs_client.conf)

connect_timeout = 10
network_timeout = 30
charset = UTF-8
http.tracker_http_port = 8080
http.anti_steal_token = no
http.secret_key = FASTDFS1234567890
tracker_server = 192.168.179.137:22122

 工具类

/**
 * @description: FastDFS工具类
 * @author: dayibalang
 * @time: 2020/7/22 9:18
 */
public class FastDFSUtil {



    private static Logger logger = LoggerFactory.getLogger(FastDFSUtil.class);

    private static final String CONF_FILENAME =  "fdfs_client.conf";

    static {
        String filePath = (FastDFSUtil.class.getClass().getResource("/").getPath()+CONF_FILENAME).replaceAll("//","\\");
        try {
            ClientGlobal.init(filePath);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }
    }

    /**
    * 功能描述:文件上传
    * @author dayibalang
    * @date 2020/7/22
    * @param file
    * @return java.lang.String
    */
    public static String uploadFile(File file) throws IOException, MyException{
        FileInputStream fis = null;
        NameValuePair[] meta_list = null;
        TrackerClient trackerClient = new TrackerClient(ClientGlobal.g_tracker_group);
        TrackerServer trackerServer = trackerClient.getTrackerServer();
        StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
        StorageClient1 storageClient = new StorageClient1(trackerServer, storageServer);
        fis = new FileInputStream(file);
        byte[] file_buff = null;
        if (fis != null) {
            int len = fis.available();
            file_buff = new byte[len];
            fis.read(file_buff);
        }
        String fileId = storageClient.upload_file1(file_buff, getFileExt(file.getName()), meta_list);
        return fileId;
    }

    /**
    * 功能描述:文件下载
    * @author dayibalang
    * @date 2020/7/22
    * @param fileId
    * @return java.io.InputStream
    */
    public static InputStream downloadFile(String fileId) throws IOException, MyException {
        TrackerClient trackerClient = new TrackerClient(ClientGlobal.g_tracker_group);
        TrackerServer trackerServer = trackerClient.getTrackerServer();
        StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
        StorageClient1 storageClient = new StorageClient1(trackerServer, storageServer);
        byte[] bytes = storageClient.download_file1(fileId);
        InputStream inputStream = new ByteArrayInputStream(bytes);
        inputStream.close();
        return inputStream;
    }



    /**
    * 功能描述:获取文件名后缀(不包含点)
    * @author dayibalang
    * @date 2020/7/22
    * @param fileName
    * @return java.lang.String
    */
    private static String getFileExt(String fileName) {
        if (fileName == null || !fileName.contains(".")) {
            return "";
        } else {
            return fileName.substring(fileName.lastIndexOf(".") + 1); // 不带最后的点
        }
    }


    /**
    * 功能描述: 删除文件
    * @author dayibalang
    * @date 2020/7/22
    * @param fileId
    * @return int
    */
    public static int deleteFile(String fileId) throws IOException, MyException {
        TrackerClient trackerClient = new TrackerClient(ClientGlobal.g_tracker_group);
        TrackerServer trackerServer = trackerClient.getTrackerServer();
        StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
        StorageClient1 storageClient = new StorageClient1(trackerServer, storageServer);
        int result = storageClient.delete_file1(fileId);
        return result;
    }

}

测试代码

public class App {
    public static void main( String[] args ) throws IOException {

        // 测试上传
        String s = FastDFSUtil.uploadFile(new File("C:\\Users\\px\\Desktop\\3.png"));
        System.out.println(s);

        //测试下载
        InputStream inputStream = FastDFSUtil.downloadFile("group1/M00/00/00/wKizjV8YJe6AUzMLAAL3YEsRLGg666.png");
        File file = new File("E:/666.png");
        if(!file.exists()){
            file.createNewFile();
        }
        FileOutputStream destFile = new FileOutputStream(file);
        byte[] b = new byte[1024];
        int length;
        while((length= inputStream.read(b))>0){
            destFile.write(b,0,length);
        }

        //测试删除
        int i = FastDFSUtil.deleteFile("group1/M00/00/00/wKizil8V9-mAURsvAAL3YEsRLGg573.png");
        System.out.println(i);
    }
}