服务器fastdfs java 文件上传测试

来源:互联网 发布:查看阿里云ecs带宽 编辑:程序博客网 时间:2024/06/05 21:49

github 地址:https://github.com/tobato/FastDFS_Client


测试目录结构图:





pom.xml 代码:

<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.phpfzh2</groupId>
<artifactId>spring-boot01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.0.RELEASE</version>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.20</version>
</dependency>

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>

<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.25.4-RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

application.yml代码:

fdfs:
  soTimeout: 1501
  connectTimeout: 601 
  thumbImage:             #缩略图生成参数
    width: 150
    height: 150
  trackerList:            #TrackerList参数,支持多个
    - 192.168.133.128:22122
    - 192.168.133.128:22122


启动类代码:

package com.phpfzh;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}



将Fdfs配置引入项目代码:

对的,只需要一行注解 @Import(FdfsClientConfig.class)就可以拥有带有连接池的FastDFS Java客户端了。

注意:@EnableMBeanExport解决问题JMX重复注册问题,不要再配置 spring.jmx.enabled=false,以免影响SpringBoot默认的JMX监控。



package com.phpfzh.conf;


import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.context.annotation.Import;
import org.springframework.jmx.support.RegistrationPolicy;


import com.github.tobato.fastdfs.FdfsClientConfig;


@Configuration
@Import(FdfsClientConfig.class)
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class ComponetImport {


}



测试类代码:

package com.phpfzh;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;


import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


import com.github.tobato.fastdfs.domain.FileInfo;
import com.github.tobato.fastdfs.domain.GroupState;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.DefaultGenerateStorageClient;
import com.github.tobato.fastdfs.service.DefaultTrackerClient;
 
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)// 1.4.0 前版本 
public class FastdfsTest {

public static Log logger = LogFactory.getLog(FastdfsTest.class);

@Autowired
DefaultGenerateStorageClient defaultGenerateStorageClient; 

@Autowired
DefaultTrackerClient defaultTrackerClient;

@Test
public void Test(){
  File file = new File("D:\\123456.png");
FileInputStream fileInputStream = null;
try {
fileInputStream = FileUtils.openInputStream(file);
} catch (IOException e) {
  e.printStackTrace();
}

StorePath storePath = defaultGenerateStorageClient.uploadFile("group1", fileInputStream, file.length(), "png"); //上传文件
  logger.debug(storePath);
System.out.println(storePath);
//StorePath [group=group1, path=M00/00/00/wKiFgFm9CRmAUTcTAAAkvvmVy_E865.png]
defaultGenerateStorageClient.deleteFile("group1", "M00/00/00/wKiFgFm9CP6AU2eBAAAkvvmVy_E526.png");//删除文件

  }

@Test
public void Test01(){
FileInfo fiele = defaultGenerateStorageClient.queryFileInfo("group1", "M00/00/00/wKiFgFm9CRmAUTcTAAAkvvmVy_E865.png");//根据组名和路径查找信息
System.out.println(fiele);//source_ip_addr = 192.168.133.128, file_size = 9406, create_timestamp = 1970-01-18 18:12:40, crc32 = -107623439
List<GroupState> list = defaultTrackerClient.listGroups();//获取组名
System.out.println(list);
/**[GroupState [groupName=group1, totalMB=18421, freeMB=14731, trunkFreeMB=0, storageCount=1, 
storagePort=23000, storageHttpPort=8888, activeCount=1, currentWriteServer=0, storePathCount=1, 
subdirCountPerPath=256, currentTrunkFileId=0]]
**/


}
}







浏览器测试:





主要接口:

  1. TrackerClient - TrackerServer接口
  2. GenerateStorageClient - 一般文件存储接口 (StorageServer接口)
  3. FastFileStorageClient - 为方便项目开发集成的简单接口(StorageServer接口)
  4. AppendFileStorageClient - 支持文件续传操作的接口 (StorageServer接口)

阅读全文
0 0
原创粉丝点击