1.导包
<!--fastdfs-->
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.5</version>
</dependency>
2.配置
#socket连接超时时长
fdfs.soTimeout=1500
#连接tracker服务器超时时长
fdfs.connectTimeout=600
fdfs.trackerList=111.111.111.111:22
fileServerUrl=http://111.111.111.111:8080/
3.启动类
@SpringBootApplication
public class FastDFSApplication {
public static void main(String[] args) {
SpringApplication.run(FastDFSApplication.class,args);
}
}
4.工具类
@Component
public class FastDFSClientUtil {
@Autowired
private FastFileStorageClient storageClient;
/**
* 上传
* @param file
* @return
* @throws IOException
*/
public String uploadFile(MultipartFile file) throws IOException {
StorePath storePath = storageClient.uploadFile((InputStream) file.getInputStream(), file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()), null);
return storePath.getFullPath();
}
/**
* 删除
* @param filePath
*/
public void delFile(String filePath) {
storageClient.deleteFile(filePath);
}
/**
* 下载
* @param groupName
* @param path
* @return
*/
public byte[] download(String groupName, String path) throws IOException {
InputStream ins = storageClient.downloadFile(groupName, path, new DownloadCallback<InputStream>() {
@Override
public InputStream recv(InputStream ins) throws IOException {
// 将此ins返回给上面的ins
return ins;
}
});
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] buff = new byte[100];
int rc = 0;
while ((rc = ins.read(buff, 0, 100)) > 0) {
byteArrayOutputStream.write(buff, 0, rc);
}
return byteArrayOutputStream.toByteArray();
}
}
5.使用
@Value("${fileServerUrl}")
String url;
@PostMapping("/up")
public String file_up(MultipartFile file) throws IOException {
String s = fastDFSClientUtil.uploadFile(file);
return url+s;
}
版权声明:本文为ty4660015原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。