Java实现Fast DFS、服务器、OSS上传

这篇具有很好参考价值的文章主要介绍了Java实现Fast DFS、服务器、OSS上传。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

支持Fast DFS、服务器、OSS等上传方式

介绍

在实际的业务中,可以根据客户的需求设置不同的文件上传需求,支持普通服务器上传+分布式上传(Fast DFS)+云服务上传OSS(OSS)


软件架构

为了方便演示使用,本项目使用的是前后端不分离的架构

前端:Jquery.uploadFile

后端:SpringBoot

前期准备:FastDFS、OSS(华为)、服务器

实现逻辑

通过 application 配置对上传文件进行一个自定义配置,从而部署在不同客户环境可以自定义选择方式。

优点:

  1. 一键切换;
  2. 支持当前主流方式;

缺点:

  1. 迁移数据难度增加:因为表示FileID在对象存储和服务器上传都是生成的UUID,而FastDFS是返回存取ID,当需要迁移的时候,通过脚本可以快速将FastDFS的数据迁移上云,因为存储ID可以共用。但是对象存储和服务器上传的UUID无法被FastDFS使用,增加迁移成本

核心代码

package com.example.file.util;

import com.example.file.common.ResultBean;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.proto.storage.DownloadByteArray;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.obs.services.ObsClient;
import com.obs.services.exception.ObsException;
import com.obs.services.model.DeleteObjectRequest;
import com.obs.services.model.GetObjectRequest;
import com.obs.services.model.ObsObject;
import com.obs.services.model.PutObjectResult;
import io.micrometer.common.util.StringUtils;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.Objects;
import java.util.UUID;

@Slf4j
public class FileUtil {

    /**
     *
     * @param file 文件
     * @param uploadFlag 标识
     * @param uploadPath 上传路径
     * @param endPoint 域名
     * @param ak    ak
     * @param sk sk
     * @param bucketName 桶名字
     * @return fileId 用于下载
     */
    public static ResultBean uploadFile(MultipartFile file, String uploadFlag, String uploadPath,
                                        FastFileStorageClient fastFileStorageClient,
                                        String endPoint, String ak, String sk,
                                        String bucketName) {

        if (StringUtils.isBlank(uploadFlag)){
            ResultBean.error("uploadFlag is null");
        }
        switch (uploadFlag){
            case "fastDFS":
                return uploadFileByFastDFS(file,fastFileStorageClient);
            case "huaweiOOS":
                return uploadFileByHuaweiObject(file, endPoint, ak, ak, bucketName);
            case "server":
            default:
                return uploadFileByOrigin(file,uploadPath);
        }}

