【FastDFS】——上传文件实例

本文介绍如何在Java环境中使用FastDFS进行文件的上传与下载操作。主要包括配置tracker_server地址、创建客户端对象、调用上传与下载方法的具体步骤,并提供代码示例。

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

安装流程:《FastDFS在Linux上的安装》,其中包括了和nginx的整合步骤。
        jar包:
                commons-io、commons-fileupload、fastdfs-client-java.jar
                前两个是文件管理不可少的jar包,比较常见。第三个是在java中使用FastDFS时需要的一个官方jar包。也                可以下载源代码,可以将源代码添加到项目工程中,使用maven install命令将其安装到本地仓库后在项目中依               赖使用(maven中没有改jar包)。

上传流程
        1、初始化全局配置。加载一个配置文件。主要是配置tracker_server地址
        2、创建一个TrackerClient对象。
        3、创建一个TrackerServer对象。
        4、声明一个StorageServer对象,null。
        5、获得StorageClient对象。
        6、直接调用StorageClient对象方法上传文件即可


        具体实现:
                1、添加配置文件,配置tracker_server地址,ip为tracker安装的服务器,端口号一般22122。

                //2、初始化全局配置。加载一个配置文件。主要是配置tracker_server地址
		ClientGlobal.init("F:\\计算机\\Java\\07_淘淘\\projectyxf\\taotao-manager\\taotao-manager-web\\src\\main\\resources\\properties\\client.conf");
			
		//3、创建一个TrackerClient对象。
		TrackerClient trackerClient = new TrackerClient();
			
		// 4、创建一个TrackerServer对象。
		TrackerServer trackerServer = trackerClient.getConnection();
			
		// 5、声明一个StorageServer对象,null。
			StorageServer storageServer = null;
			
		// 6、获得StorageClient对象。
			StorageClient storageClient = new StorageClient(trackerServer, storageServer);
		
		//7、直接调用StorageClient对象方法上传文件即可
		String[] strings=storageClient.upload_file("C:\\Users\\xiaofei\\Desktop\\1.png", "png", null);
		for (String string : strings) {
			System.out.println(string);
		}

        upload_file方法返回的结果为:组名+文件名

        实例中用到的 upload_file 方法 API——
      upload_file(String local_filename, String file_ext_name, NameValuePair[] meta_list) throws IOException, MyException
		upload file to storage server (by file name) 
			Parameters: 
				local_filename local filename to upload 
				file_ext_name file ext name, do not include dot(.), null to extract ext name from the local filename 
				meta_list meta info array 
			Returns: 
				2 elements string array if success:
				□ results[0]: the group name to store the file 
				□ results[1]: the new created filename
				return null if fail


        访问:ip/组名/文件名,如下图为上传的启动nginx、tracker server及storage server命令的截图。

下载实现
	@Test
	public void Download() throws Exception{          
	        try {               
	        	ClientGlobal.init("F:\\计算机\\Java\\07_淘淘\\projectyxf\\taotao-manager\\taotao-manager-web\\src\\main\\resources\\properties\\client.conf");              
	            TrackerClient tracker = new TrackerClient();               
	            TrackerServer trackerServer = tracker.getConnection();              
	            StorageServer storageServer = null;               
	            StorageClient storageClient = new StorageClient(trackerServer, storageServer);               
	            byte[] b = storageClient.download_file("group1", "M00/00/00/wKipHlhYl8GAWpvUAAAmlNozzbU177.png");               
	            IOUtils.write(b, new FileOutputStream("D:/"+UUID.randomUUID().toString()+".png"));          
	            }   
	        catch (Exception e) {               
	            e.printStackTrace();           
	        }       
	 }



        实例中用到的FileOutputStream 方法API——
FileOutputStream(String name) throws FileNotFoundException
	Creates a file output stream to write to the file with the specified name. A new FileDescriptor object is created to represent this file connection. 
	First, if there is a security manager, its checkWrite method is called with name as its argument. 
	If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason then a FileNotFoundException is thrown. 
		Parameters: 
			name the system-dependent filename



        其他上传和下载的api可自行查询,包括批量上传等。
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值