fastDFS+LibreOffice文件上传/下载/预览/删除/大小转换(一):实体类和配置文件设置

       因为我也是喜欢当伸手党,所以讲究代码copy即用。下面的文件实体类设置,只要提前搭好了fastDFS的文件服务器和有配置文件,就能用了。先看这个实体类的属性生成的对应字段格式,我这里是把它转换成了JSON对象存储。

[{
"id":"26993283811004",
"fileName":"测试文件二.txt",
"fdfsGroup":"group1",
"fdfsPath":"M00/00/06/rBkFTFvpDrOAW-FnAAAAFQZeVlU202.txt",
"remotePath":"http://172.25.5.76:8880/group1/M00/00/06/rBkFTFvpDrOAW-FnAAAAFQZeVlU202.txt?
             token\u003dee866eff35eb4af13491540934e1fdfb\u0026ts\u003d1542000225",
"ext":"txt",
"title":"测试文件二",
"size":"21B"},

 {
"id":"26993283811004",
"fileName":"测试文件一.txt",
"fdfsGroup":"group1",
"fdfsPath":"M00/00/06/rBkFTFvpDrOAFbw_AAAAFQZeVlU632.txt",  "remotePath":"http://172.25.5.76:8880/group1/M00/00/06/rBkFTFvpDrOAFbw_AAAAFQZeVlU632.txt?
              token\u003ddc9d00bb4759a57b39fe6d3ca1edff77\u0026ts\u003d1542000225",
"ext":"txt",
"title":"测试文件一",
size":"21B"
}]

       

       该实体类主要有几个重要属性:fdfsGroup、fdfsPath、remotePath,之后在文件下载、预览功能会用到。然后是实体类里主要的方法。

一:在文件预览功能中用到的,通过判断文件格式把文件转换成pdf输出:

private static final Map<String, DocumentFormat> typeMap = new HashMap<String, DocumentFormat>();

static {}


public DocumentFormat getType() {
        return null == ext ? null : typeMap.get(ext.toUpperCase());
}

这个方法在Controller中调用方式如下:

// 转换格式,注意:execute必须要用到,此为转换
JodConverter.convert(byteArrayInputStream).as(fileInfo.getType())
.to(byteArrayOutputStream).as(DefaultDocumentFormatRegistry.PDF).execute();

 

二: 文件删除:判断是否和fastdfs中文件相同(临时使用)

public boolean isEqualFdfs(FastDfsInfo fastDfsInfo) {
       
}


/**
 * 组装fastdfs对象,用于删除
 */
public FastDfsInfo getFastDfsInfo() {
     return new FastDfsInfo(this.fdfsGroup, this.fdfsPath);
}

 

三:判断文件是不是图片或者pdf

public boolean getIsImage() {
}


public boolean getIsPdf() {
}

四:上传的文件都是以字节为单位,所以根据文件大小灵活转换文件的大小单位,存到数据库

public static String getPrintSize(long size) {}

下面是fastDFS配置文件的代码:

#连接超时的时限,单位为秒
fdfs.connect_timeout=60
#网络超时的时限,单位为秒
fdfs.network_timeout=80
#防盗链配置
fdfs.anti_steal_token=xxxxx
fdfs.secret_key=xxxxx
#池的大小
fdfs.pool_max_total=100
#连接池中最大空闲的连接数
fdfs.pool_max_idel=10
#tracker的配置 ","逗号分隔
fdfs.tracker_servers=172.25.5.66:6666
#HTTP访问服务的端口号
fdfs.tracker_http_port=8880
#nginx的对外访问地址,如果没有端口号,将取g_tracker_http_port配置的端口号 ","逗号分隔
fdfs.nginx_address=172.25.5.66:8880

下面是实体类完整代码: 

