zoukankan      html  css  js  c++  java
  • FastDFS相关

    所需依赖:

    <!-- 文件上传组件 -->
    <dependency>
        <groupId>cn.bestwu</groupId>
        <artifactId>fastdfs-client-java</artifactId>
        <version>1.27</version>
    </dependency>
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
    </dependency>    

    工具类:

      1 import org.csource.common.NameValuePair;
      2 import org.csource.fastdfs.ClientGlobal;
      3 import org.csource.fastdfs.StorageClient1;
      4 import org.csource.fastdfs.StorageServer;
      5 import org.csource.fastdfs.TrackerClient;
      6 import org.csource.fastdfs.TrackerServer;
      7 
      8 public class FastDFSClient {
      9 
     10     private TrackerClient trackerClient = null;
     11     private TrackerServer trackerServer = null;
     12     private StorageServer storageServer = null;
     13     private StorageClient1 storageClient = null;
     14     
     15     public FastDFSClient(String conf) throws Exception {
     16         if (conf.contains("classpath:")) {
     17             conf = conf.replace("classpath:", this.getClass().getResource("/").getPath());
     18         }
     19         ClientGlobal.init(conf);
     20         trackerClient = new TrackerClient();
     21         trackerServer = trackerClient.getConnection();
     22         storageServer = null;
     23         storageClient = new StorageClient1(trackerServer, storageServer);
     24     }
     25     
     26     /**
     27      * 上传文件方法
     28      * <p>Title: uploadFile</p>
     29      * <p>Description: </p>
     30      * @param fileName 文件全路径
     31      * @param extName 文件扩展名,不包含(.)
     32      * @param metas 文件扩展信息
     33      * @return
     34      * @throws Exception
     35      */
     36     public String uploadFile(String fileName, String extName, NameValuePair[] metas) throws Exception {
     37         String result = storageClient.upload_file1(fileName, extName, metas);
     38         return result;
     39     }
     40     
     41     public String uploadFile(String fileName) throws Exception {
     42         return uploadFile(fileName, null, null);
     43     }
     44     
     45     public String uploadFile(String fileName, String extName) throws Exception {
     46         return uploadFile(fileName, extName, null);
     47     }
     48     
     49     /**
     50      * 上传文件方法
     51      * <p>Title: uploadFile</p>
     52      * <p>Description: </p>
     53      * @param fileContent 文件的内容,字节数组
     54      * @param extName 文件扩展名
     55      * @param metas 文件扩展信息
     56      * @return
     57      * @throws Exception
     58      */
     59     public String uploadFile(byte[] fileContent, String extName, NameValuePair[] metas) throws Exception {
     60         
     61         String result = storageClient.upload_file1(fileContent, extName, metas);
     62         return result;
     63     }
     64     
     65     public String uploadFile(byte[] fileContent) throws Exception {
     66         return uploadFile(fileContent, null, null);
     67     }
     68     
     69     public String uploadFile(byte[] fileContent, String extName) throws Exception {
     70         return uploadFile(fileContent, extName, null);
     71     }
     72     
     73     /**
     74      * 文件下载
     75      * @param filePath 文件地址
     76      * @param savePath 本地保存地址
     77      */
     78     public void download(String filePath,String savePath){
     79         try {
     80             byte[] bytes = storageClient.download_file1(filePath);
     81             IOUtils.write(bytes,new FileOutputStream(savePath));
     82         } catch (Exception e) {
     83             e.printStackTrace();
     84         }
     85     }
     86 
     87     /**
     88      * 文件删除 
     89      * @param filePath 文件的地址
     90      * @return
     91      */
     92     
     93     public Boolean deleteFile(String filePath){
     94         try {
     95             int i = storageClient.delete_file1(filePath);
     96             return i==0;
     97         } catch (Exception e) {
     98             e.printStackTrace();
     99             return false;
    100         }
    101     }
    102 
    103     /**
    104      * 获取文件信息
    105      * @param filePath 文件的地址
    106      * @return
    107      */
    108     public String getFileInfo(String filePath){
    109         try {
    110             FileInfo fileInfo = storageClient.get_file_info1(filePath);
    111             String sourceIpAddr = fileInfo.getSourceIpAddr();//文件IP地址
    112             long fileSize = fileInfo.getFileSize();//文件大小
    113             Date createTimestamp = fileInfo.getCreateTimestamp();//文件创建时间
    114             long crc32 = fileInfo.getCrc32();//错误码
    115             return filePath;
    116         } catch (Exception e) {
    117             e.printStackTrace();
    118             return null;
    119         }
    120 
    121     }
    122 }

    上传文件方法:

    @RequestMapping("/upload")
        public Result upload(MultipartFile[] files){
        List<String> list=new ArrayList<>();
            for (MultipartFile file : files) {
                String originalFilename = file.getOriginalFilename();//获取当前文件的全路径加名称
                String extName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);//获取当前上传文件的扩展名
    
                /**
                进行相应的逻辑操作,保存上传文件的信息到数据库
                */
                try {
                    FastDFSClient fastDFSClient=new FastDFSClient("classpath:config/fdfs_client.conf"); //创建FastDFS的客户端
                    String path = fastDFSClient.uploadFile(file.getBytes(), extName);//获取上传数据的二进制字节码,以扩展名为extName的格式存在文件服务器,返回该文件在文件服务器的路径
                    String url=FILE_SERVER_URL+path;  //拼接服务器的ip和该文件在服务器的路径,可以在网站中查询该图片
                    list.add(url);
                } catch (Exception e) {
                    e.printStackTrace();
                    return new Result(false,"上传失败");
                }
            }
            return new Result(true,"上传成功");
        }

    fastDfs配置ip:

    vim /etc/fdfs/tracker.conf
    
    http.server_port = 8080
    use_storage_id = false
    vim /etc/fdfs/storage.conf
    
    http.server_port = 8888
    tracker_server = 公网IP:22122
    vim /etc/fdfs/storage_ids.conf
    <id> <group_name> <ip_or_hostname>
    100001 group1 公网IP
    vim /etc/fdfs/mod_fastdfs.conf
    
    use_storage_id = false
    tracker_server = 公网地址:22122
    vim /etc/fdfs/client.conf
    
    use_storage_id = false
    http.tracker_server_port = 80
    tracker_server = 内网地址:22122
    vim /fastdfs/storage/data/.data_init_flag
    
    last_ip_addr = 内网IP
    last_server_port = 23000
    last_http_port = 8888
    vim /fastdfs/tracker/data/storage_sync_timestamp.dat
    
    group1,内网地址,0,0
    group1,公网IP,0,0
  • 相关阅读:
    内存:你跑慢点行不行?CPU:跑慢点你养我吗?内存:我不管!(内附超全思维导图)
    又能扯皮了!没内存了还能看片?
    写给大忙人看的进程和线程
    Python面试常用的高级用法,怎么动态创建类?
    LeetCode 74,直击BAT经典面试题
    LeetCode 73,为什么第一反应想到的解法很有可能是个坑?
    Golang基础教程——map使用篇
    手把手教你学numpy,从此数据处理不再慌【三】
    从辗转相除法到求逆元,数论算法初体验
    深入了解机器学习决策树模型——C4.5算法
  • 原文地址:https://www.cnblogs.com/ljl5921/p/13827222.html
Copyright © 2011-2022 走看看