java端调用fastdfs存储文件的工具方法之一(针对单台追踪器的)

import java.io.IOException;

import org.csource.common.MyException;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;

public class fastdfsUtil {
	private TrackerClient tClient;
	private TrackerServer tServer;
	private StorageClient1 sClient;
	
	/**
	 * 
	 * @param path 配置文件的名字  (在配置文件中配追踪器的IP和端口号:tracker_server=192.168.101.131:22122)
	 */
	public fastdfsUtil(String path){
		try {
			ClientGlobal.init(fastdfsUtil.class.getResource("/").getPath()+path);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (MyException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * @param file 
	 * @param host
	 * @return
	 */
	public String upload(Multipartfile file,String host){
		try {
			//截取后缀
			int lastindex = file.getOriginalFilename().lastIndexOf(".");
			String houzui = file.getOriginalFilename().substring(lastindex+1);
			
			tClient = new TrackerClient();
			tServer = tClient.getConnection();
			
			sClient = new StorageClient1(tServer,null);
			String upload_file1 = sClient.upload_file1(file.getBytes(), houzui, null);
			return host+"/"+upload_file1;
			} catch (Exception e) {
			e.printStackTrace();
			} 
		return null;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Java使用FastDFS删除文件,需要使用FastDFS的客户API。以下是一个简单的示例代码: ```java import org.csource.fastdfs.*; public class FastDFSDeleteExample { public static void main(String[] args) { String fileUrl = "http://your.fastdfs.server/path/to/file.jpg"; try { ClientGlobal.init("path/to/fastdfs.properties"); TrackerClient tracker = new TrackerClient(); TrackerServer trackerServer = tracker.getConnection(); StorageServer storageServer = null; StorageClient storageClient = new StorageClient(trackerServer, storageServer); int result = storageClient.delete_file("group1", "M00/00/00/" + fileUrl.substring(fileUrl.lastIndexOf("/") + 1)); if (result == 0) { System.out.println("File deleted successfully"); } else { System.out.println("Failed to delete file"); } } catch (Exception e) { e.printStackTrace(); } } } ``` 在上面的代码,我们首先通过调用`ClientGlobal.init`来初始化FastDFS客户。然后,我们使用`TrackerClient`和`TrackerServer`来获取一个`StorageClient`实例。最后,我们调用`delete_file`方法来删除指定的文件。 请注意,`delete_file`方法接受两个参数:存储组名和文件名。在本例存储组名是“group1”,而文件名是从文件URL提取出来的。 此外,`delete_file`方法返回一个整数值,表示删除操作的结果。如果结果为0,则表示成功删除文件,否则表示删除文件失败。 最后,需要注意的是,在调用完`delete_file`方法后,我们应该关闭与FastDFS服务的连接,以释放资源。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值