/**
 * Copyright (c) 2018,TravelSky. 
 * All Rights Reserved.
 * TravelSky CONFIDENTIAL
 * 
 * Project Name:fileServerDemo
 * Package Name:com.travelsky.sh.domain
 * File Name:FileInfo.java
 * Date:2018-10-17 上午10:33:56
 * 
 */
package com.travelsky.sh.modules.notice.entity;

import com.travelsky.sh.fastdfs.FastDfsInfo;
import org.jodconverter.document.DefaultDocumentFormatRegistry;
import org.jodconverter.document.DocumentFormat;

import java.util.HashMap;
import java.util.Map;

/**
 * 文件对象
 * @author fuxy
 * 2018年10月16日15:52:33
 */
public class FileInfo {

    /**
     * 自定义的ID
     */
    private String id;
    /**
     * 上传的原始文件名,fastdfs上传后会修改文件名
     */
    private String fileName;
    /**
     * fastdfs中的组,上传后会返回,删除和获取时需要
     */
    private String fdfsGroup;
    /**
     * fastdfs中的路径,上传后会返回,删除和获取时需要
     */
    private String fdfsPath;
    /**
     * 搭建nginx后,返回的外部可以直接访问的链接
     */
    private String remotePath;
    /**
     * 文件后缀,用于判断是否是图片(临时使用)
     */
    private String ext;
    /**
     * 上传的原始文件名,无后缀
     */
    private String title;
    /**
     * 上传的原始文件大小
     */
    private String size;

    private static final Map<String, DocumentFormat> typeMap = new HashMap<String, DocumentFormat>();
    static {
        typeMap.put("DOC", DefaultDocumentFormatRegistry.DOC);
        typeMap.put("CSV", DefaultDocumentFormatRegistry.CSV);
        typeMap.put("DOCX", DefaultDocumentFormatRegistry.DOCX);
        typeMap.put("HTML", DefaultDocumentFormatRegistry.HTML);
        typeMap.put("ODG", DefaultDocumentFormatRegistry.ODG);
        typeMap.put("ODP", DefaultDocumentFormatRegistry.ODP);
        typeMap.put("ODS", DefaultDocumentFormatRegistry.ODS);
        typeMap.put("ODT", DefaultDocumentFormatRegistry.ODT);
        typeMap.put("OTG", DefaultDocumentFormatRegistry.OTG);
        typeMap.put("OTP", DefaultDocumentFormatRegistry.OTP);
        typeMap.put("OTS", DefaultDocumentFormatRegistry.OTS);
        typeMap.put("OTT", DefaultDocumentFormatRegistry.OTT);
        typeMap.put("PDF", DefaultDocumentFormatRegistry.PDF);
        typeMap.put("PNG", DefaultDocumentFormatRegistry.PNG);
        typeMap.put("PPT", DefaultDocumentFormatRegistry.PPT);
        typeMap.put("PPTX", DefaultDocumentFormatRegistry.PPTX);
        typeMap.put("RTF", DefaultDocumentFormatRegistry.RTF);
        typeMap.put("SVG", DefaultDocumentFormatRegistry.SVG);
        typeMap.put("SWF", DefaultDocumentFormatRegistry.SWF);
        typeMap.put("SXC", DefaultDocumentFormatRegistry.SXC);
        typeMap.put("SXI", DefaultDocumentFormatRegistry.SXI);
        typeMap.put("SXW", DefaultDocumentFormatRegistry.SXW);
        typeMap.put("TSV", DefaultDocumentFormatRegistry.TSV);
        typeMap.put("TXT", DefaultDocumentFormatRegistry.TXT);
        typeMap.put("WPD", DefaultDocumentFormatRegistry.WPD);
        typeMap.put("XLS", DefaultDocumentFormatRegistry.XLS);
        typeMap.put("XLSX", DefaultDocumentFormatRegistry.XLSX);
    }

    public FileInfo() {
    }

