fastDFS 使用fastDFS实现文件上传和下载

清疚 2022-10-02 06:42 90阅读 0赞

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

1. 搭建环境

这里我们使用javaApi测试文件的上传,java版本的fastdfs-client地址在: https://github.com/happyfish100/fastdfs-client-java 参考此工程编写测试用例。

1)创建maven工程

pom.xml

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-web</artifactId>
  5. </dependency>
  6. <!-- https://mvnrepository.com/artifact/net.oschina.zcx7878/fastdfs-client-java -->
  7. <dependency>
  8. <groupId>net.oschina.zcx7878</groupId>
  9. <artifactId>fastdfs-client-java</artifactId>
  10. <version>1.27.0.0</version>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.springframework.boot</groupId>
  14. <artifactId>spring-boot-starter-test</artifactId>
  15. <scope>test</scope>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.apache.commons</groupId>
  19. <artifactId>commons-io</artifactId>
  20. <version>1.3.2</version>
  21. </dependency>
  22. </dependencies>

2) 配置 文件

在classpath:config下创建fastdfs-client.properties文件

  1. fastdfs.connect_timeout_in_seconds = 5
  2. fastdfs.network_timeout_in_seconds = 30
  3. fastdfs.charset = UTF-8
  4. fastdfs.tracker_servers = 192.168.101.65:22122

2. 文件上传

  1. //上传文件
  2. @Test
  3. public void testUpload() {
  4. try {
  5. ClientGlobal.initByProperties("config/fastdfs‐client.properties");
  6. System.out.println("network_timeout=" + ClientGlobal.g_network_timeout + "ms");
  7. System.out.println("charset=" + ClientGlobal.g_charset);
  8. //创建客户端
  9. TrackerClient tc = new TrackerClient();
  10. //连接tracker Server
  11. TrackerServer ts = tc.getConnection();
  12. if (ts == null) {
  13. System.out.println("getConnection return null");
  14. return;
  15. }
  16. //获取一个storage server
  17. StorageServer ss = tc.getStoreStorage(ts);
  18. if (ss == null) {
  19. System.out.println("getStoreStorage return null");
  20. }
  21. //创建一个storage存储客户端
  22. StorageClient1 sc1 = new StorageClient1(ts, ss);
  23. NameValuePair[] meta_list = null; //new NameValuePair[0];
  24. String item = "C:\\Users\\admin\\Desktop\\1.png";
  25. String fileid;
  26. fileid = sc1.upload_file1(item, "png", meta_list);
  27. System.out.println("Upload local file " + item + " ok, fileid=" + fileid);
  28. } catch (Exception ex) {
  29. ex.printStackTrace();
  30. }
  31. }

3. 文件查询

  1. //查询文件
  2. @Test
  3. public void testQueryFile() throws IOException, MyException {
  4. ClientGlobal.initByProperties("config/fastdfs‐client.properties");
  5. TrackerClient tracker = new TrackerClient();
  6. TrackerServer trackerServer = tracker.getConnection();
  7. StorageServer storageServer = null;
  8. StorageClient storageClient = new StorageClient(trackerServer,storageServer);
  9. FileInfo fileInfo = storageClient.query_file_info("group1",
  10. "M00/00/01/wKhlQFrKBSOAW5AWAALcAg10vf4862.png");
  11. System.out.println(fileInfo);
  12. }

4. 文件下载

  1. //下载文件
  2. @Test
  3. public void testDownloadFile() throws IOException, MyException {
  4. ClientGlobal.initByProperties("config/fastdfs‐client.properties");
  5. TrackerClient tracker = new TrackerClient();
  6. TrackerServer trackerServer = tracker.getConnection();
  7. StorageServer storageServer = null;
  8. StorageClient1 storageClient1 = new StorageClient1(trackerServer,storageServer);
  9. byte[] result =storageClient1.download_file1("group1/M00/00/01/wKhlQFrKBSOAW5AWAALcAg10vf4862.png");
  10. File file = new File("d:/1.png");
  11. FileOutputStream fileOutputStream = new FileOutputStream(file);
  12. fileOutputStream.write(result);
  13. fileOutputStream.close();
  14. }

转载于:https://my.oschina.net/edisonOnCall/blog/3039662

发表评论

表情:
评论列表 (有 0 条评论,90人围观)

还没有评论,来说两句吧...

相关阅读