zoukankan      html  css  js  c++  java
  • fastdfs java客户端操作

    https://github.com/happyfish100/fastdfs-client-java

    到此处下载下来demo

    这里采用maven的方式

    mvn clean install

    上传到本地仓库

    mvn install:install-file -DgroupId=org.csource -DartifactId=fastdfs-client-java -Dversion=1.29-SNAPSHOT -Dpackaging=jar -Dfile=fastdfs-client-java-1.29-SNAPSHOT.jar

    引入文件上传依赖

    <dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.4</version>
    </dependency>

    <dependency>
    <groupId>org.csource</groupId>
    <artifactId>fastdfs-client-java</artifactId>
    <version>1.29-SNAPSHOT</version>
    </dependency>


    项目结构

     代码如下:

    @RestController
    public class AttachmentController {

    @RequestMapping("/upload")
    public String fdfsUpload(@RequestParam MultipartFile file){
    try {
    ClientGlobal.initByProperties("fdfs-client.properties");
    System.out.println("初始化信息:"+ClientGlobal.configInfo());
    // 链接FastDFS服务器,创建tracker和Stroage
    TrackerClient trackerClient = new TrackerClient();
    TrackerServer trackerServer = trackerClient.getTrackerServer();
    StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
    StorageClient storageClient = new StorageClient(trackerServer,storageServer);
    String tempFileName = file.getOriginalFilename();
    String fileExtName = tempFileName.substring(tempFileName.lastIndexOf(".")+1);
    //上传文件,只需要文件字节数组,第二个参数是文件扩展名,第三个参数是元数据
    String[] strings = storageClient.upload_file(file.getBytes(), fileExtName, null);
    String result="";
    for (int i=0;i<strings.length;i++){

    if(i==strings.length-1){
    result+=strings[i];
    }else{
    result+=strings[i]+"/";
    }

    }
    System.out.println(result);


    } catch (Exception e) {
    e.printStackTrace();
    }

    return null;
    }

    /**
    * FastDFS实现文件下载
    *
    * @param filePath
    */
    @RequestMapping(value = "/download", method = RequestMethod.GET)
    @ResponseBody
    public void fdfsDownload(String filePath) {
    try {
    ClientGlobal.initByProperties("fdfs-client.properties");

    // 链接FastDFS服务器,创建tracker和Stroage
    TrackerClient trackerClient = new TrackerClient();
    TrackerServer trackerServer = trackerClient.getTrackerServer();
    StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
    StorageClient storageClient = new StorageClient(trackerServer,storageServer);
    byte[] b = storageClient.download_file("group1", filePath);
    if (b == null) {
    throw new IOException("文件" + filePath + "不存在");
    }

    String fileName = filePath.substring(filePath.lastIndexOf("/") + 1);
    FileOutputStream fileOutputStream = new FileOutputStream("E://" + fileName);
    IOUtils.write(b, fileOutputStream);
    fileOutputStream.close();
    System.out.println("文件下载成功");
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    /**
    * FastDFS实现文件删除
    *
    * @param filePath
    */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    @ResponseBody
    public void fdfsDelete(String filePath) {
    try {
    ClientGlobal.initByProperties("fdfs-client.properties");

    TrackerClient trackerClient = new TrackerClient();
    TrackerServer trackerServer = trackerClient.getTrackerServer();
    StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
    StorageClient storageClient = new StorageClient(trackerServer,storageServer);
    int deleteResult = storageClient.delete_file("group1", filePath);

    if (deleteResult== 0) {
    System.out.println("文件删除成功");
    }else {
    System.out.println("文件删除失败");
    }



    } catch (Exception e) {
    e.printStackTrace();
    }
    }


    }


    亲测可用
  • 相关阅读:
    EasyUI左边树菜单和datagrid分页
    Linux上安装Redis教程
    TreeMap和TreeSet的区别与联系
    将Map<String, List<Map<String,Object>>>进行排序
    Linux系统安装JDK和Tomcat
    点击添加按钮,使用ajax动态添加一行和移除一行,并且序号重新排序和数据不重复操作判断
    23种设计模式汇总整理
    SSH架构BaseDao实现
    双击Table表格td变成text修改内容
    用户找回密码功能JS验证邮箱通过点击下一步隐藏邮箱输入框并修改下一步按钮的ID
  • 原文地址:https://www.cnblogs.com/changefl/p/12780701.html
Copyright © 2011-2022 走看看