欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

Java fastdfs客户端实现上传下载文件

时间:2021-07-15 09:30:11|栏目:JAVA代码|点击:

一、项目结构

二、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();
    }
  }
}

上一篇:Java编程中利用InetAddress类确定特殊IP地址的方法

栏    目:JAVA代码

下一篇:详解Http请求中Content-Type讲解以及在Spring MVC中的应用

本文标题:Java fastdfs客户端实现上传下载文件

本文地址:http://www.codeinn.net/misctech/157443.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有