0
点赞
收藏
分享

微信扫一扫

SpringBoot.集成FastDFS

我是芄兰 2022-04-13 阅读 63

POM

<!--fstdfs client-->
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.5</version>
</dependency>

配置

fdfs:
so-timeout: 1501
connect-timeout: 601
thumb-image: #缩略图生成参数
width: 200
height: 200
tracker-list: #TrackerList参数,支持多个
- 192.168.1.18:22122

引入配置FastDFSClientConfig


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)
// 解决jmx重复注册bean的问题
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class FastDFSClientConfig {

}

FastDFSClientUtil

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.upload.FastFile;
import com.github.tobato.fastdfs.exception.FdfsUnsupportStorePathException;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.glodon.qis.common.base.util.QisStringUtils;

import lombok.extern.slf4j.Slf4j;

/**
* Fastdfs工具类
*/

@Component
@Slf4j
public class FastdfsClientUtil {

@Autowired
private FastFileStorageClient storageClient;

/**
* 上传文件
* @param myfile
* @return
* @throws Exception
*/

public String upload(MultipartFile myfile) throws Exception{
//文件名
String originalFilename = myfile.getOriginalFilename().substring(myfile.getOriginalFilename().lastIndexOf(".") + 1);
// 文件扩展名
//String ext = originalFilename.substring(originalFilename.lastIndexOf(".") + 1, originalFilename.length());

StorePath storePath = this.storageClient.uploadImageAndCrtThumbImage(myfile.getInputStream(), myfile.getSize(),originalFilename , null);

String path = storePath.getFullPath();

return path;
}

public String upload(File file) throws Exception{

String fileName = file.getName();

InputStream inputStream = new FileInputStream(file);

String fileExtName = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());

FastFile fastFile = new FastFile.Builder().withFile(inputStream, file.length(), fileExtName).build();
StorePath storePath = this.storageClient.uploadFile(fastFile );

String path = storePath.getFullPath();

return path;

}

/**
* 删除文件
* @Param fileUrl 文件访问地址
*/

public void deleteFile(String fileUrl) {
if (QisStringUtils.isEmpty(fileUrl)) {
return;
}
try {
StorePath storePath = StorePath.parseFromUrl(fileUrl);
storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
} catch (FdfsUnsupportStorePathException e) {
log.warn(e.getMessage());
}
}
}


访问

http://192.168.18.12:8888/group1/M00/00/00/wKh2ol2yvEiAJRiAAAW1fdYHlgk608.jpg

返回:group1/M00/00/00/wKh2ol2yvEiAJRiAAAW1fdYHlgk608.jpg

	@Autowired
private FastdfsClientUtil fastdfsClientUtil;

@GetMapping("/fdfs1")
@ApiOperation(value="测试接口4", notes="测试FastDFS的使用")
@ApiResponse(response = String.class,message = "结果字符串", code = 200)
public String fdfs1() throws Exception{
File file = new File("C:\\Users\\glodon\\Desktop\\12.jpg");
return fastdfsClientUtil.upload(file);
}

end

举报

相关推荐

0 条评论