FastDFS之java客户端使用

为了方便应用程序的访问FastDFS,官网提供了fastdfs-client-java,以便更好的与应用程序结合使用。

下载fastdfs-client-java源码添加到项目工程里面,添加配置文件:fdfs_client.conf

这个jar包在中央仓库是没有的,我们可以将源码下载下来,使用maven install安装到本地仓库。

附上pom.xml文件

<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.csource</groupId>
    <artifactId>fastdfs-client-java</artifactId>
    <version>1.25</version>
    <name>fastdfs-client-java</name>
    <description>fastdfs client with java</description>
    <packaging>jar</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
    </properties>

    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <skip>true</skip>
                    <source>1.5</source>
                    <target>1.5</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                        <configuration>
                            <excludes>
                                <exclude>**/test/*.class</exclude>
                            </excludes>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

在自己的maven项目中加入坐标

<groupId>org.csource</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.25</version>

即可。

测试文件上传

项目的配置文件

fdfs_client.conf

connect_timeout = 2
network_timeout = 30
#对这些设置还不是很清楚
charset = ISO8859-1
http.tracker_http_port = 8080
http.anti_steal_token = no
http.secret_key = FastDFS1234567890

#tracker_server的设置是必须的
tracker_server = 192.168.0.111:22122
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;

public class FastDFSTest {
    public static void main(String[] args) throws Exception {
        //加载配置文件的方式
        String configFileName = "D:\\IdeaProject\\taotao\\taotao-manager\\taotao-manager-web\\src\\              main\\resources\\properties\\fdfs_client.conf";
        try {
            ClientGlobal.init(configFileName);
        }catch(Exception e){
            e.printStackTrace();
        }
        File file = new File("C:/Users/Public/Pictures/Sample Pictures/qie.jpg");
        //返回储存路径:group1 M00/00/00/wKhuW1Vmj6KAZ09pAAC9przUxEk788.jpg
        String[] files =  uploadFile(file, "test.jpg", file.length());
        System.out.println(Arrays.asList(files));
    }
    /**
     * 上传文件
     */
    public static String[] uploadFile(File file, String uploadFileName, long fileLength) throws IOException {
        byte[] fileBuff = getFileBuffer(new FileInputStream(file), fileLength);
        String[] files = null;
        String fileExtName = "";
        if (uploadFileName.contains(".")) {
            fileExtName = uploadFileName.substring(uploadFileName.lastIndexOf(".") + 1);
        } else {
            System.out.println("Fail to upload file, because the format of filename is illegal.");
            return null;
        }

        // 建立连接
        TrackerClient tracker = new TrackerClient();
        TrackerServer trackerServer = tracker.getConnection();
        StorageServer storageServer = null;
        StorageClient client = new StorageClient(trackerServer, storageServer);

        // 设置元信息
        NameValuePair[] metaList = new NameValuePair[3];
        metaList[0] = new NameValuePair("fileName", uploadFileName);
        metaList[1] = new NameValuePair("fileExtName", fileExtName);
        metaList[2] = new NameValuePair("fileLength", String.valueOf(fileLength));

        // 上传文件
        try {
            files = client.upload_file(fileBuff, fileExtName, metaList);
        } catch (Exception e) {
            System.out.println("Upload file \"" + uploadFileName + "\"fails");
        }
        trackerServer.close();
        return files;
    }
    private static byte[] getFileBuffer(InputStream inStream, long fileLength) throws IOException {

        byte[] buffer = new byte[256 * 1024];
        byte[] fileBuffer = new byte[(int) fileLength];

        int count = 0;
        int length = 0;

        while ((length = inStream.read(buffer)) != -1) {
            for (int i = 0; i < length; ++i) {
                fileBuffer[count + i] = buffer[i];
            }
            count += length;
        }
        return fileBuffer;
    }

}

程序的执行结果

[group1, M00/00/00/wKgAb1dAU0WAQEhwAAvea_OGt2M139.jpg]

拼接之后就是图片的访问url了

http://192.168.0.111/group1/M00/00/00/wKgAb1dAU0WAQEhwAAvea_OGt2M139.jpg

时间: 2024-08-01 22:46:44

FastDFS之java客户端使用的相关文章

docker安装fastdfs与java客户端测试