    /**
     * 上传文件fastDFS
     * @param file 文件名
     * @return
     */
    private static ResultBean uploadFileByFastDFS(MultipartFile file,FastFileStorageClient fastFileStorageClient){
        Long size=file.getSize();
        String fileName=file.getOriginalFilename();
        String extName=fileName.substring(fileName.lastIndexOf(".")+1);
        InputStream inputStream=null;
        try {
            inputStream=file.getInputStream();
            //1-上传的文件流 2-文件的大小 3-文件的后缀 4-可以不管他
            StorePath storePath=fastFileStorageClient.uploadFile(inputStream,size,extName,null);
            log.info("[uploadFileByFastDFS][FullPath]"+storePath.getFullPath());
            return ResultBean.success(storePath.getPath());
        }catch (Exception e){
            log.info("[ERROR][uploadFileByFastDFS]"+e.getMessage());
            return ResultBean.error(e.getMessage());
        }finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 对象存储上传
     * @param file 文件名
     * @return
     */
    private static ResultBean uploadFileByHuaweiObject(MultipartFile file, String huaweiEndPoint,String huaweiobsAk,String huaweiobsSk,
                                                   String bucketName){
        String fileName = file.getOriginalFilename();
        fileName = renameToUUID(fileName);
        InputStream inputStream=null;
        try {
            inputStream=file.getInputStream();
            // 创建ObsClient实例
            ObsClient obsClient = new ObsClient(huaweiobsAk, huaweiobsSk, huaweiEndPoint);
            PutObjectResult result = obsClient.putObject(bucketName, fileName, inputStream);
            obsClient.close();
            return ResultBean.success(fileName);
        }catch (ObsException e){
            log.info("[ERROR][uploadFileByHuaweiObject]"+e.getErrorMessage());
            return ResultBean.error(e.getErrorMessage());
        }catch (Exception e){
            log.info("[ERROR][uploadFileByHuaweiObject]"+e.getMessage());
            return ResultBean.error(e.getMessage());
        }finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 上传文件原本方法
     * @param file 文件名
     * @return
     */
    private static ResultBean uploadFileByOrigin(MultipartFile file,String uploadPath){
        String fileName = file.getOriginalFilename();
        fileName = renameToUUID(fileName);
        File targetFile = new File(uploadPath);
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(uploadPath + fileName);
            out.write(file.getBytes());
        } catch (IOException e) {
            log.info("[ERROR][uploadFileByOrigin]"+e.getMessage());
            return ResultBean.error(e.getMessage());
        }finally {
            try {
                out.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return ResultBean.success(fileName);
    }
    /**
     * 下载
     * @return
     */
    public static byte[] downloadFile(String fileId,String uploadFlag,String uploadPath
            ,FastFileStorageClient fastFileStorageClient, String group,
                                      String endPoint,String ak,String sk,
                                      String bucketName) {
        byte[] result=null;
        switch (uploadFlag){
            case "fastDFS":
                result =downloadFileByFastDFS(fileId,fastFileStorageClient,group);
                break;
            case "huaweiOOS":
                result =downloadFileByHuaweiObject(fileId, endPoint, ak, sk, bucketName);
                break;
            case "server":
            default:
                String path2 = uploadPath + fileId;
                path2 = path2.replace("//", "/");
                result=downloadFileByOrigin(path2);
                break;
        }
        return result;

    }

    /**
     * 下载文件fastDFS
     * @param fileId 文件名
     * @return
     */
    private static byte[] downloadFileByFastDFS(String fileId,FastFileStorageClient fastFileStorageClient,
                                                String group){
        DownloadByteArray callback=new DownloadByteArray();
        byte[] group1s=null;
        try {
            group1s = fastFileStorageClient.downloadFile(group, fileId, callback);
        }catch (Exception e){
            log.info("[ERROR][downloadFileByFastDFS]"+e.getMessage());

        }
        return group1s;
    }

    /**
     * 下载文件对象存储
     * @param fileId 文件名
     * @return
     */
    private static byte[] downloadFileByHuaweiObject(String fileId, String huaweiEndPoint,String huaweiobsAk,String huaweiobsSk,
                                                     String bucketName){
        byte[] bytes =null;
        try {
            // 创建ObsClient实例
            ObsClient obsClient = new ObsClient(huaweiobsAk, huaweiobsSk, huaweiEndPoint);
            // 构造GetObjectRequest请求
            GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, fileId);
            // 执行下载操作
            ObsObject obsObject = obsClient.getObject(getObjectRequest);
            bytes = inputStreamToByteArray(obsObject.getObjectContent());
            // 关闭OBS客户端
            obsClient.close();
            return bytes;
        }catch (ObsException e){
            log.info("[ERROR][downloadFileByHuaweiObject]"+e.getErrorMessage());
        }catch (Exception e) {
            log.info("[ERROR][downloadFileByHuaweiObject]"+e.getMessage());
        }
        return bytes;
    }

    /**
     *
     * @param input
     * @return
     * @throws IOException
     */
    private static byte[] inputStreamToByteArray(InputStream input) throws IOException {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte[] buffer = new byte[4096];
        int n = 0;
        while (-1 != (n = input.read(buffer))) {
            output.write(buffer, 0, n);
        }
        return output.toByteArray();
    }

    /**
     * 下载文件
     * @param fileId 文件名
     * @return
     */
    private static byte[] downloadFileByOrigin(String fileId){
        File file  =new File(fileId);
        InputStream inputStream=null;
        byte[] buff = new byte[1024];
        byte[] result=null;
        BufferedInputStream bis = null;
        ByteArrayOutputStream os = null;
        try {
            os=new ByteArrayOutputStream();
            bis = new BufferedInputStream(new FileInputStream(file));
            int i = bis.read(buff);
            while (i != -1) {
                os.write(buff, 0, buff.length);
                i = bis.read(buff);
            }
            result=os.toByteArray();
            os.flush();
        } catch (Exception e) {
            log.info("[ERROR][downloadFile]"+e.getMessage());
        }finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    /**
     * 删除文件
     * @param fileId 文件ID
     * @return 删除失败返回-1,否则返回0
     */
    public static boolean deleteFile(String fileId,String fastDFSFlag,FastFileStorageClient fastFileStorageClient,
                                     String group,String uploadPath) {
        boolean result=false;
        if (StringUtils.isNotBlank(fastDFSFlag)&&
                fastDFSFlag.trim().equalsIgnoreCase("true")){
            result =deleteFileByFastDFS(fileId,fastFileStorageClient,group);
        }else {
            String path2 = uploadPath + fileId;
            path2 = path2.replace("//", "/");
            result=deleteByOrigin(path2);
        }
        return result;
    }
    private static boolean deleteByOrigin(String fileName) {
        File file = new File(fileName);
        // 如果文件路径所对应的文件存在,并且是一个文件,则直接删除
        if (file.exists() && file.isFile()) {
            if (file.delete()) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    private static boolean deleteFileByFastDFS(String fileId,FastFileStorageClient fastFileStorageClient,
                                               String group) {
        try {
            String groupFieId=group+"/"+fileId;
            StorePath storePath = StorePath.praseFromUrl(groupFieId);
            fastFileStorageClient.deleteFile(storePath.getGroup(), storePath.getPath());
        } catch (Exception e) {
            log.info("[ERROR][deleteFileByFastDFS]"+e.getMessage());
            return false;
        }
        return true;
    }

    /**
     * 生成fileId
     * @param fileName
     * @return
     */
    private static String renameToUUID(String fileName) {
        return UUID.randomUUID() + "." + fileName.substring(fileName.lastIndexOf(".") + 1);
    }

    /**
     * 删除文件对象存储
     * @param fileId 文件名
     * @return
     */
    private static boolean deleteFileByHuaweiObject(String fileId, String huaweiEndPoint,String huaweiobsAk,String huaweiobsSk,
                                                    String bucketName){
        try {
            // 创建ObsClient实例
            ObsClient obsClient = new ObsClient(huaweiobsAk, huaweiobsSk, huaweiEndPoint);
            // 构造GetObjectRequest请求
            DeleteObjectRequest getObjectRequest = new DeleteObjectRequest(bucketName, fileId);

            // 执行删除操作
            obsClient.deleteObject(getObjectRequest);
            // 关闭OBS客户端
            obsClient.close();
        }catch (ObsException e){
            log.info("[ERROR][deleteFileByHuaweiObject]"+e.getErrorMessage());
        }catch (Exception e) {
            log.info("[ERROR][downloadFileByHuaweiObject]"+e.getMessage());
        }
        return true;
    }

    /**
     * 文件数据输出(image)
     * @param fileId
     * @param bytes
     * @param res
     */
    public static void fileDataOut(String fileId, byte[] bytes, HttpServletResponse res){

        String[] prefixArray = fileId.split("\\.");
        String prefix=prefixArray[1];
        File file  =new File(fileId);

        res.reset();
        res.setCharacterEncoding("utf-8");
//        res.setHeader("content-type", "");
        res.addHeader("Content-Length", "" + bytes.length);
        if(prefix.equals("svg"))
            prefix ="svg+xml";
        res.setContentType("image/"+prefix);
        res.setHeader("Accept-Ranges","bytes");
        OutputStream os = null;

        try {
//            os = res.getOutputStream();
//            os.write(bytes);
//            os.flush();
            res.getOutputStream().write(bytes);
        } catch (IOException e) {
            System.out.println("not find img..");
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}

参考资料

假设个人实战使用可以采用docker快速安装,但是未安装过FastDFS建议普通安装部署,了解一下tracker和storage的使用,以及部署搭建集群。

FastDFS普通安装部署:https://www.cnblogs.com/chenliugou/p/15322389.html

FasdDFS docker安装部署:https://blog.csdn.net/weixin_44621343/article/details/117825755?utm_medium=distribute.pc_relevant.none-task-blog-2defaultbaidujs_baidulandingword~default-0-117825755-blog-127896984.235v43pc_blog_bottom_relevance_base6&spm=1001.2101.3001.4242.1&utm_relevant_index=3

OpenFeign和FastDFS的类冲突:https://www.cnblogs.com/chenliugou/p/18113183

Gitee地址:https://gitee.com/chen-liugou/file/new/master?readme=true文章来源地址https://www.toymoban.com/news/detail-844580.html

到了这里,关于Java实现Fast DFS、服务器、OSS上传的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • JAVA实现文件上传到服务器

    java如何实现大文件断点续传、秒传,JAVA实现文件上传到服务器,jsp实现文件上传到服务器,SpringBoot实现文件上传到服务器,SpringMVC实现文件上传到服务器,SpringCloud实现文件上传到服务器,webuploader实现文件上传到服务器,百度webuploader实现文件上传到服务器,JAVA如何将文件

    2024年04月27日
    浏览(39)
  • Linux服务器上传文件到阿里云oss对象存储的两种方法ossutil、curl

    ossutil支持在Windows、Linux、macOS等系统中运行,您可以根据实际环境下载和安装合适的版本。 安装过程中,需要使用解压工具(unzip、7z)解压软件包,请提前安装其中的一个解压工具。 yum -y install unzip Linux系统一键安装 sudo -v ; curl https://gosspublic.alicdn.com/ossutil/install.sh | sudo b

    2024年02月13日
    浏览(51)
  • Java实现文件上传到ftp服务器

    文件传输协议(File Transfer Protocol,FTP)是用于在网络上进行文件传输的一套标准协议,它工作在 OSI 模型的第七层, TCP 模型的第四层, 即应用层, 使用 TCP 传输而不是 UDP, 客户在和服务器建立连接前要经过一个“三次握手”的过程, 保证客户与服务器之间的连接是可靠的

    2024年02月14日
    浏览(45)
  • java连接sftp服务器实现上传下载

    我最初的需求是java读取远程windows服务器的文件。查了一圈,发现将远程服务器作为ftp服务器是最方便快捷的。着手准备,首先要让远程服务器提供ftp服务,再做相关配置,然后通过代码配置远程地址,用户名密码(ftp服务设置)读取文件。 我目前使用的是 freeSSHd.exe,下载后

    2024年02月07日
    浏览(46)
  • Java -- OSS对象存储服务(Object Storage Service,简称 OSS)文件服务器

    一个成熟的技术架构要有一定的分离性, 平台级的产品一般会这么分:应用服务器、数据库服务器、文件服务器。一般文件、数据库、应用服务器,都应该做逻辑和物理的分离。 以前我们想要做文件上传可能要自己去搭建一个专门的服务器,然后将我们的文件上传到这个服

    2024年02月04日
    浏览(47)
  • SpringBoot上传文件到Minio服务器,支持批量上传

    本文主要介绍如何使用SpringBoot上传到minio服务器。 没什么可多说的,公司用什么咱们开发研究什么就完事了。直接分享核心代码。 minio依赖 配置文件 首先是核心的参数,包括服务器minio地址,以及用户名密码,使用的桶名称 controller代码 如果只需要上传文件,只需要Multipa

    2024年02月08日
    浏览(45)
  • Java实现文件上传到服务器本地,并通过url访问

    Java实现文件上传到服务器本地,并通过url访问 有个需求,前端上传文件,需要用开关的方式同时支持上传七牛和服务器本地,方便不同的用户需求合理分配资源。本篇主要介绍文件上传到本地,然后通过url访问。 首先想到的就是可以通过SpringBoot通常访问静态资源的方式,当

    2024年02月03日
    浏览(55)
  • Spring Cloud Feign MultipartFile文件上传踩坑之路(包含前端文件上传请求、后端文件保存到aliyun-oss文件服务器)

    文件上传组件用的是ant-design的a-upload组件,我的界面如下所示: 文件上传请求API: FileUtils.js 需要注意的只有FileUtils.js定义的uploadApi请求函数,其中 URL 为后端请求接口(“/imageConvert/upload”),文件上传方法必须定义为 POST ,在 headers 加入’Content-type’: ‘multipart/form-data’,后端

    2024年02月12日
    浏览(41)
  • 申请阿里云服务器并搭建公网可支持数据上传的HTTP服务器

            拥有一台自己的云服务器可以做很多事情。阿里云服务器毫无疑问是国内最好的。         阿里云服务器可以用于各种互联网应用的搭建和运行,提供稳定、高性能的服务。         阿里云服务器的用途,包括但不限于以下几个方面: 网站托管:可以将网站

    2024年02月16日
    浏览(67)
  • Java实现以form-data形式文件上传到服务器

    Java实现以form-data形式文件上传到服务器

    2024年02月13日
    浏览(48)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包