Javafastdfs客户端实现上传下载文件

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Javafastdfs客户端实现上传下载⽂件⼀、项⽬结构

⼆、pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="/POM/4.0.0"

xmlns:xsi="/2001/XMLSchema-instance"

xsi:schemaLocation="/POM/4.0.0 /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:///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:///artifact/mons/commons-io -->

<dependency>

<groupId>mons</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⽹络通信超时时间

work_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();

}

}

}

以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

相关文档
最新文档