• 使用fastDFS上传和下载图片文件


    package com.xuecheng.test.fastdfs;

    import org.csource.common.MyException;
    import org.csource.fastdfs.*;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;

    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;

    /**
    * @author newcityman
    * @date 2020/2/16 - 15:01
    */
    @SpringBootTest
    @RunWith(SpringRunner.class)
    public class TestFastDFS {
    //上传文件
    @Test
    public void testUpload(){
    try {
    //加载fastdfs-client.properties配置文件
    ClientGlobal.initByProperties("config/fastdfs_client.properties");
    //定义TrackerClient,用于请求Tracker
    TrackerClient trackerClient = new TrackerClient();
    //连接Tracker
    TrackerServer trackerServer = trackerClient.getConnection();
    //获取Storage服务器
    StorageServer storageServer= trackerClient.getStoreStorage(trackerServer);
    //创建一个StorageClient
    StorageClient1 storageClient1 = new StorageClient1(trackerServer,storageServer);
    //向storage服务器上传文件
    String filePath="h:/wx_cz.jpg";
    String fileId = storageClient1.upload_file1(filePath, "jpg", null);
    System.out.println(fileId);
    //group1/M00/00/00/wKgAaV5JY7CAGDfKAAAjb9XlGuI005.jpg
    } catch (IOException e) {
    e.printStackTrace();
    } catch (MyException e) {
    e.printStackTrace();
    }
    }

    //下载文件
    @Test
    public void testDownload(){
    try {
    //加载fastdfs-client.properties配置文件
    ClientGlobal.initByProperties("config/fastdfs_client.properties");
    //定义TrackerClient,用于请求Tracker
    TrackerClient trackerClient = new TrackerClient();
    //连接Tracker
    TrackerServer trackerServer = trackerClient.getConnection();
    //获取Storage服务器
    StorageServer storageServer= trackerClient.getStoreStorage(trackerServer);
    //创建一个StorageClient
    StorageClient1 storageClient1 = new StorageClient1(trackerServer,storageServer);
    //下载文件
    //文件id
    String fileId="group1/M00/00/00/wKgAaV5JY7CAGDfKAAAjb9XlGuI005.jpg";
    byte[] bytes = storageClient1.download_file1(fileId);
    //使用输出流保存文件
    FileOutputStream fileOutputStream = new FileOutputStream(new File("i:/lg.jpg"));
    fileOutputStream.write(bytes);
    } catch (IOException e) {
    e.printStackTrace();
    } catch (MyException e) {
    e.printStackTrace();
    }

    }

    }
  • 相关阅读:
    客户端性能优化
    session、cookie、token的区别
    java 动态代理模式(jdk和cglib)
    java 静态代理模式
    java ReentrantLock 公平锁 非公平锁 测试
    java ReentrantLock结合条件队列 实现生产者-消费者模式 以及ReentratLock和Synchronized对比
    VS2010 开发 VB6.0 activeX控件 dll
    C++CLI语法 在项目中的使用
    word 内容控件属性编辑
    VC调用静态库、动态库
  • 原文地址:https://www.cnblogs.com/newcityboy/p/12319715.html
Copyright © 2020-2023  润新知