SpringBoot.集成FastDFS

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

闲猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值