使用java操作FastDFS上传,下载,删除文件

来源:互联网 发布:知乎 尼泊尔 徒步 编辑:程序博客网 时间:2024/05/16 10:40
前提条件:将源码生成为jar包,install到本地maven仓库

1.从github上down下源代码:https://github.com/happyfish100/fastdfs-client-java在myeclipse中创建maven项目,然后把解压的内容复制到maven项目中


2.你也可以直接下载我整的maven工程,解压导入myeclipse中直接使用,下载地址:http://download.csdn.net/detail/qq_34021712/9812335

执行maven install 将代码打成jar到本地maven仓库




在maven中依赖jar包

[java] view plain copy
  1. <!-- fastdfs上传下载图片  路径和上面的pom中对应 -->  
  2. <dependency>  
  3.     <groupId>org.csource.fastdfs-client-java</groupId>  
  4.     <artifactId>fastdfs-client-java</artifactId>  
  5.     <version>1.25</version>  
  6. </dependency>  


创建FastDFSClient类

[java] view plain copy
  1. import java.io.BufferedOutputStream;  
  2. import java.io.IOException;  
  3. import java.net.URL;  
  4. import java.net.URLDecoder;  
  5.   
  6.   
  7. import org.csource.common.MyException;  
  8. import org.csource.common.NameValuePair;  
  9. import org.csource.fastdfs.ClientGlobal;  
  10. import org.csource.fastdfs.StorageClient1;  
  11. import org.csource.fastdfs.StorageServer;  
  12. import org.csource.fastdfs.TrackerClient;  
  13. import org.csource.fastdfs.TrackerServer;  
  14.   
  15.   
  16. public class FastDFSClient {  
  17.   
  18.   
  19.     private  TrackerClient trackerClient = null;  
  20.     private  TrackerServer trackerServer = null;  
  21.     private  StorageServer storageServer = null;  
  22.     private  StorageClient1 storageClient = null;  
  23.       
  24.     public FastDFSClient(String conf) throws Exception {  
  25.         if (conf.contains("classpath:")) {  
  26.             String path = URLDecoder.decode(getClass().getProtectionDomain().getCodeSource().getLocation().toString(),"UTF-8");   
  27.             path=path.substring(6);  
  28.             conf = conf.replace("classpath:",URLDecoder.decode(path,"UTF-8"));  
  29.         }  
  30.         ClientGlobal.init(conf);  
  31.         trackerClient = new TrackerClient();  
  32.         trackerServer = trackerClient.getConnection();  
  33.         storageServer = null;  
  34.         storageClient = new StorageClient1(trackerServer, storageServer);  
  35.     }  
  36.       
  37.     /** 
  38.      * 上传文件方法 
  39.      * <p>Title: uploadFile</p> 
  40.      * <p>Description: </p> 
  41.      * @param fileName 文件全路径 
  42.      * @param extName 文件扩展名,不包含(.) 
  43.      * @param metas 文件扩展信息 
  44.      * @return 
  45.      * @throws Exception 
  46.      */  
  47.     public String uploadFile(String fileName, String extName, NameValuePair[] metas) {  
  48.         String result=null;  
  49.         try {  
  50.             result = storageClient.upload_file1(fileName, extName, metas);  
  51.         } catch (IOException e) {  
  52.             e.printStackTrace();  
  53.         } catch (MyException e) {  
  54.             e.printStackTrace();  
  55.         }  
  56.         return result;  
  57.     }  
  58.       
  59.     /** 
  60.      * 上传文件,传fileName 
  61.      * @param fileName 文件的磁盘路径名称 如:D:/image/aaa.jpg 
  62.      * @return null为失败 
  63.      */  
  64.     public String uploadFile(String fileName) {  
  65.         return uploadFile(fileName, nullnull);  
  66.     }  
  67.     /** 
  68.      *  
  69.      * @param fileName 文件的磁盘路径名称 如:D:/image/aaa.jpg 
  70.      * @param extName 文件的扩展名 如 txt jpg等 
  71.      * @return null为失败 
  72.      */  
  73.     public  String uploadFile(String fileName, String extName) {  
  74.         return uploadFile(fileName, extName, null);  
  75.     }  
  76.       
  77.     /** 
  78.      * 上传文件方法 
  79.      * <p>Title: uploadFile</p> 
  80.      * <p>Description: </p> 
  81.      * @param fileContent 文件的内容,字节数组 
  82.      * @param extName 文件扩展名 
  83.      * @param metas 文件扩展信息 
  84.      * @return 
  85.      * @throws Exception 
  86.      */  
  87.     public String uploadFile(byte[] fileContent, String extName, NameValuePair[] metas) {  
  88.         String result=null;  
  89.         try {  
  90.             result = storageClient.upload_file1(fileContent, extName, metas);  
  91.         } catch (IOException e) {  
  92.             e.printStackTrace();  
  93.         } catch (MyException e) {  
  94.             e.printStackTrace();  
  95.         }  
  96.         return result;  
  97.     }  
  98.     /** 
  99.      * 上传文件 
  100.      * @param fileContent 文件的字节数组 
  101.      * @return null为失败 
  102.      * @throws Exception 
  103.      */  
  104.     public String uploadFile(byte[] fileContent) throws Exception {  
  105.         return uploadFile(fileContent, nullnull);  
  106.     }  
  107.       
  108.     /** 
  109.      * 上传文件 
  110.      * @param fileContent  文件的字节数组 
  111.      * @param extName  文件的扩展名 如 txt  jpg png 等 
  112.      * @return null为失败 
  113.      */  
  114.     public String uploadFile(byte[] fileContent, String extName) {  
  115.         return uploadFile(fileContent, extName, null);  
  116.     }  
  117.       
  118.     /** 
  119.      * 文件下载到磁盘 
  120.      * @param path 图片路径 
  121.      * @param output 输出流 中包含要输出到磁盘的路径 
  122.      * @return -1失败,0成功 
  123.      */  
  124.     public int download_file(String path,BufferedOutputStream output) {  
  125.         //byte[] b = storageClient.download_file(group, path);  
  126.         int result=-1;  
  127.         try {  
  128.             byte[] b = storageClient.download_file1(path);  
  129.                 try{  
  130.                     if(b != null){  
  131.                         output.write(b);  
  132.                         result=0;  
  133.                     }  
  134.                 }catch (Exception e){} //用户可能取消了下载  
  135.                 finally {  
  136.                     if (output != null)  
  137.                         try {  
  138.                             output.close();  
  139.                         } catch (IOException e) {  
  140.                             e.printStackTrace();  
  141.                         }  
  142.             }  
  143.         } catch (Exception e) {  
  144.             e.printStackTrace();  
  145.         }  
  146.         return result;  
  147.     }  
  148.     /** 
  149.      * 获取文件数组 
  150.      * @param path 文件的路径 如group1/M00/00/00/wKgRsVjtwpSAXGwkAAAweEAzRjw471.jpg 
  151.      * @return 
  152.      */  
  153.     public byte[] download_bytes(String path) {  
  154.         byte[] b=null;  
  155.         try {  
  156.             b = storageClient.download_file1(path);  
  157.         } catch (IOException e) {  
  158.             e.printStackTrace();  
  159.         } catch (MyException e) {  
  160.             e.printStackTrace();  
  161.         }  
  162.         return b;  
  163.     }  
  164.       
  165.     /** 
  166.      * 删除文件 
  167.      * @param group 组名 如:group1 
  168.      * @param storagePath 不带组名的路径名称 如:M00/00/00/wKgRsVjtwpSAXGwkAAAweEAzRjw471.jpg 
  169.      * @return -1失败,0成功 
  170.      */  
  171.     public Integer delete_file(String group ,String storagePath){  
  172.         int result=-1;  
  173.         try {  
  174.             result = storageClient.delete_file(group, storagePath);  
  175.         } catch (IOException e) {  
  176.             e.printStackTrace();  
  177.         } catch (MyException e) {  
  178.             e.printStackTrace();  
  179.         }  
  180.          return  result;  
  181.     }  
  182.     /** 
  183.      *  
  184.      * @param storagePath  文件的全部路径 如:group1/M00/00/00/wKgRsVjtwpSAXGwkAAAweEAzRjw471.jpg 
  185.      * @return -1失败,0成功 
  186.      * @throws IOException 
  187.      * @throws Exception 
  188.      */  
  189.     public Integer delete_file(String storagePath){  
  190.         int result=-1;  
  191.         try {  
  192.             result = storageClient.delete_file1(storagePath);  
  193.         } catch (IOException e) {  
  194.             e.printStackTrace();  
  195.         } catch (MyException e) {  
  196.             e.printStackTrace();  
  197.         }  
  198.         return  result;  
  199.     }  
  200. }  


