1. 新建SpringBoot项目
2. 项目依赖
< dependencies>
< dependency>
< groupId> com.github.tobato</ groupId>
< artifactId> fastdfs-client</ artifactId>
< version> 1.26.4</ version>
</ dependency>
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-test</ artifactId>
</ dependency>
</ dependencies>
3. 配置文件
fdfs :
so-timeout : 3000
connect-timeout : 1000
thumb-image :
width : 60
height : 60
tracker-list :
- 121.199.58.42: 22122
4. 文件服务配置类
package zw. springboot. config ;
import com. github. tobato. fastdfs. FdfsClientConfig ;
import org. springframework. context. annotation. Configuration ;
import org. springframework. context. annotation. EnableMBeanExport ;
import org. springframework. context. annotation. Import ;
import org. springframework. jmx. support. RegistrationPolicy ;
@Configuration
@Import ( FdfsClientConfig . class )
@EnableMBeanExport ( registration = RegistrationPolicy . IGNORE_EXISTING )
public class FastDFSClientConfig
{
}
5. 文件上传测试
package zw. springboot ;
import com. github. tobato. fastdfs. domain. StorePath ;
import com. github. tobato. fastdfs. service. FastFileStorageClient ;
import org. apache. commons. io. FilenameUtils ;
import org. junit. jupiter. api. Test ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. boot. test. context. SpringBootTest ;
import java. io. * ;
@SpringBootTest
class SpringbootApplicationTest
{
@Autowired
private FastFileStorageClient storageClient;
@Test
public void uploadTest ( )
{
InputStream is = null ;
try
{
File source = new File ( "D:\\util\\fstDFS.jpg" ) ;
is = new FileInputStream ( source) ;
StorePath storePath = storageClient. uploadFile ( is, source. length ( ) , FilenameUtils . getExtension ( source. getName ( ) ) , null ) ;
String fullPath = storePath. getFullPath ( ) ;
System . out. println ( "fullPath = " + fullPath) ;
}
catch ( FileNotFoundException e)
{
e. printStackTrace ( ) ;
}
finally
{
try
{
if ( is != null )
{
is. close ( ) ;
}
}
catch ( IOException e)
{
e. printStackTrace ( ) ;
}
}
}
}
6. 文件上传运行
控制台打印文件访问路径:fullPath = group1/M00/00/00/eccnMl9PT9uAUK9VAAB1IQneShA844.jpg 浏览器输入地址访问:ip + port + fullPath
7. 文件下载测试
package zw. springboot ;
import com. github. tobato. fastdfs. domain. StorePath ;
import com. github. tobato. fastdfs. proto. storage. DownloadByteArray ;
import com. github. tobato. fastdfs. service. FastFileStorageClient ;
import org. apache. commons. io. FilenameUtils ;
import org. junit. jupiter. api. Test ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. boot. test. context. SpringBootTest ;
import java. io. * ;
@SpringBootTest
class SpringbootApplicationTest
{
@Autowired
private FastFileStorageClient storageClient;
@Test
public void downloadTest ( )
{
String fullPath = "group1/M00/00/00/eccnMl9PT9uAUK9VAAB1IQneShA844.jpg" ;
String group = fullPath. substring ( 0 , fullPath. indexOf ( "/" ) ) ;
String path = fullPath. substring ( fullPath. indexOf ( "/" ) + 1 ) ;
byte [ ] buffer = storageClient. downloadFile ( group, path, new DownloadByteArray ( ) ) ;
File target = new File ( "D://util" , "target" + fullPath. substring ( fullPath. indexOf ( "." ) ) ) ;
OutputStream os = null ;
try
{
os = new FileOutputStream ( target) ;
os. write ( buffer) ;
}
catch ( FileNotFoundException e)
{
e. printStackTrace ( ) ;
}
catch ( IOException e)
{
e. printStackTrace ( ) ;
}
finally
{
try
{
if ( os != null )
{
os. close ( ) ;
}
}
catch ( IOException e)
{
e. printStackTrace ( ) ;
}
}
}
}
8. 文件下载运行
9. 文件删除测试
package zw. springboot ;
import com. github. tobato. fastdfs. domain. StorePath ;
import com. github. tobato. fastdfs. proto. storage. DownloadByteArray ;
import com. github. tobato. fastdfs. service. FastFileStorageClient ;
import org. apache. commons. io. FilenameUtils ;
import org. junit. jupiter. api. Test ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. boot. test. context. SpringBootTest ;
import java. io. * ;
@SpringBootTest
class SpringbootApplicationTest
{
@Autowired
private FastFileStorageClient storageClient;
@Test
public void deleteTest ( )
{
String fullPath = "group1/M00/00/00/eccnMl9PT9uAUK9VAAB1IQneShA844.jpg" ;
String group = fullPath. substring ( 0 , fullPath. indexOf ( "/" ) ) ;
String path = fullPath. substring ( fullPath. indexOf ( "/" ) + 1 ) ;
storageClient. deleteFile ( group, path) ;
}
}
10. 文件删除运行
浏览器输入地址访问:ip + port + fullPath