    public FileInfo(String fileName, String ext, FastDfsInfo dfsInfo) {
        super();
        this.id = "" + System.nanoTime();
        this.fileName = fileName;
        this.fdfsGroup = dfsInfo.getGroup();
        this.fdfsPath = dfsInfo.getPath();
        this.remotePath = dfsInfo.getFileAbsolutePath();
        this.ext = ext;
    }

    public FileInfo(String fileName, String fdfsGroup, String fdfsPath, String remotePath, String ext) {
        super();
        this.id = "" + System.nanoTime();
        this.fileName = fileName;
        this.fdfsGroup = fdfsGroup;
        this.fdfsPath = fdfsPath;
        this.remotePath = remotePath;
        this.ext = ext;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getFdfsGroup() {
        return fdfsGroup;
    }

    public void setFdfsGroup(String fdfsGroup) {
        this.fdfsGroup = fdfsGroup;
    }

    public String getFdfsPath() {
        return fdfsPath;
    }

    public void setFdfsPath(String fdfsPath) {
        this.fdfsPath = fdfsPath;
    }

    public String getRemotePath() {
        return remotePath;
    }

    public void setRemotePath(String remotePath) {
        this.remotePath = remotePath;
    }

    public String getExt() {
        return ext;
    }

    public void setExt(String ext) {
        this.ext = ext;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getSize() {
        return size;
    }

    public void setSize(String size) {
        this.size = size;
    }

    /**
     * 用于判断是否和fastdfs中文件相同,用于删除(临时使用)
     */
    public boolean isEqualFdfs(FastDfsInfo fastDfsInfo) {
        if (null == this.fdfsGroup || null == this.fdfsPath || null == fastDfsInfo || null == fastDfsInfo.getGroup()
                || null == fastDfsInfo.getPath()) {
            return false;
        }
        if (this.fdfsGroup.equals(fastDfsInfo.getGroup()) && this.fdfsPath.equals(fastDfsInfo.getPath())) {
            return true;
        } else {
            return false;
        }
    }

    public DocumentFormat getType() {
        return null == ext ? null : typeMap.get(ext.toUpperCase());
    }
    
    public boolean getCanShowAsPdf(){
        return null != getType();
    }

    /**
     * 用于判断是否后缀是图片格式(临时使用)
     */
    public boolean getIsImage() {
        if (null == ext) {
            return false;
        }
        if ("JPG".equalsIgnoreCase(ext) || "PNG".equalsIgnoreCase(ext) || "GIF".equalsIgnoreCase(ext)) {
            return true;
        } else {
            return false;
        }
    }

    public boolean getIsPdf() {
        if (null == ext) {
            return false;
        }
        if ("PDF".equalsIgnoreCase(ext)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 组装fastdfs对象,用于删除
     */
    public FastDfsInfo getFastDfsInfo() {
        return new FastDfsInfo(this.fdfsGroup, this.fdfsPath);
    }

    /**
     * 把字节单位的文件转换大小单位
     * @param size
     * @return
     */
    public static String getPrintSize(long size) {
        //如果字节数少于1024,则直接以B为单位,否则先除于1024,后3位因太少无意义
        if (size < 1024) {
            return String.valueOf(size) + "B";
        } else {
            size = size / 1024;
        }
        //如果原字节数除于1024之后,少于1024,则可以直接以KB作为单位
        //因为还没有到达要使用另一个单位的时候
        //接下去以此类推
        if (size < 1024) {
            return String.valueOf(size) + "KB";
        } else {
            size = size / 1024;
        }
        if (size < 1024) {
            //因为如果以MB为单位的话,要保留最后1位小数,
            //因此,把此数乘以100之后再取余
            size = size * 100;
            return String.valueOf((size / 100)) + "."
                    + String.valueOf((size % 100)) + "MB";
        } else {
            //否则如果要以GB为单位的,先除于1024再作同样的处理
            size = size * 100 / 1024;
            return String.valueOf((size / 100)) + "."
                    + String.valueOf((size % 100)) + "GB";
        }
    }

}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值