JAVA全系列 教程
3762个小节阅读:7092.8k
目录
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
在通用模块添加文件服务接口
xxxxxxxxxx
// 文件服务
public interface FileService {
/**
* 上传文件
* @param fileBytes 图片文件转成的字节数组
* @param fileName 文件名
* @return 上传后的文件访问路径
*/
String uploadImage(byte[] fileBytes,String fileName) throws IOException;
/**
* 删除文件
* @param filePath 文件路径
*/
void delete(String filePath);
}
在文件服务模块创建文件服务接口实现类
xxxxxxxxxx
@DubboService
public class FileServiceImpl implements FileService {
@Autowired
private FastFileStorageClient fastFileStorageClient;
@Value("${fdfs.fileUrl}")
private String fileUrl; // Nginx访问FastDFS中文件的路径
@Override
public String uploadImage(byte[] fileBytes, String fileName) {
if (fileBytes.length != 0) {
try {
// 1.将文件字节数组转为输入流
InputStream inputStream = new ByteArrayInputStream(fileBytes);
// 2.获取文件的后缀名
String fileSuffix = fileName.substring(fileName.lastIndexOf(".") + 1);
// 3.上传文件
StorePath storePath = fastFileStorageClient.uploadFile(inputStream, inputStream.available(), fileSuffix, null);
// 4.返回图片路径
String imageUrl = fileUrl + "/"+storePath.getFullPath();
return imageUrl;
} catch (IOException ioException) {
throw new BusException(CodeEnum.UPLOAD_FILE_ERROR);
}
} else {
throw new BusException(CodeEnum.UPLOAD_FILE_ERROR);
}
}
@Override
public void delete(String filePath) {
fastFileStorageClient.deleteFile(filePath);
}
}