FastDFS实现文件上传下载实战

来源:互联网 发布:linux 查看外网ip 编辑:程序博客网 时间:2024/05/18 00:36


       正好,淘淘商城讲这一块的时候,我又想起来当时老徐让我写过一个关于实现FastDFS实现文件上传下载的使用文档,当时结合我们的ITOO的视频系统和毕业论文系统,整理了一下,有根据网上查到的知识,总结了一点东西,下面分享一下


一、FastDFS简介:

        参见博客:http://blog.csdn.net/liweizhong193516/article/details/53234486


二、FastDFS使用流程介绍:        

       我们在itoo的dobbu+zk框架中使用fastdfs+nginx+MySQL实现上传附件的功能,主要原理就是将附件上传到fastdfs得到一个文件的链接路径url,我们获取到这个url将他以字符串形式保存到我们的mysql中,下载的时候获取到这个url直接打开或者下载附件。(url内容参见博客:http://blog.csdn.NET/liweizhong193516/article/details/52556526)

       

               

               

        默认的,我们就知道fastdfs后面还有一个nginx服务器,当我们进行存储文件的时候,我们要通过fastdfs给我们生成一个保存地址,也就是grounpID 和保存地址,然后将文件存在fastdfs服务器中,fastdfs中有许多的16进制命名文件夹,文件夹中套文件夹;当我们进行读取文件的时候,我们要通过获取mysql里面的相应文件的存储地址去nginx服务器中通过url路径文件名,从fastdfs文件中取出我们的文件进行展示。(所以下载图,有点问题,在这里解释一下)


三、实现流程:

1、配置依赖:因为我们使用的maven来管理工程,所以,我们需要去陪pom文件

[html] view plain copy
print?
  1. <project xmlns=“http://maven.apache.org/POM/4.0.0” xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”    
  2.     xsi:schemaLocation=“http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”>    
  3.     <modelVersion>4.0.0</modelVersion>     
  4.     <groupId>com.leech</groupId>    
  5.     <artifactId>fastdfs-demo</artifactId>    
  6.     <version>0.0.1-SNAPSHOT</version>    
  7.     <packaging>jar</packaging>     
  8.     <name>fastdfs-demo</name>    
  9.     <url>http://maven.apache.org</url>     
  10.     <properties>      
  11.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>    
  12.     </properties>     
  13.       
  14.     <dependencies>      
  15.         <dependency>        
  16.             <groupId>junit</groupId>        
  17.             <artifactId>junit</artifactId>        
  18.             <version>4.11</version>        
  19.             <scope>test</scope>      
  20.         </dependency>           
  21.         <dependency>        
  22.             <groupId>org.csource</groupId>        
  23.             <artifactId>fastdfs-client-java</artifactId>        
  24.             <version>1.25</version>      
  25.         </dependency>           
  26.         <dependency>          
  27.             <groupId>commons-io</groupId>          
  28.             <artifactId>commons-io</artifactId>          
  29.             <version>2.4</version>      
  30.             </dependency>         
  31.         </dependencies>  
  32.     </project>  
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">      <modelVersion>4.0.0</modelVersion>       <groupId>com.leech</groupId>      <artifactId>fastdfs-demo</artifactId>      <version>0.0.1-SNAPSHOT</version>      <packaging>jar</packaging>       <name>fastdfs-demo</name>      <url>http://maven.apache.org</url>       <properties>            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>      </properties>       <dependencies>            <dependency>                  <groupId>junit</groupId>                  <artifactId>junit</artifactId>                  <version>4.11</version>                  <scope>test</scope>            </dependency>                 <dependency>                  <groupId>org.csource</groupId>                  <artifactId>fastdfs-client-java</artifactId>                  <version>1.25</version>            </dependency>                 <dependency>                    <groupId>commons-io</groupId>                    <artifactId>commons-io</artifactId>                    <version>2.4</version>                </dependency>               </dependencies>    </project>
当然,更多时候我们利用maven的继承特性,直接配置在父工程中,然后继承下来就o了。

2、新建fdfs_client.conf文件,在我们的itoo中,我们建立在(src/main/resources底下)

[html] view plain copy
print?
  1. connect_timeout = 2  
  2. network_timeout = 30  
  3. charset = UTF-8  
  4. http.tracker_http_port = 80   
  5. #没什么用  
  6. http.anti_steal_token = no  
  7. http.secret_key = FastDFS1234567890  
  8. tracker_server = 192.168.17.112:22122   
  9. #tracker_server = 192.168.0.119:22122  
connect_timeout = 2network_timeout = 30charset = UTF-8http.tracker_http_port = 80 
#没什么用http.anti_steal_token = nohttp.secret_key = FastDFS1234567890tracker_server = 192.168.17.112:22122 #tracker_server = 192.168.0.119:22122


3、实现文件上传:

[java] view plain copy
print?
  1.       public void save(HttpServletRequest request,HttpServletResponse response){          
  2.     String videoName=request.getParameter(”videoName”);  
  3.     String videoType=request.getParameter(”videoType”);  
  4.     String videoDesc=request.getParameter(”videoDesc”);  
  5.     String videoPath=request.getParameter(”videoPath”);  
  6.     String picturePath=request.getParameter(”picturePath”);  
  7.   
  8.     SimpleDateFormat formatter = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);  
  9.     Date uploadTime=null;  
  10.     try {  
  11.         uploadTime = formatter.parse(formatter.format(new Date()));  
  12.     } catch (ParseException e) {  
  13.         // TODO Auto-generated catch block  
  14.         e.printStackTrace();  
  15.     }  
  16.       
  17.     JacksonJsonUntil jackJsonUtil =new JacksonJsonUntil ();  
  18.       
  19.     String dataBaseName = ”itoo_video”;  
  20.   
  21.     Video video=new Video();  
  22.     video.setUserName(”why”);  
  23.     video.setUserID(”why”);       
  24.     video.setVideoName(videoName);  
  25.     video.setVideoPath(videoPath);  
  26.     video.setVideoType(videoType);  
  27.     video.setVideoDesc(videoDesc);    
  28.     video.setDataBaseName(dataBaseName);  
  29.     video.setPicturePath(picturePath);  
  30.     video.setUploadTime(uploadTime);  
  31.       
  32.     uploadFileService.save(video);        
  33.       
  34.        jackJsonUtil.beanToJson(response,video);  
  35.       
  36. }  
  37.   
  38. /* 
  39.  * 上传文件 
  40.  */  
  41. @RequestMapping(value={“/upload”})  
  42. @ResponseBody  
  43. public void upload( MultipartFile file, HttpServletRequest request,HttpServletResponse response){         
  44.       
  45.     String ext_Name = file.getOriginalFilename().split(”\\.”)[1];  
  46.     String videoName=file.getOriginalFilename().split(”\\.”)[0];  
  47.       
  48.        byte[] bytes = null;  
  49.     try {  
  50.         bytes = file.getBytes();  
  51.     } catch (IOException e) {  
  52.         e.printStackTrace();  
  53.     }  
  54.     String videoPath=uploadFile(bytes,ext_Name);  
  55.       
  56.     JacksonJsonUntil jackJsonUtil =new JacksonJsonUntil ();  
  57.       
  58.     Video video=new Video();  
  59.     video.setVideoPath(videoPath);  
  60.     video.setVideoName(videoName);        
  61.       
  62.     jackJsonUtil.beanToJson(response,video);  
  63. }   
  64.   
  65. public String uploadFile(byte[] byteFile, String ext_file) {  
  66.     // 拼接服务区的文件路径  
  67.     StringBuffer sbPath = new StringBuffer();  
  68.     sbPath.append(”http://192.168.22.252”);  
  69.     try {  
  70.         // 初始化文件资源  
  71.         ClientGlobal  
  72.                 .init(”C:\\Users\\alsr\\Desktop\\ITOO-5.0\\itoo-video-Test\\dmsd-itoo-video-parent\\dmsd-itoo-video-web\\src\\main\\resources\\fdfs_client.conf”);  
  73.   
  74.         // 链接FastDFS服务器,创建tracker和Stroage  
  75.         TrackerClient trackerClient = new TrackerClient();  
  76.         TrackerServer trackerServer = trackerClient.getConnection();  
  77.         StorageServer storageServer = null;  
  78.         StorageClient storageClient = new StorageClient(trackerServer,  
  79.                 storageServer);  
  80.         //利用字节流上传文件  
  81.         String[] strings = storageClient.upload_file(byteFile, ext_file, null);  
  82.   
  83.         for (String string : strings) {  
  84.             sbPath.append(”/” + string);  
  85.             System.out.println(string);  
  86.         }  
  87.         // 全路径  
  88.         System.out.println(sbPath);  
  89.     } catch (IOException | MyException e) {  
  90.         e.printStackTrace();  
  91.     }  
  92.     return sbPath.toString();  
  93. }  
       public void save(HttpServletRequest request,HttpServletResponse response){                String videoName=request.getParameter("videoName");        String videoType=request.getParameter("videoType");        String videoDesc=request.getParameter("videoDesc");        String videoPath=request.getParameter("videoPath");        String picturePath=request.getParameter("picturePath");        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        Date uploadTime=null;        try {            uploadTime = formatter.parse(formatter.format(new Date()));        } catch (ParseException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        JacksonJsonUntil jackJsonUtil =new JacksonJsonUntil ();        String dataBaseName = "itoo_video";        Video video=new Video();        video.setUserName("why");        video.setUserID("why");             video.setVideoName(videoName);        video.setVideoPath(videoPath);        video.setVideoType(videoType);        video.setVideoDesc(videoDesc);          video.setDataBaseName(dataBaseName);        video.setPicturePath(picturePath);        video.setUploadTime(uploadTime);        uploadFileService.save(video);              jackJsonUtil.beanToJson(response,video);    }    /*     * 上传文件     */    @RequestMapping(value={"/upload"})    @ResponseBody    public void upload( MultipartFile file, HttpServletRequest request,HttpServletResponse response){               String ext_Name = file.getOriginalFilename().split("\\.")[1];        String videoName=file.getOriginalFilename().split("\\.")[0];        byte[] bytes = null;        try {            bytes = file.getBytes();        } catch (IOException e) {            e.printStackTrace();        }        String videoPath=uploadFile(bytes,ext_Name);        JacksonJsonUntil jackJsonUtil =new JacksonJsonUntil ();        Video video=new Video();        video.setVideoPath(videoPath);        video.setVideoName(videoName);              jackJsonUtil.beanToJson(response,video);    }     public String uploadFile(byte[] byteFile, String ext_file) {        // 拼接服务区的文件路径        StringBuffer sbPath = new StringBuffer();        sbPath.append("http://192.168.22.252");        try {            // 初始化文件资源            ClientGlobal                    .init("C:\\Users\\alsr\\Desktop\\ITOO-5.0\\itoo-video-Test\\dmsd-itoo-video-parent\\dmsd-itoo-video-web\\src\\main\\resources\\fdfs_client.conf");            // 链接FastDFS服务器,创建tracker和Stroage            TrackerClient trackerClient = new TrackerClient();            TrackerServer trackerServer = trackerClient.getConnection();            StorageServer storageServer = null;            StorageClient storageClient = new StorageClient(trackerServer,                    storageServer);            //利用字节流上传文件            String[] strings = storageClient.upload_file(byteFile, ext_file, null);            for (String string : strings) {                sbPath.append("/" + string);                System.out.println(string);            }            // 全路径            System.out.println(sbPath);        } catch (IOException | MyException e) {            e.printStackTrace();        }        return sbPath.toString();    }
       只要我们能正常接收到一个json类型的字符串(url地址),就证明我们已经上传成功了,如果不信,可以直接用浏览器去验证一下,看看能不能得到文件展示。


4、FastDFS实现文件下载:

[java] view plain copy
print?
  1. public void testDownload() {          
  2.         try {               
  3.             ClientGlobal.init(conf_filename);               
  4.             TrackerClient tracker = new TrackerClient();               
  5.             TrackerServer trackerServer = tracker.getConnection();              
  6.             StorageServer storageServer = null;               
  7.             StorageClient storageClient = new StorageClient(trackerServer, storageServer);               
  8.             byte[] b = storageClient.download_file(“group1”“M00/00/00/wKgRcFV_08OAK_KCAAAA5fm_sy874.conf”);               
  9.             System.out.println(b);               
  10.             IOUtils.write(b, new FileOutputStream(“D:/”+UUID.randomUUID().toString()+“.conf”));          
  11.             }   
  12.         catch (Exception e) {               
  13.             e.printStackTrace();           
  14.         }       
  15.     }         
public void testDownload() {                try {                         ClientGlobal.init(conf_filename);                         TrackerClient tracker = new TrackerClient();                         TrackerServer trackerServer = tracker.getConnection();                        StorageServer storageServer = null;                         StorageClient storageClient = new StorageClient(trackerServer, storageServer);                         byte[] b = storageClient.download_file("group1", "M00/00/00/wKgRcFV_08OAK_KCAAAA5fm_sy874.conf");                         System.out.println(b);                         IOUtils.write(b, new FileOutputStream("D:/"+UUID.randomUUID().toString()+".conf"));                    }         catch (Exception e) {                         e.printStackTrace();                 }         }       


5、FastDFS获取将上传文件信息:

[java] view plain copy
print?
  1. public void testGetFileInfo(){           
  2.         try {               
  3.             ClientGlobal.init(conf_filename);               
  4.             TrackerClient tracker = new TrackerClient();               
  5.             TrackerServer trackerServer = tracker.getConnection();               
  6.             StorageServer storageServer = null;               
  7.             StorageClient storageClient = new StorageClient(trackerServer, storageServer);               
  8.             FileInfo fi = storageClient.get_file_info(”group1”“M00/00/00/wKgRcFV_08OAK_KCAAAA5fm_sy874.conf”);               
  9.             System.out.println(fi.getSourceIpAddr());             System.out.println(fi.getFileSize());               
  10.             System.out.println(fi.getCreateTimestamp());             System.out.println(fi.getCrc32());           
  11.             }     
  12.         catch (Exception e) {               
  13.             e.printStackTrace();           
  14.             }       
  15.         }         
public void testGetFileInfo(){                 try {                         ClientGlobal.init(conf_filename);                         TrackerClient tracker = new TrackerClient();                         TrackerServer trackerServer = tracker.getConnection();                         StorageServer storageServer = null;                         StorageClient storageClient = new StorageClient(trackerServer, storageServer);                         FileInfo fi = storageClient.get_file_info("group1", "M00/00/00/wKgRcFV_08OAK_KCAAAA5fm_sy874.conf");                         System.out.println(fi.getSourceIpAddr());             System.out.println(fi.getFileSize());                         System.out.println(fi.getCreateTimestamp());             System.out.println(fi.getCrc32());                     }           catch (Exception e) {                         e.printStackTrace();                     }             }       


6、FastDFS获取文件名称:

[java] view plain copy
print?
  1. public void testGetFileMate(){           
  2.         try {              
  3.             ClientGlobal.init(conf_filename);               
  4.             TrackerClient tracker = new TrackerClient();               
  5.             TrackerServer trackerServer = tracker.getConnection();               
  6.             StorageServer storageServer = null;               
  7.             StorageClient storageClient = new StorageClient(trackerServer,                       
  8.             storageServer);               
  9.             NameValuePair nvps [] = storageClient.get_metadata(”group1”“M00/00/00/wKgRcFV_08OAK_KCAAAA5fm_sy874.conf”);               
  10.             for(NameValuePair nvp : nvps){                   
  11.                 System.out.println(nvp.getName() + ”:” + nvp.getValue());              
  12.                 }           
  13.             }   
  14.         catch (Exception e) {               
  15.             e.printStackTrace();           
  16.             }       
  17.     }      
    public void testGetFileMate(){                     try {                            ClientGlobal.init(conf_filename);                             TrackerClient tracker = new TrackerClient();                             TrackerServer trackerServer = tracker.getConnection();                             StorageServer storageServer = null;                             StorageClient storageClient = new StorageClient(trackerServer,                                     storageServer);                             NameValuePair nvps [] = storageClient.get_metadata("group1", "M00/00/00/wKgRcFV_08OAK_KCAAAA5fm_sy874.conf");                             for(NameValuePair nvp : nvps){                                     System.out.println(nvp.getName() + ":" + nvp.getValue());                                }                         }             catch (Exception e) {                             e.printStackTrace();                         }             }    

7、FastDFS实现删除文件:

[java] view plain copy
print?
  1.           public void testDelete(){           
  2. try {               
  3.     ClientGlobal.init(conf_filename);               
  4.     TrackerClient tracker = new TrackerClient();               
  5.     TrackerServer trackerServer = tracker.getConnection();               
  6.     StorageServer storageServer = null;               
  7.     StorageClient storageClient = new StorageClient(trackerServer,storageServer);               
  8.     int i = storageClient.delete_file(“group1”“M00/00/00/wKgRcFV_08OAK_KCAAAA5fm_sy874.conf”);               
  9.     System.out.println( i==0 ? “删除成功” : “删除失败:”+i);           
  10.     }   
  11. catch (Exception e) {               
  12.     e.printStackTrace();           
  13.     }       
  14.           }  
             public void testDelete(){                     try {                             ClientGlobal.init(conf_filename);                             TrackerClient tracker = new TrackerClient();                             TrackerServer trackerServer = tracker.getConnection();                             StorageServer storageServer = null;                             StorageClient storageClient = new StorageClient(trackerServer,storageServer);                             int i = storageClient.delete_file("group1", "M00/00/00/wKgRcFV_08OAK_KCAAAA5fm_sy874.conf");                             System.out.println( i==0 ? "删除成功" : "删除失败:"+i);                         }             catch (Exception e) {                             e.printStackTrace();                         }                  }




原创粉丝点击