使用fastdfs文件服务器进行上传下载删除

该博客详细介绍了FastDFS的配置步骤,包括fdfs_config.properties、application-dev.yml和zuul的配置,并提供了文件上传、下载和删除的Java工具类实现。FastdfsUtil类中初始化配置文件并实现了关键操作,确保了文件服务的正常运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值