fdfs_config.properties 配置
storeType=FastDfs
tracker_server=106.164.131.1167:22122 //根据自己的服务器地址配置
fdfsread_server=106.164.131.1167
uap.system.properties.mode=1
defaultBucketFull=uappr
defaultBucket=uappr
defaultBucketRead=uapread
application-dev.yml 配置
server:
fastdfs:
fileUrl: http://106.164.131.1167:22122
geteWay 配置
application-dev.yml
zuul:
routes:
fastdfs:
path: /workbench-api/group1/**
url: http://106.164.131.1167:20050/group1/
上传、下载、删除工具类
public class FastdfsUtil{
static{
//读取本地resources 中的配置文件
String filePath=Thread.currentThread().getContextClassLoader().getResoure("fdfs_config.properties").getPath().toString;
//如果配置到华为云类似的服务器上是配置文件要与jar包同级 使用流的方式读取
File file=new File("/data/config/fdfs_config.properties");
String filePath=file.getPath();
ClientGlobal.init(filePath);
}
//文件上传
public static FastdfsPath uploadFile(String groupName,byte[] bytes,String extName){
TrackerServer trackerServer=null;
try{
TrackerClient tracker=new TrackerClient();
trackerServer=tracker.getConnectioon();
StorageServer storageServer=tracker.getStoreStorage(trackerServer);
//定义storage客户端对象,需要使用这个对象完成具体的上传下载删除操作
StorageClient storageClient=new StorageClient(trackerServer,storageServer);
String fileIds[] = storageClient.upload_file(groupName,bytes,extName,null);
FastdfsPath fastdfsPath=new FastdfsPath();
fastdfsPath.setHost(trackerServer.getInetSocketAddress().getHostString());
fastdfsPath.setPort(trackerServer.getInetSocketAddress().getPort()+"");
fastdfsPath.setGroupName(fileIds[0]);
fastdfsPath.setRemoteFilename(fileIds[1]);
return fastdfsPath;
}catch(Exception e){
new RuntimeException("文件服务器上传失败");
}finally{
if(trackerServer !=null){
try{
trackerServer.close();
}catch(IOException e){
new IOException("fastdfs connection close failed" + e.getMessage());
}
}
}
return null;
}
//文件下载
public static byte[] downloadFile(String groupName,String fileName){
TrackerServer trackerServer=null;
try{
TrackerClient tracker=new TrackerClient();
trackerServer=tracker.getConnectioon();
StorageServer storageServer=tracker.getStoreStorage(trackerServer);
//定义storage客户端对象,需要使用这个对象完成具体的上传下载删除操作
StorageClient storageClient=new StorageClient(trackerServer,storageServer);
byte[] b=storageClient.download_file(groupName,fileName);
return b;
}catch(Exception e){
new RuntimeException("文件服务器下载失败");
}finally{
if(trackerServer !=null){
try{
trackerServer.close();
}catch(IOException e){
new IOException("fastdfs connection close failed" + e.getMessage());
}
}
}
return null;
}
//文件删除
public static int deleteFile(String groupName,String fileName){
TrackerServer trackerServer=null;
int result=0;
try{
TrackerClient tracker=new TrackerClient();
trackerServer=tracker.getConnectioon();
StorageServer storageServer=null;
StorageClient storageClient=new StorageClient(trackerServer,storageServer);
result=storageClient.delete_file(groupName,fileName);
return b;
}catch(Exception e){
new RuntimeException("文件服务器删除文件失败");
}finally{
if(trackerServer !=null){
try{
trackerServer.close();
}catch(IOException e){
new IOException("fastdfs connection close failed" + e.getMessage());
}
}
}
return result
}
//将byte[]转为file
public static File downloadIsFile(String url){
String fileurl=url.replaceAll("/group1/","");
int n =url.lastIndexOf(".");
String ext=url.substring(n);
byte[] fileByte = FastdfsUtil.downloadFile("group1",fileurl);
int index=fileurl.lastIndexOf(".");
int index2=fileurl.lastIndexOf("/");
String falseFileName=fileurl.sunstring(index2+1,index);
File file =File.createTempFile(falseFileName,ext);//假文件名
BufferedOutputStream bos=null;
FileOutputStream fos =null;
try{
fos=new FileOutputStream(file);
bos=new BufferedOutputStream(fos);
bos.write(fileByte);
}catch(IOException e){
new IOException("文件类型转换错误");
}finally{
if(bos!=null){
bos.close();
}
if(fos!=null){
fos.close();
}
}
return file;
}
}
组装信息返回类
@Data
public class FastdfsPath{
private String host="";
private String port="";
private String groupName="";
private String remoteFilename="";
@Getter
private String fullPath;
public String getFullPath(){
fullPath="http://"+host+":"+port+"/"+groupName+"/"+remoteFilename;
return fullPath;
}
}