一.docker 安装FastDFS 1.拉取镜像 docker pull morunchang/fastdfs 2.创建并启动tracker容器 docker run -d --name=tracker -v /home/fastdfs_docker/fdfs/tracker:/data/fast_data --privileged=true --net=host morunchang/fastdfs sh tracker.sh 3.创建并启动storage容器.此处只做单机版测试 注意:由于

FastDFS分布文件系统Java客户端使用

原文链接:http://blog.csdn.net/xyang81/article/details/52847311 Java客户端源代码和jar:链接:http://pan.baidu.com/s/1jHIwtsq 密码:n757 我将官方提供的sdk封装了一个工具类,将工具类导入工程即可使用,如下所示: package com.digi_zones.fdfs; import org.apache.commons.io.FileUtils; import org.apache.commons.

FastDFS分布文件系统Java客户端集成

参考博客:http://blog.csdn.net/xyang81/article/details/52847311 官网Java客户端源代码: https://github.com/happyfish100/fastdfs-client-java 从上面地址下载压缩包,解压 使用idea工具打开执行clean install将包打到本地的maven仓库 如果协同开发,可将该包上传到公司的私服,供大家一起下载,就不需要下载该工程进行本地打包,只需要引pom地址 工程目录结构 根据官方提供的sdk

FastDFS单机搭建以及java客户端Demo

http://blog.csdn.net/u012453843/article/details/69951920 http://blog.csdn.net/xyang81/article/details/52847311 http://blog.csdn.net/kingboyworld/article/details/52299602 参考了这几个搭建了FastDFS文件系统 主要是fastDFS,nginx,以及在nginx中加入fastDFS模块:这里只有一台服务器,所以搭建的是单机版的.

java客户端作为kafka的consumer报错org.I0Itec.zkclient.exception.ZkTimeoutException

出错现象: java客户端编程作为kafka的消费端,连接kafka的broker报错 出错原因分析: 当服务器配置或者网络环境较差时,会出现连接zk超时的情况出现; 解决方法:将程序中的timeout数值调大 props.put("zookeeper.session.timeout.ms", "15000");

hadoop系列二:HDFS文件系统的命令及JAVA客户端API

转载请在页首明显处注明作者与出处 http://www.cnblogs.com/zhuxiaojie/p/6391518.html 一:说明 此为大数据系列的一些博文,有空的话会陆续更新,包含大数据的一些内容,如hadoop,spark,storm,机器学习等. 当前使用的hadoop版本为2.6.4 上一篇:hadoop系列一:hadoop集群安装 二:HDFS的shell命令 上一章说完了安装HADOOP集群部分,这一张讲HDFS. 其实基本上操作都是通过JAVA API来操作,所以这里的s

Elasticsearch及java客户端jest使用

本文使用Github中的Elasticsearch-rtf,已经集成了众多的插件,例如必须使用的中文分词等,可以简单的通过配置来启用中文分词.本文主要分为以下几部分: 1.配置和启用中文分词: 2.定义索引的mapping 3.java客户端jest创建和检索索引 4.高亮检索结果 5.集群配置 工具: 由于Elasticsearch完全REST风格,支持json进行交互,简单的curl工具就可以完成很多功能,本文中也有部分操作会直接使用curl.window环境下建议下载一个可执行的curl.

读书笔记-HBase in Action-第二部分Advanced concepts-(3)非Java客户端

HBase Shell HBase shell使用起来最方便,进入HBase shell控制台即可使用. $ $HBASE_HOME/bin/hbase shell 常见操作有create(创建表)/put(插入或更新数据)/get(根据rowkey查询)/scan(范围查询)/delete(删除列)/deleteAll(根据rowkey删除整行数据)/disable&drop(禁用表之后再删除). 基于数据库的项目,往往会在某个目录下存储专门的sql脚本,记录每次迭代数据库变更:同理,HBas

ElasticSearch java客户端更新时出现的错误:NoNodeAvailableException[None of the configured nodes are available

下午尝试 用ElasticSearch  的java客户端去做数据检索工作,测试了一下批量更新,代码如下: public static void bulkUpdateGoods(List<Goods> goods) throws IOException, InterruptedException, ExecutionException { Client client = null; try { client = TransportClient.builder().build() .addTra