centos64位中 fastDFS的安装以及nginx反向代理实现搭建图片服务器(三)之图片服务器的使用

本文介绍如何使用Java客户端操作FastDFS文件服务器,包括配置环境、上传文件及使用封装工具类的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目前我对这个图片服务器的使用是利用java语言。

官方提供了一个jar:
fastdfs_client_v1.24.jar

使用方法:

1、FastDFS提供的jar包添加到工程中

   由于我使用的maven工程管理,所以我首先需要将fastdfs_client_v1.24.jar添加到本地仓库:

      添加方法详见见:

http://blog.youkuaiyun.com/qq_20565303/article/details/66971981

      我这里直接在mvn中使用命令:

mvn install:install-file -Dfile=C:\fastdfs_client_v1.24.jar -DgroupId=fastdfs_client -DartifactId=fastdfs_client -Dversion=1.24 -Dpackaging=jar
      注意:我这个是在win10中添加,使用win10自带的shell命令无法成功,使用cmd命令就可已成功,原因我还没有看。

      然后需要在项目中的pom文件中添加:

<dependency>
      <groupId>fastdfs_client</groupId>
      <artifactId>fastdfs_client</artifactId>
      <version>1.24</version>
 </dependency>

      然后maven—>update下 就好了


2、初始化全局配置。加载一个配置文件。

          

       配置文件的内容为:


tracker_server=192.168.147.130:22122

      注意:这个tracker_server的值就是服务器上tracker服务配置文件里面的值。


3、创建一个FastdfsTest文件。

      1)创建一个TrackerClient对象。

      2)创建一个TrackerServer对象。

      3)声明一个StorageServer对象,null

   4)获得StorageClient对象。

      5)直接调用StorageClient对象方法上传文件即可。

 文件代码如下:

package com.test.fastdfs;
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; import org.junit.Test; public class FastdfsTest {  @Test  public void testUpload() throws Exception {   // 1、把FastDFS提供的jar包添加到工程中   // 2、初始化全局配置。加载一个配置文件。   ClientGlobal.init("D:\\workspace\\yigouwu-manager\\yigouwu-manager-web\\src\\main\\resources\\properties\\client.conf");   // 3、创建一个TrackerClient对象。   TrackerClient trackerClient = new TrackerClient();   // 4、创建一个TrackerServer对象。   TrackerServer trackerServer = trackerClient.getConnection();   // 5、声明一个StorageServer对象,null。   StorageServer storageServer = null;   // 6、获得StorageClient对象。   StorageClient storageClient = new StorageClient(trackerServer, storageServer);   // 7、直接调用StorageClient对象方法上传文件即可。   String[] strings = storageClient.upload_file("C:\\Users\\WANGTIANYUAN\\Desktop\\235113-1403260U22059[1].jpg", "jpg", null);   for (String string : strings) {    System.out.println(string);   }  } }

其中上面的那个配置文件的路径取法:

右键点击配置文件client.conf,选择perproties,然后如图选择Location全路径:

    


4、测试


对FastdfsTest文件进行Junit Test执行:


然后将拿到的地址在浏览器上访问:

其中地址为:http://192.168.147.130/group1/M00/00/00/wKiTgllFXD2AArZ5AANrSBHtS0I016.jpg


5、使用fastdfs-client-java封装的工具类

工具类代码:

package com.test.fastdfs;
import java.io.FileNotFoundException; import java.io.IOException;
import org.csource.common.MyException; import org.csource.common.NameValuePair; import org.csource.fastdfs.ClientGlobal; import org.csource.fastdfs.StorageClient1; import org.csource.fastdfs.StorageServer; import org.csource.fastdfs.TrackerClient; import org.csource.fastdfs.TrackerServer;
public class FastDFSClient {    private TrackerClient trackerClient = null;  private TrackerServer trackerServer = null;  private StorageServer storageServer = null;  private StorageClient1 storageClient = null;    public FastDFSClient(String conf) throws FileNotFoundException, IOException, MyException {   if(conf.contains("classpath:")){    conf = conf.replace("classpath:", this.getClass().getResource("/").getPath());   }   ClientGlobal.init(conf);   trackerClient = new TrackerClient();   trackerServer = trackerClient.getConnection();   storageServer = null;   storageClient = new StorageClient1(trackerServer, storageServer);  }    /**   * 上传文件方法   * <p>Title:uploadFile</p>   * <p>Description:</p>   * @param fileName 文件全路径   * @param extName 文件扩展名,不包含(.)   * @param metas 文件扩展信息   * @return   * @throws IOException   * @throws MyException   */  public String uploadFile(String fileName, String extName, NameValuePair[] metas) throws IOException, MyException{   String result = storageClient.upload_file1(fileName, extName, metas);   return result;  }    public String uploadFile(String fileName, String extName) throws IOException, MyException{   return uploadFile(fileName, extName, null);  }    public String uploadFile(String fileName) throws IOException, MyException{   return uploadFile(fileName, null, null);  }
 /**   * 上传文件方法   * <p>Title:uploadFile</p>   * <p>Description:</p>   * @param fileContent 文件的内容,字节数组   * @param extName 文件扩展名,不包含(.)   * @param metas 文件扩展信息   * @return   * @throws IOException   * @throws MyException   */  public String uploadFile(byte[] fileContent, String extName, NameValuePair[] metas) throws IOException, MyException{   String result = storageClient.upload_file1(fileContent, extName, metas);   return result;  }    public String uploadFile(byte[] fileContent, String extName) throws IOException, MyException{   return uploadFile(fileContent, extName, null);  }    public String uploadFile(byte[] fileContent) throws IOException, MyException{   return uploadFile(fileContent, null, null);  } }


测试:

	@Test
	public void testFastDfsClient() throws Exception {
		FastDFSClient client = new FastDFSClient("D:\\workspace\\yigouwu-manager\\yigouwu-manager-web\\src\\main\\resources\\properties\\client.conf");
		String uploadFile = client.uploadFile("C:\\Users\\WANGTIANYUAN\\Desktop\\timg[1].jpg", "jpg");
		System.out.println(uploadFile);
	}

执行结果:



访问这个图片:




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值