Java fastdfs客户端实现上传下载文件
作者:一路繁花似锦绣前程 发布时间:2023-11-26 09:45:41
标签:Java,fastdfs,客户端
一、项目结构
二、pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>A01fastdfs</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/net.oschina.zcx7878/fastdfs-client-java -->
<dependency>
<groupId>net.oschina.zcx7878</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27.0.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-io -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.9</source>
<target>1.9</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
三、fastdfs-client.properties
#http连接超时时间
fastdfs.connect_timeout_in_seconds=5
#tracker和storage网络通信超时时间
fastdfs.network_timeout_in_seconds=30
#字符编码
fastdfs.charset=utf-8
#tracker服务器地址,多个地址中间用英文逗号分隔
fastdfs.tracker_servers=192.168.2.105:22122
四、测试
package com.wuxi.test;
import org.csource.fastdfs.*;
import org.junit.Test;
import java.io.File;
import java.io.FileOutputStream;
public class MyTest {
//上传文件
@Test
public void testUpload() {
try {
//加载fastdfs-client.properties配置文件
ClientGlobal.initByProperties("config/fastdfs-client.properties");
//定义TrackerClient,用于请求TrackerServer
TrackerClient trackerClient = new TrackerClient();
//连接tracker
TrackerServer trackerServer = trackerClient.getConnection();
//获取storage
StorageServer storeStorage = trackerClient.getStoreStorage(trackerServer);
//创建storageClient
StorageClient1 storageClient1 = new StorageClient1(trackerServer, storeStorage);
//向storage服务器上传文件
//本地文件的路径
String path = "F:/java/resource/data.txt";
//上传成功后拿到文件Id
String fileId = storageClient1.upload_file1(path, "txt", null);
System.out.println(fileId);//group1/M00/00/00/wKgCaV9vaSaARBTKAAAAGjJpL2g017.txt
} catch (Exception e) {
e.printStackTrace();
}
}
//下载文件
@Test
public void testDownload() {
try {
//加载fastdfs-client.properties配置文件
ClientGlobal.initByProperties("config/fastdfs-client.properties");
//定义TrackerClient,用于请求TrackerServer
TrackerClient trackerClient = new TrackerClient();
//连接tracker
TrackerServer trackerServer = trackerClient.getConnection();
//获取storage
StorageServer storeStorage = trackerClient.getStoreStorage(trackerServer);
//创建storageClient
StorageClient1 storageClient1 = new StorageClient1(trackerServer, storeStorage);
//下载文件
//文件id
String fileId = "group1/M00/00/00/wKgCaV9vaSaARBTKAAAAGjJpL2g017.txt";
byte[] bytes = storageClient1.download_file1(fileId);
//使用输出流保存文件
FileOutputStream fileOutputStream = new FileOutputStream(new File("F:/data.txt"));
fileOutputStream.write(bytes);
} catch (Exception e) {
e.printStackTrace();
}
}
}
来源:https://www.cnblogs.com/linding/p/13737440.html
0
投稿
猜你喜欢
- 一、背景今天心血来潮,准备测试一下项目中 logback 的自动刷新功能,但是测试时发现并不生效。logback 的配置如下:<con
- 为了防止测试妹子或者用户频繁点击某个按钮,导致程序在短时间内进行多次数据提交or数据处理,那到时候就比较坑了~那么如何有效避免这种情况的发生
- 一、ehcahe的介绍EhCache 是一个纯Java的进程内缓存框架,具有高速、精干等特点,是Hibernate中默认的CacheProv
- 本文实例讲述了Android编程设计模式之中介者模式。分享给大家供大家参考,具体如下:一、介绍中介者模式(Mediator Pattern)
- idea中的Maven导包失败问题解决总结1.先确定idea和Maven 的配置文件settings 没有问题找到我们本地的maven仓库,
- Android 资源 id详解我们平时获取资源是通过 findViewById 方法进行的,比如我们常在onCreate方法中使用这样的语句
- vs2005中总是保留最近打开的项目和文件的记录,甚至是以删除的它也不删,-_-!下面介绍几种删除的方法:第一种:建立一个bat文件,以后双
- 前言之前学习的顺序表查询非常快,时间复杂度为O(1),但是增删改效率非常低,因为每一次增删改都会元素的移动。可以使用另一种存储方式-链式存储
- 解决办法:1.VCS--->Enable Version Control Integration2.选择要关联的版本工具补充:git
- 一、Images1、概述Image 类为Bitmap(位图) 和 Metafile(矢量图) 的类提供功能的抽象基类。Image类不能直接创
- Java提供一种机制叫做序列化,通过有序的格式或者字节序列持久化java对象,其中包含对象的数据,还有对象的类型,和保存在对象中
- 1. 字段取别名,和属性名保持一致映射文件<mapper namespace="com.atguigu.mybatis.ma
- Kotlin this详解及实例为了表示当前函数的接收者(receiver), 们使用this表达式:在类的成员函数中,this指向这个类的
- 差不多一年前在自己的项目中用过这效果,虽然很简单,但还是写写。1、首先在你的res目录下新建anim子目录,并在anim目录下新建两个文件:
- 本文实例讲述了Java之JFrame输出Helloworld的方法。分享给大家供大家参考。具体如下:JAVA的GUI程序的基本思路是以JFr
- 本文实例为大家分享了C#实现简化QQ聊天窗口的具体代码,供大家参考,具体内容如下如图样式,详细步骤如下整个窗体设置private void
- 本文实例讲述了Java定义泛型方法。分享给大家供大家参考,具体如下:一 点睛1 如果定义类、接口是没有使用类型形参,但定义方法时想自己定义类
- Android Studio 3.6正式版已经发布https://android-developers.googleblog.com/202
- 使用限定符在平板上面大多数时候采用的双页的模式,程序会在左侧列表上显示一个包含子项列表,右侧的面版会显示详细的内容的因为平板具有足够大的屏幕
- 本文实例为大家分享了java实现猜拳小游戏的具体代码,供大家参考,具体内容如下实现下图要求public class User {privat