pom.xml 导入 FastDFS依赖,如下:
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.4</version>
</dependency>
application.yml 配置FastDFS连接信息
fdfs:
tracker-list: 192.168.1.11:22122
endpoint: http://192.168.1.11:8889
FastDFS操作类
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.io.InputStream;
/**
* fdfs 操作类
*
* @author terry
* @version 1.0
* @date 2022/8/22 21:18
*/
@Service
@Slf4j
public class FastDFSService {
@Value("${fdfs.endpoint}")
private String endpoint;
@Autowired
private FastFileStorageClient storageClient;
public void upload(InputStream inputStream, String name) {
try {
StorePath storePath = storageClient.uploadFile(inputStream, (long)inputStream.available(), name, null);
// 获得文件上传后访问地址
String url = storePath.getFullPath();
log.info(" 相对路径: " + url);
log.info(" 完整路径: " + endpoint + "/" + url);
} catch (IOException e) {
e.printStackTrace();
}
}
}
测试 FastDFS
import com.terry.service.FastDFSService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
/**
* 测试 FastDFS
*
* @author terry
* @version 1.0
* @date 2022/8/16 22:19
*/
@SpringBootTest
public class MinioTest {
@Autowired
private FastDFSService fastDFSService;
@Test
public void test() throws FileNotFoundException {
fastDFSService.upload(new FileInputStream("D:\\test.txt"), "test.txt");
}
}