创建Test测试类

[java] view plain copy
  1. package com.taotao.controller;  
  2.   
  3. import java.util.List;  
  4.   
  5. import org.csource.fastdfs.ClientGlobal;  
  6. import org.csource.fastdfs.StorageClient;  
  7. import org.csource.fastdfs.StorageServer;  
  8. import org.csource.fastdfs.TrackerClient;  
  9. import org.csource.fastdfs.TrackerServer;  
  10. import org.springframework.context.ApplicationContext;  
  11. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  12.   
  13. import com.github.pagehelper.PageHelper;  
  14. import com.github.pagehelper.PageInfo;  
  15. import com.taotao.common.util.FastDFSClient;  
  16. import com.taotao.mapper.TbItemMapper;  
  17. import com.taotao.pojo.TbItem;  
  18. import com.taotao.pojo.TbItemExample;  
  19.   
  20. public class Test {  
  21.       
  22.     /*@org.junit.Test 
  23.     public void testPageHelper(){ 
  24.         ApplicationContext applicationContext=new ClassPathXmlApplicationContext("classpath:spring/applicationContext-*.xml"); 
  25.         TbItemMapper tbItemMapper = applicationContext.getBean(TbItemMapper.class); 
  26.         //执行查询,并分页 
  27.                 TbItemExample example = new TbItemExample(); 
  28.                 //分页处理 
  29.                 PageHelper.startPage(2, 10); 
  30.                 List<TbItem> list = tbItemMapper.selectByExample(example); 
  31.                 //取商品列表 
  32.                 for (TbItem tbItem : list) { 
  33.                     System.out.println(tbItem.getTitle()); 
  34.                 } 
  35.                 //取分页信息 
  36.                 PageInfo<TbItem> pageInfo = new PageInfo<>(list); 
  37.                 long total = pageInfo.getTotal(); 
  38.                 System.out.println("共有商品:"+ total); 
  39.     } 
  40.      
  41.     @org.junit.Test 
  42.     public void testUpload() throws Exception { 
  43.         // 1、把FastDFS提供的jar包添加到工程中 
  44.                 // 2、初始化全局配置。加载一个配置文件。 
  45.                 ClientGlobal.init("D:\\MyEclipse2\\taotao-manager\\taotao-manager-web\\src\\main\\resources\\properties\\fastdfs-client.conf"); 
  46.                 // 3、创建一个TrackerClient对象。 
  47.                 TrackerClient trackerClient = new TrackerClient(); 
  48.                 // 4、创建一个TrackerServer对象。 
  49.                 TrackerServer trackerServer = trackerClient.getConnection(); 
  50.                 // 5、声明一个StorageServer对象,null。 
  51.                 StorageServer storageServer = null; 
  52.                 // 6、获得StorageClient对象。 
  53.                 StorageClient storageClient = new StorageClient(trackerServer, storageServer); 
  54.                 // 7、直接调用StorageClient对象方法上传文件即可。 
  55.                 String[] strings = storageClient.upload_file("D:\\image\\aaa.jpg", "jpg", null); 
  56.                 for (String string : strings) { 
  57.                     System.out.println(string); 
  58.                 } 
  59.  
  60.     }*/  
  61.       
  62.     /** 
  63.      * 测试上传 
  64.      * @throws Exception 
  65.      */  
  66.     @org.junit.Test  
  67.     public void testFastDfsClient() throws Exception {  
  68.         FastDFSClient client = new FastDFSClient("D:\\MyEclipse2\\taotao-manager\\taotao-manager-web\\src\\main\\resources\\properties\\fastdfs-client.conf");  
  69.         String uploadFile = client.uploadFile("D:\\image\\aaa.jpg""jpg");  
  70.         System.out.println(uploadFile);  
  71.     }  
  72.     /** 
  73.      * 测试删除 
  74.      * @throws Exception 
  75.      */  
  76.     @org.junit.Test  
  77.     public void testFastDfsClientDelete() throws Exception {  
  78.         FastDFSClient client = new FastDFSClient("D:\\MyEclipse2\\taotao-manager\\taotao-manager-web\\src\\main\\resources\\properties\\fastdfs-client.conf");  
  79.         //String uploadFile = client.uploadFile("D:\\image\\aaa.jpg", "jpg");  
  80.         //Integer delete_file = client.delete_file("group1","M00/00/00/wKgRsVjtwg-ACOlSAAAweEAzRjw844.jpg");  
  81.         Integer delete_file = client.delete_file("group1/M00/00/00/wKgRsVjtyWqAdpG9AAAweEAzRjw047.jpg");  
  82.         System.out.println(delete_file);  
  83.     }  
  84.       
  85.   
  86. }  
阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 缉毒风暴 缉毒 全缉毒狂潮 缉毒警 缉毒犬 缉毒英雄 缉毒先锋 缉毒犬品种 中国缉毒警 生死缉毒 平远缉毒案 缉毒大队 缉毒读音 缉毒者 缉毒精英 缉毒人 他们是缉毒英雄 缉毒风暴免费观看 云南平远街缉毒枪战全记录 美国缉毒局1985报复墨西哥 缉毒犬史宾格 缉毒警工资一般多少 设卡盘查时缉毒犬 中华之剑缉毒纪录片 缉毒2天地对决 缉毒队长龙强的下场 缉毒王榕七针下全文在线阅读 缌麻 缌怎么读 靴下绅士缌先生 缌先生 绒缎 绵缎 缎袄缎被吧 弹力缎 素绉缎 绯缎膏 缎纹 平纹 缎面高跟鞋