国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

SpringBoot整合hdfs,實(shí)現(xiàn)文件上傳下載刪除與批量刪除,以及vue前端發(fā)送請(qǐng)求,實(shí)現(xiàn)前后端交互功能;

這篇具有很好參考價(jià)值的文章主要介紹了SpringBoot整合hdfs,實(shí)現(xiàn)文件上傳下載刪除與批量刪除,以及vue前端發(fā)送請(qǐng)求,實(shí)現(xiàn)前后端交互功能;。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

部分工具類代碼參考文章:https://blog.csdn.net/qq_27242695/article/details/119683823

前端實(shí)現(xiàn)效果

springboot整合hdfs,spring boot,hdfs,大數(shù)據(jù),hadoop,混合現(xiàn)實(shí),Powered by 金山文檔

HDFSController

package com.jack.graduation.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jack.graduation.bean.FileInfo;
import com.jack.graduation.common.Constants;
import com.jack.graduation.common.Result;
import com.jack.graduation.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashSet;
import java.util.List;

/**
 * @BelongsProject: graduation
 * @BelongsPackage: com.jack.graduation.controller
 * @Author: jack
 * @CreateTime: 2023-01-05  17:27
 * @Description: TODO 文件上傳接口
 * @Version: jdk1.8
 */

@RestController
@RequestMapping("/file")
public class FileController {
    @Autowired
    private FileService fileService;

    @PostMapping("/uploadToHdfs")
    public Result uploadToHdfs(@RequestParam MultipartFile file) throws Exception {
        String originalFilename = file.getOriginalFilename(); //文件名
        String type = FileUtil.extName(originalFilename);//獲取文件擴(kuò)展名(文件類型后綴名),擴(kuò)展名不帶“.”
        if (!"csv".equals(type)) {
            //throw new ServiceException(Constants.CODE_400, "文件類型必須是csv逗號(hào)分隔文件!");
            return Result.error(Constants.CODE_400, "文件類型必須是csv逗號(hào)分隔文件!");
        }
        //文件大小
        long size = file.getSize();
        // 定義一個(gè)文件唯一的標(biāo)識(shí)碼
        String uuid = IdUtil.fastSimpleUUID();
        //新的文件名
        String newOriginalFilename = uuid + StrUtil.DOT + type;
        String md5 = SecureUtil.md5(file.getInputStream());
        //下載路徑
        String url = "http://localhost:9090/file/" + newOriginalFilename;
        FileInfo fileInfo = new FileInfo(null, originalFilename, md5, uuid, type, size / 1024, url, null, null, null, null);
        //信息寫進(jìn)數(shù)據(jù)庫
        fileService.save(fileInfo);
        //存儲(chǔ)到hdfs
        boolean res = fileService.uploadHdfs(file, newOriginalFilename);
        if (res) {
            return Result.success("文件上傳成功!");
        } else {
            return Result.error(Constants.CODE_500, "服務(wù)器錯(cuò)誤!");
        }
    }

    /**
     * 清洗后的文件,從hdfs下載
     *
     * @param newFileName 文件唯一標(biāo)識(shí)
     * @param isEtl       是否清洗標(biāo)識(shí)
     * @param response    響應(yīng)體
     * @throws IOException exception
     */
    @GetMapping("/{newFileName}/{isEtl}")
    public void downloadFile(@PathVariable String newFileName, @PathVariable Integer isEtl, HttpServletResponse response) {

        ServletOutputStream os = null;
        // 設(shè)置輸出流的格式
        try {
            os = response.getOutputStream();
            response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(newFileName, "UTF-8"));
            response.setContentType("application/octet-stream");
            byte[] resBytes = fileService.downloadHdfsFile(newFileName, isEtl);
            // 讀取文件的字節(jié)流
            os.write(resBytes);
            os.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @DeleteMapping("/deleteFile/{id}")
    public Result deleteFile(@PathVariable Integer id) {
        QueryWrapper<FileInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        FileInfo fileInfo = fileService.getOne(queryWrapper);
        if (fileService.removeHdfsFile(fileInfo) && fileService.removeById(id)) {
            return Result.success("文件刪除成功");
        } else {
            return Result.error(Constants.CODE_500, "hdfs文件刪除失敗");
        }
    }

    //批量刪除數(shù)據(jù)
    @PostMapping("/delFileBatch")
    public Result delUserBatch(@RequestBody List<Integer> ids) {
        QueryWrapper<FileInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", ids);
        List<FileInfo> fileInfoList = fileService.list(queryWrapper);
        HashSet<String> resSet = fileService.removeHdfsFileBatch(fileInfoList);
        if (resSet.isEmpty() && fileService.removeByIds(ids)) {
            return Result.success("批量刪除文件成功");
        } else {
            return Result.error(Constants.CODE_500, resSet.toString());
        }
    }

    //根據(jù)md5查找文件是否存在
    public FileInfo getFileByMd5(String md5) {
        QueryWrapper<FileInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("file_md5", md5);
        FileInfo fileInfo = fileService.getOne(queryWrapper);
        return fileInfo;
    }

    //分頁數(shù)據(jù)
    @RequestMapping("/page")
    public Result getPage(@RequestParam Integer pageNum,
                          @RequestParam Integer pageSize,
                          @RequestParam(defaultValue = "") String fileName,
                          @RequestParam(defaultValue = "") String id,
                          @RequestParam(defaultValue = "") String uuid
    ) {
        List<FileInfo> list = fileService.list();
        IPage<FileInfo> page = new Page<>(pageNum, pageSize);
        QueryWrapper<FileInfo> wrapper = new QueryWrapper<>();

        //根據(jù)username搜索
        if (!"".equals(fileName)) {
            wrapper.eq("file_name", fileName);
        }
        //根據(jù)id搜索
        if (!"".equals(id)) {
            wrapper.and(wra -> wra.eq("id", Integer.valueOf(id)));
        }
        //根據(jù)uuid搜索
        if (!"".equals(uuid)) {
            wrapper.eq("uuid", uuid);
        }
        //倒序排
        wrapper.orderByDesc("id");
        IPage<FileInfo> iPage = fileService.page(page, wrapper);
        return Result.success(iPage);
    }
}

HDFS FileInterface (文件接口)

package com.jack.graduation.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.jack.graduation.bean.FileInfo;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashSet;
import java.util.List;

public interface FileService extends IService<FileInfo> {
    //上傳hdfs方法
    boolean uploadHdfs(MultipartFile file, String fileName);

    boolean removeHdfsFile(FileInfo fileInfo);

    byte[] downloadHdfsFile(String fileUUID, Integer isEtl);

    HashSet<String> removeHdfsFileBatch(List<FileInfo> fileInfoList);
}

HDFS FileImplService (文件接口實(shí)現(xiàn)類)

package com.jack.graduation.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jack.graduation.bean.FileInfo;
import com.jack.graduation.common.Constants;
import com.jack.graduation.config.HdfsConfig;
import com.jack.graduation.exception.ServiceException;
import com.jack.graduation.mapper.FileMapper;
import com.jack.graduation.service.FileService;
import com.jack.graduation.utils.HdfsUtil;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

/**
 * @BelongsProject: graduation
 * @BelongsPackage: com.jack.graduation.service.impl
 * @Author: jack
 * @CreateTime: 2023-01-05  18:48
 * @Description: TODO
 * @Version: jdk1.8
 */
@Service
public class FileServiceImpl extends ServiceImpl<FileMapper, FileInfo> implements FileService {


    @Autowired
    private HdfsUtil hdfsUtil;
    @Autowired
    private HdfsConfig hdfsConfig;

    /**
     * @param file     前端傳過來的文件
     * @param fileName 文件名
     * @return
     */
    @Override
    public boolean uploadHdfs(MultipartFile file, String fileName) {
        boolean res = false;
        try {
            hdfsUtil.createFile(hdfsConfig.getHdfsPath() + fileName, file, fileName);
            res = hdfsUtil.existFile(hdfsConfig.getHdfsPath() + fileName);
            if (res) {
                return true;
            }
        } catch (Exception e) {
            throw new ServiceException(Constants.CODE_500, "hdfs io error!");
        }
        return res;
    }

    @Override
    public boolean removeHdfsFile(FileInfo fileInfo) {
        boolean res = false;
        String filename = fileInfo.getUuid() + StrUtil.DOT + fileInfo.getFileType();

        try {
            //未清洗文件路徑IsEtl==0
            if (fileInfo.getIsEtl() == 0) {
                res = hdfsUtil.deleteFile(hdfsConfig.getHdfsPath() + filename);
            } else {
                res = hdfsUtil.deleteFile(hdfsConfig.getHdfsCleanPath() + filename);
            }
        } catch (Exception e) {
            throw new ServiceException(Constants.CODE_500, "刪除hdfs文件失??!");
        }
        return res;
    }

    @Override
    public byte[] downloadHdfsFile(String newFileName, Integer isEtl) {

        FileSystem fs = null;
        System.out.println("filename:"+newFileName);
        FSDataInputStream fis = null;
        byte[] resBytes;
        try {
            //文件名
            fs = hdfsUtil.getFileSystem();
            if (isEtl == 0) {
                //創(chuàng)建輸入流
                System.out.println("hdfs:"+hdfsConfig.getHdfsPath() + newFileName);
                fis = fs.open(new Path(hdfsConfig.getHdfsPath() + newFileName));
                resBytes = IOUtils.readFullyToByteArray(fis);
            } else {
                //創(chuàng)建輸入流
                fis = fs.open(new Path(hdfsConfig.getHdfsCleanPath() + newFileName));
                resBytes = IOUtils.readFullyToByteArray(fis);
            }
        } catch (Exception e) {
            throw new ServiceException(Constants.CODE_500, "hdfs文件下載失??!");
            //e.printStackTrace();
        } finally {
            IOUtils.closeStream(fis);
            if (fs != null) {
                try {
                    //關(guān)流
                    fs.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
        return resBytes;
    }

    @Override
    public HashSet<String> removeHdfsFileBatch(List<FileInfo> fileInfoList) {
        boolean res = false;
        HashSet<String> resSet = new HashSet<>();
        for (FileInfo fileInfo : fileInfoList) {
            String filename = fileInfo.getUuid() + StrUtil.DOT + fileInfo.getFileType();
            try {
                //未清洗文件路徑IsEtl==0
                if (fileInfo.getIsEtl() == 0) {
                    res = hdfsUtil.deleteFile(hdfsConfig.getHdfsPath() + filename);
                    if (!res) {
                        resSet.add(fileInfo.getFileName() + "刪除失敗!");
                    }
                } else {
                    res = hdfsUtil.deleteFile(hdfsConfig.getHdfsCleanPath() + filename);
                    if (!res) {
                        resSet.add(fileInfo.getFileName() + "刪除失敗!");
                    }
                }
            } catch (Exception e) {
                throw new ServiceException(Constants.CODE_500, resSet.toString());
            }
        }
        return resSet;
    }

}

HDFSConfig(從yaml讀取文件)

package com.jack.graduation.config;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
 * @BelongsProject: graduation
 * @BelongsPackage: com.jack.graduation.config
 * @Author: jack
 * @CreateTime: 2023-01-03  01:38
 * @Description: TODO:hdfs配置類
 * @Version: jdk1.8
 */
@Configuration
@Data
@NoArgsConstructor
@AllArgsConstructor
public class HdfsConfig {
    // hdfs nameNode連接URL
    @Value("${nameNode.url}")
    private String nameNodeUrl;

    // 操作用戶
    @Value("${hdfs.userName}")
    private String hdfsUserName;

    // 操作存儲(chǔ)節(jié)點(diǎn)路徑
    @Value("${hdfs.dataNode}/")
    private String pdfDataNode;

    //hdfs存儲(chǔ)路徑
    @Value("${nameNode.hdfsPath}")
    private String hdfsPath;

    //hdfs清洗存儲(chǔ)路徑
    @Value("${nameNode.hdfsCleanPath}")
    private String hdfsCleanPath;

}

HDFSUTils

package com.jack.graduation.utils;

import com.alibaba.druid.util.StringUtils;
import com.jack.graduation.config.HdfsConfig;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: graduation
 * @BelongsPackage: com.jack.graduation.utils
 * @Author: jack
 * @CreateTime: 2023-01-03  01:40
 * @Description: TODO hdfs工具類
 * @Version: jdk1.8
 */
@Component
public class HdfsUtil {
    public static final Logger logger = LoggerFactory.getLogger(HdfsUtil.class);


    @Autowired
    private HdfsConfig hdfsConfig;

    /**
     * 獲取HDFS配置信息 配置文件優(yōu)先級(jí)
     * Configuration  > resource下的hdfs-site.xml > 服務(wù)器上的 hdfs-default.xml
     *
     * @return
     */
    private Configuration getConfiguration() {
        Configuration configuration = new Configuration();
        configuration.set("dfs.support.append", "true");
        configuration.set("dfs.client.block.write.replace-datanode-on-failure.enable", "true");
        configuration.set("dfs.client.block.write.replace-datanode-on-failure.policy", "NEVER");
        return configuration;
    }


    /**
     * 獲取HDFS文件系統(tǒng)對(duì)象
     *
     * @return
     * @throws Exception
     */
    public FileSystem getFileSystem() throws Exception {
        // 客戶端去操作hdfs時(shí)是有一個(gè)用戶身份的,默認(rèn)情況下hdfs客戶端api會(huì)從jvm中獲取一個(gè)參數(shù)作為自己的用戶身份
        // DHADOOP_USER_NAME=hadoop
        // 也可以在構(gòu)造客戶端fs對(duì)象時(shí),通過參數(shù)傳遞進(jìn)去
        System.out.println(hdfsConfig.getNameNodeUrl());
        System.out.println(hdfsConfig.getPdfDataNode());
        System.out.println(hdfsConfig.getHdfsUserName());

        FileSystem fileSystem = FileSystem.get(
                new URI(hdfsConfig.getNameNodeUrl()),
                getConfiguration(), hdfsConfig.getHdfsUserName());
        return fileSystem;
    }

    /**
     * 在HDFS創(chuàng)建文件夾
     *
     * @param path
     * @return
     * @throws Exception
     */
    public boolean mkdir(String path) throws Exception {
        FileSystem fs = null;
        boolean isOk = false;
        if (StringUtils.isEmpty(path)) {
            return false;
        }
        try {
            if (existFile(path)) {
                logger.error("hdfs file is exists: {}", path);
                return true;
            }
            // 目標(biāo)路徑
            fs = getFileSystem();
            Path srcPath = new Path(path);
            isOk = fs.mkdirs(srcPath);
            logger.error("hdfs mkdir success: {}", path);
        } catch (Exception e) {
            logger.error("hdfs mkdir: {}", e);
        } finally {
            if (fs != null) {
                fs.close();
            }
        }
        return isOk;
    }

    /**
     * 判斷HDFS文件是否存在
     *
     * @param path
     * @return
     * @throws Exception
     */
    public boolean existFile(String path) throws Exception {
        Boolean isExists = false;
        FileSystem fs = null;
        if (StringUtils.isEmpty(path)) {
            return false;
        }
        try {
            fs = getFileSystem();
            Path srcPath = new Path(path);
            isExists = fs.exists(srcPath);
        } catch (Exception e) {
            logger.error("existFile {}", e);
        } finally {
            if (fs != null) {
                fs.close();
            }
        }
        return isExists;
    }

    /**
     * 讀取HDFS目錄信息
     *
     * @param path
     * @return
     * @throws Exception
     */
    public List<Map<String, Object>> readPathInfo(String path) throws Exception {
        try {
            if (StringUtils.isEmpty(path)) {
                return null;
            }
            if (!existFile(path)) {
                return null;
            }
            FileSystem fs = getFileSystem();
            // 目標(biāo)路徑
            Path newPath = new Path(path);
            FileStatus[] statusList = fs.listStatus(newPath);
            List<Map<String, Object>> list = new ArrayList<>();
            if (null != statusList && statusList.length > 0) {
                for (FileStatus fileStatus : statusList) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("filePath", fileStatus.getPath());
                    map.put("fileStatus", fileStatus.toString());
                    list.add(map);
                }
                return list;
            }
        } catch (Exception e) {
            logger.error("hdfs readPathInfo {}", e);
        }
        return null;
    }

    /**
     * HDFS創(chuàng)建文件
     *
     * @param path 上傳的路徑
     * @param file
     * @throws Exception
     */
    public void createFile(String path, MultipartFile file) throws Exception {
        if (StringUtils.isEmpty(path) || null == file.getBytes()) {
            return;
        }
        FileSystem fs = null;
        FSDataOutputStream outputStream = null;
        try {
            fs = getFileSystem();
            String fileName = file.getOriginalFilename();
            // 上傳時(shí)默認(rèn)當(dāng)前目錄,后面自動(dòng)拼接文件的目錄
            Path newPath = new Path(path + "/" + fileName);
            // 打開一個(gè)輸出流
            outputStream = fs.create(newPath);
            outputStream.write(file.getBytes());
            outputStream.flush();
        } catch (Exception e) {
            throw e;
        } finally {
            if (outputStream != null) {
                outputStream.close();
            }

            if (fs != null) {
                fs.close();
            }
        }
    }

    public void createFile(String path, MultipartFile file,String newFilename) throws Exception {
        if (StringUtils.isEmpty(path) || null == file.getBytes()) {
            return;
        }
        FileSystem fs = null;
        FSDataOutputStream outputStream = null;
        try {
            fs = getFileSystem();
            // 上傳時(shí)默認(rèn)當(dāng)前目錄,后面自動(dòng)拼接文件的目錄
            Path newPath = new Path(path);
            // 打開一個(gè)輸出流
            outputStream = fs.create(newPath);
            outputStream.write(file.getBytes());
            outputStream.flush();
        } catch (Exception e) {
            throw e;
        } finally {
            if (outputStream != null) {
                outputStream.close();
            }

            if (fs != null) {
                fs.close();
            }
        }
    }


    /**
     * 直接往輸出流輸出文件
     *
     * @param path 活動(dòng)方式 遠(yuǎn)程文件
     * @param os   輸出流
     * @return
     * @throws Exception
     */
    public void writeOutputStreamFile(OutputStream os, String path) throws Exception {
        if (StringUtils.isEmpty(path)) {
            return;
        }
/*        if (!existFile(path)) {
            // 文件不存在則拋出異常
            throw new Exception(path + " hdfs文件不存在");
        }*/
        FileSystem fs = null;
        FSDataInputStream inputStream = null;
        try {
            // 目標(biāo)路徑
            Path srcPath = new Path(path);
            fs = getFileSystem();
            inputStream = fs.open(srcPath);
            // 防止中文亂碼
            // BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            fileDownload(os, new BufferedInputStream(inputStream));
        } catch (Exception e) {
            throw e;
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
            if (fs != null) {
                fs.close();
            }
        }
    }

    /**
     * 讀取HDFS文件內(nèi)容
     *
     * @param path
     * @return
     * @throws Exception
     */
    public String readFile(String path) throws Exception {
        if (StringUtils.isEmpty(path)) {
            return null;
        }
        if (!existFile(path)) {
            return null;
        }
        FileSystem fs = null;
        FSDataInputStream inputStream = null;
        try {
            // 目標(biāo)路徑
            Path srcPath = new Path(path);
            fs = getFileSystem();
            inputStream = fs.open(srcPath);
            // 防止中文亂碼
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            String lineTxt = "";
            StringBuffer sb = new StringBuffer();
            while ((lineTxt = reader.readLine()) != null) {
                sb.append(lineTxt);
            }
            return sb.toString();
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
            if (fs != null) {
                fs.close();
            }
        }
    }


    /**
     * 讀取HDFS文件列表
     *
     * @param path
     * @return
     * @throws Exception
     */
    public List<Map<String, String>> listFile(String path) throws Exception {
        if (StringUtils.isEmpty(path)) {
            return null;
        }
        if (!existFile(path)) {
            return null;
        }

        FileSystem fs = null;
        try {
            fs = getFileSystem();
            // 目標(biāo)路徑
            Path srcPath = new Path(path);
            // 遞歸找到所有文件
            RemoteIterator<LocatedFileStatus> filesList = fs.listFiles(srcPath, true);
            List<Map<String, String>> returnList = new ArrayList<>();
            while (filesList.hasNext()) {
                LocatedFileStatus next = filesList.next();
                String fileName = next.getPath().getName();
                Path filePath = next.getPath();
                Map<String, String> map = new HashMap<>();
                map.put("fileName", fileName);
                map.put("filePath", filePath.toString());
                returnList.add(map);
            }
            return returnList;
        } catch (Exception e) {
            logger.error("hdfs listFile {}", e);
        } finally {
            if (fs != null) {
                fs.close();

            }
        }
        return null;
    }


    /**
     * HDFS重命名文件
     *
     * @param oldName
     * @param newName
     * @return
     * @throws Exception
     */
    public boolean renameFile(String oldName, String newName) throws Exception {
        if (StringUtils.isEmpty(oldName) || StringUtils.isEmpty(newName)) {
            return false;
        }
        FileSystem fs = null;
        Boolean isOk = false;
        try {
            fs = getFileSystem();
            // 原文件目標(biāo)路徑
            Path oldPath = new Path(oldName);
            // 重命名目標(biāo)路徑
            Path newPath = new Path(newName);
            isOk = fs.rename(oldPath, newPath);

            return isOk;
        } catch (Exception e) {
            logger.error("hdfs renameFile {}", e);
        } finally {
            if (fs != null) {
                fs.close();
            }
        }
        return isOk;
    }


    /**
     * 刪除HDFS文件
     *
     * @param path
     * @return
     * @throws Exception
     */
    public boolean deleteFile(String path) throws Exception {
        if (StringUtils.isEmpty(path)) {
            return false;
        }

        FileSystem fs = null;
        Boolean isOk = false;
        try {
            if (!existFile(path)) {
                return false;
            }
            fs = getFileSystem();
            Path srcPath = new Path(path);
            isOk = fs.deleteOnExit(srcPath);
        } catch (Exception e) {
            logger.error("hdfs deleteFile {}", e);
        } finally {
            if (fs != null) {
                fs.close();
            }
        }
        return isOk;
    }

    /**
     * 上傳HDFS文件
     *
     * @param path       上傳路徑(本服務(wù)器文件全路徑)
     * @param uploadPath 目標(biāo)路徑(全節(jié)點(diǎn)路徑)
     * @throws Exception
     */
    public void uploadFile(String path, String uploadPath) throws Exception {
        if (StringUtils.isEmpty(path) || StringUtils.isEmpty(uploadPath)) {
            return;
        }
        FileSystem fs = null;
        try {
            fs = getFileSystem();
            // 上傳路徑
            Path clientPath = new Path(path);
            // 目標(biāo)路徑
            Path serverPath = new Path(uploadPath);
            // 調(diào)用文件系統(tǒng)的文件復(fù)制方法,第一個(gè)參數(shù)是否刪除本地文件  true為刪除,默認(rèn)為false
            fs.copyFromLocalFile(false, clientPath, serverPath);
        } catch (Exception e) {
            logger.error("hdfs uploadFile {}", e);
        } finally {
            if (fs != null) {
                fs.close();
            }
        }

    }


    /**
     * 下載HDFS文件
     *
     * @param path         hdfs目標(biāo)路徑
     * @param downloadPath 客戶端存放路徑
     * @throws Exception
     */
    public void downloadFile(String path, String downloadPath) throws Exception {
        if (StringUtils.isEmpty(path) || StringUtils.isEmpty(downloadPath)) {
            return;
        }
        FileSystem fs = null;
        try {
            fs = getFileSystem();
            // hdfs目標(biāo)路徑
            Path clientPath = new Path(path);
            // 客戶端存放路徑
            Path serverPath = new Path(downloadPath);
            // 調(diào)用文件系統(tǒng)的文件復(fù)制方法,第一個(gè)參數(shù)是否刪除原文件 true為刪除,默認(rèn)為false
            fs.copyToLocalFile(false, clientPath, serverPath);
        } catch (Exception e) {
            logger.error("hdfs downloadFile {}", e);
        } finally {
            if (fs != null) {
                fs.close();
            }
        }
    }

    /**
     * HDFS文件復(fù)制
     * @param sourcePath
     * @param targetPath
     * @throws Exception
     */
    /*public void copyFile(String sourcePath, String targetPath) throws Exception {
        if (StringUtils.isEmpty(sourcePath) || StringUtils.isEmpty(targetPath)) {
            return;
        }
        FileSystem fs = getFileSystem();
        // 原始文件路徑
        Path oldPath = new Path(sourcePath);
        // 目標(biāo)路徑
        Path newPath = new Path(targetPath);

        FSDataInputStream inputStream = null;
        FSDataOutputStream outputStream = null;
        try {
            inputStream = fs.open(oldPath);
            outputStream = fs.create(newPath);

            IOUtils.copyBytes(inputStream, outputStream, bufferSize, false);
        } finally {
            inputStream.close();
            outputStream.close();
            fs.close();
        }
    }

    *//**
     * 打開HDFS上的文件并返回byte數(shù)組
     * @param path
     * @return
     * @throws Exception
     *//*
    public byte[] openFileToBytes(String path) throws Exception {
        if (StringUtils.isEmpty(path)) {
            return null;
        }
        if (!existFile(path)) {
            return null;
        }
        FileSystem fs = getFileSystem();
        // 目標(biāo)路徑
        Path srcPath = new Path(path);
        try {
            FSDataInputStream inputStream = fs.open(srcPath);
            return IOUtils.readFullyToByteArray(inputStream);
        } finally {
            fs.close();
        }
    }

    *//**
     * 打開HDFS上的文件并返回java對(duì)象
     * @param path
     * @return
     * @throws Exception
     *//*
    public <T extends Object> T openFileToObject(String path, Class<T> clazz) throws Exception {
        if (StringUtils.isEmpty(path)) {
            return null;
        }
        if (!existFile(path)) {
            return null;
        }
        String jsonStr = readFile(path);
        return JsonUtil.fromObject(jsonStr, clazz);
    }

    *//**
     * 獲取某個(gè)文件在HDFS的集群位置
     * @param path
     * @return
     * @throws Exception
     *//*
    public BlockLocation[] getFileBlockLocations(String path) throws Exception {
        if (StringUtils.isEmpty(path)) {
            return null;
        }
        if (!existFile(path)) {
            return null;
        }
        FileSystem fs = getFileSystem();
        // 目標(biāo)路徑
        Path srcPath = new Path(path);
        FileStatus fileStatus = fs.getFileStatus(srcPath);
        return fs.getFileBlockLocations(fileStatus, 0, fileStatus.getLen());
    }
*/

    /**
     * @param os  response輸出流
     * @param bis 輸入流
     */
    private void fileDownload(OutputStream os, BufferedInputStream bis) throws Exception {
        if (bis == null) {
            return;
        }
        try {
            byte[] buff = new byte[1024];
            int i = bis.read(buff);
            while (i != -1) {
                os.write(buff, 0, i);
                os.flush();
                i = bis.read(buff);
            }
        } catch (IOException e) {
            throw e;
        } finally {
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}

前端vue代碼:文章來源地址http://www.zghlxwxcb.cn/news/detail-596170.html

<template>
    <div>
        <div class="searchForm">
            <el-input style="width: 200px" placeholder="請(qǐng)輸入ID" v-model="id"
                      prefix-icon="el-icon-search"></el-input>
            <el-input class="ml-5" style="width: 200px" placeholder="請(qǐng)輸入文件名" v-model="fileName"
                      prefix-icon="el-icon-search"></el-input>
            <el-input class="ml-5" style="width: 200px" placeholder="請(qǐng)輸入uuid" v-model="uuid"
                      prefix-icon="el-icon-search"></el-input>
            <el-button class="ml-5" type="primary" @click="rigthId();getData()">搜索</el-button>
            <el-button class="ml-5" type="warning" @click="reset">重置</el-button>
        </div>

        <el-table :data="tableData" border stripe :header-cell-class-name="headerBg"
                  @selection-change="handleSelectionChange"
                  :header-cell-style="{'text-align':'center'}" :cell-style="{'text-align':'center'}">
            <el-table-column type="selection" width="40"></el-table-column>
            <el-table-column prop="id" label="ID" width="60"></el-table-column>
            <el-table-column prop="fileName" label="文件名" width="70"></el-table-column>
            <el-table-column prop="fileType" label="文件類型" width="70"></el-table-column>
            <el-table-column :formatter="formatIsEtl" prop="isEtl" label="是否清洗" width="70"></el-table-column>
            <el-table-column prop="uploadTime" label="上傳時(shí)間" width="90"></el-table-column>
            <!--            <el-table-column prop="updateTime" label="修改時(shí)間" width="90"></el-table-column>-->
            <el-table-column prop="etlTime" label="清洗時(shí)間" width="90"></el-table-column>
            <el-table-column prop="fileSize" label="大小(kb)" width="70"></el-table-column>
            <el-table-column prop="uuid" label="uuid" width="245"></el-table-column>
            <el-table-column prop="url" label="下載地址" width="440"></el-table-column>
            <el-table-column label="操作" width="220" align="center">
                <template slot-scope="scope">
                    <el-button style="width: 60px;margin-left: 1px;text-align: center" type="success"
                               @click="cleanFile(scope.row)">清洗
                        <i
                                class="el-icon-coin"></i>
                    </el-button>
                    <el-button type="primary" @click="downloadFile(scope.row)"
                               style="width: 60px;margin-left: 1px;text-align: center">下載 <i
                            class="el-icon-caret-bottom"></i></el-button>
                    <el-popconfirm
                            class="ml-5"
                            confirm-button-text='確定'
                            cancel-button-text='我再想想'
                            icon="el-icon-info"
                            icon-color="red"
                            title="您確定刪除嗎?"
                            @confirm="delFile(scope.row.id)">
                        <el-button type="danger" slot="reference"
                                   style="width: 60px;margin-right: 1px;text-align: center">刪除 <i
                                class="el-icon-remove-outline"></i>
                        </el-button>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <div style=" margin: 10px 0">
            <el-upload action="http://" :show-file-list="false"
                       :on-success="uploadToHdfsSuccess" style="display: inline-block;">
                <el-button type="primary" class="ml-5" style="width: 90px;" @click="uploadToHdfs">上傳<i
                        class="el-icon-caret-top"></i>
                </el-button>
            </el-upload>
            <el-popconfirm
                    class="ml-5"
                    confirm-button-text='確定'
                    cancel-button-text='我再想想'
                    icon="el-icon-info"
                    icon-color="red"
                    title="您確定批量刪除這些數(shù)據(jù)嗎?"
                    @confirm="delFileBatch"
            >
                <el-button type="danger" slot="reference" class="ml-5" style="width: 90px;">批量刪除 <i
                        class="el-icon-remove-outline"></i>
                </el-button>
            </el-popconfirm>

        </div>

        <div class="pagination">
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="pageNum"
                    :page-sizes="[9, 18, 27, 36]"
                    :page-size="pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">     <!--分頁插件-->
            </el-pagination>
        </div>
    </div>
</template>

<script>
    export default {
        name: "file",
        data() {
            return {
                tableData: [],
                total: 0,
                pageNum: 1,
                fileName: '',
                pageSize: 9,
                dialogFormVisible: false,
                addfileForm: {},
                uuid: '',
                id: '',
                multipleSelection: [],
                headerBg: 'headerBg'
            }
        },
        created() {
            this.getData()
        },
        methods: {
            rigthId() {
                if (isNaN(this.id)) {
                    this.$message({
                        type: "warning",
                        message: "請(qǐng)輸入正確輸入數(shù)字id!"
                    })
                    this.reset()
                }
            },
            reset() {
                this.id = ''
                this.fileName = ''
                this.uuid = ''
                this.getData()
            },
            getData() {
                this.request.get(
                    "/file/page", {
                        params: {
                            pageNum: this.pageNum,
                            pageSize: this.pageSize,
                            fileName: this.fileName,
                            uuid: this.uuid,
                            id: this.id
                        }
                    }
                ).then(res => {
                    console.log(res.data);
                    this.tableData = res.data.records
                    this.total = res.data.total
                })
            },
            //文件保存
            savefile() {
                this.request.post("/file/savefile", this.addfileForm).then(res => {
                    if (res.data) {
                        this.$message.success("添加成功")
                        this.dialogFormVisible = false
                        this.getData()
                    } else {
                        this.$message.error("添加失敗")
                        this.dialogFormVisible = false
                    }
                })
            },
            //刪除文件
            delFile(id) {
                this.request.delete("/file/deleteFile/" + id).then(res => {
                    if (res.code === "200") {
                        this.$message.success(res.data)
                        this.getData()
                    } else {
                        this.$message.error(res.msg)
                        this.getData()
                    }
                })
            },
            //批量選擇用戶
            handleSelectionChange(val) {
                console.log(val)
                this.multipleSelection = val
            },
            //批量刪除
            delFileBatch() {
                //將ids對(duì)象取出變成單個(gè)id,放到數(shù)組里面
                let ids = this.multipleSelection.map(ids => ids.id);
                //post到服務(wù)器
                this.request.post("/file/delFileBatch", ids).then(res => {
                    if (res.code === "200") {
                        this.$message.success(res.data)
                        this.getData()
                    } else {
                        this.$message.error(res.msg)
                        this.getData()
                    }
                })
            },
            //分頁數(shù)據(jù)請(qǐng)求
            handleSizeChange(pageSize) {
                console.log(pageSize)
                this.pageSize = pageSize
                this.getData()

            },
            handleCurrentChange(pageNum) {
                console.log(pageNum)
                this.pageNum = pageNum
                this.getData()
            },
            //將后端的0&1映射為是和否
            formatIsEtl(row) {
                return row.isEtl === 1 ? "已清洗" : "未清洗";
            },
            //文件上傳成功回傳
            uploadToHdfsSuccess(res) {
                console.log(res);
                if (res.code === '200') {
                    this.getData()
                    this.$message.success("文件上傳成功")
                } else if (res.code >= "200") {
                    this.getData()
                    this.$message.error(this.date())
                } else {
                    this.getData()
                    this.$message.success("文件上傳成功")
                }
            },

            downloadFile(row) {
                    //如果是清洗過的文件,則從hdfs下載
                    window.open(row.url + "/" + row.isEtl)
            }
        }
    }
</script>
<style>
    .headerBg {
        background: #eee !important;
    }

    .searchForm {
        margin: 10px 0;
    }

    .pagination {
        padding: 10px 0;
        width: max-content;
        margin: 0 auto;
        position: fixed;
        bottom: 10px;
        left: 40%;
    }

</style>

到了這里,關(guān)于SpringBoot整合hdfs,實(shí)現(xiàn)文件上傳下載刪除與批量刪除,以及vue前端發(fā)送請(qǐng)求,實(shí)現(xiàn)前后端交互功能;的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • SpringBoot 如何實(shí)現(xiàn)文件上傳和下載

    SpringBoot 如何實(shí)現(xiàn)文件上傳和下載

    當(dāng)今Web應(yīng)用程序通常需要支持文件上傳和下載功能,Spring Boot提供了簡(jiǎn)單且易于使用的方式來實(shí)現(xiàn)這些功能。在本篇文章中,我們將介紹Spring Boot如何實(shí)現(xiàn)文件上傳和下載,同時(shí)提供相應(yīng)的代碼示例。 Spring Boot提供了Multipart文件上傳的支持。Multipart是HTTP協(xié)議中的一種方式,用

    2024年02月15日
    瀏覽(39)
  • SpringBoot+MinIO 實(shí)現(xiàn)文件上傳、讀取、下載、刪除

    一、 MinIO 二、 MinIO安裝和啟動(dòng) 三、 pom.xml 四、 applicatin.properties(配置文件) 五、 編寫Java業(yè)務(wù)類

    2024年02月09日
    瀏覽(43)
  • springboot+微信小程序?qū)崿F(xiàn)文件上傳下載(預(yù)覽)pdf文件

    實(shí)現(xiàn)思路: 選擇文件 wx.chooseMessageFile ,官方文檔: https://developers.weixin.qq.com/miniprogram/d e v/api/media/image/wx.chooseMessageFile.html 上傳文件 `wx,uploadFile` , 官方文檔:https://developers.weixin.qq.com/miniprogram/dev/api/network/upload/wx.uploadFile.html 查看所有上傳的pdf文件,顯示在頁面上 點(diǎn)擊pdf文件

    2024年02月08日
    瀏覽(96)
  • Springboot + MySQL + html 實(shí)現(xiàn)文件的上傳、存儲(chǔ)、下載、刪除

    Springboot + MySQL + html 實(shí)現(xiàn)文件的上傳、存儲(chǔ)、下載、刪除

    實(shí)現(xiàn)步驟及效果呈現(xiàn)如下: 1.創(chuàng)建數(shù)據(jù)庫表: 表名:file_test 存儲(chǔ)后的數(shù)據(jù): 2.創(chuàng)建數(shù)據(jù)庫表對(duì)應(yīng)映射的實(shí)體類: import com.baomidou.mybatisplus.annotation.IdType ; import com.baomidou.mybatisplus.annotation. TableField ; import com.baomidou.mybatisplus.annotation. TableId ; import com.baomidou.mybatisplus.annotation. Tab

    2024年04月29日
    瀏覽(28)
  • SpringBoot實(shí)現(xiàn)文件上傳和下載筆記分享(提供Gitee源碼)

    前言:這邊匯總了一下目前SpringBoot項(xiàng)目當(dāng)中常見文件上傳和下載的功能,一共三種常見的下載方式和一種上傳方式,特此做一個(gè)筆記分享。 目錄 一、pom依賴 二、yml配置文件 三、文件下載

    2024年02月11日
    瀏覽(26)
  • 一張圖帶你學(xué)會(huì)入門級(jí)別的SpringBoot實(shí)現(xiàn)文件上傳、下載功能

    一張圖帶你學(xué)會(huì)入門級(jí)別的SpringBoot實(shí)現(xiàn)文件上傳、下載功能

    ?????作者名稱:DaenCode ??作者簡(jiǎn)介:啥技術(shù)都喜歡搗鼓搗鼓,喜歡分享技術(shù)、經(jīng)驗(yàn)、生活。 ??人生感悟:嘗盡人生百味,方知世間冷暖。 ??所屬專欄:SpringBoot實(shí)戰(zhàn) 標(biāo)題 一文帶你學(xué)會(huì)使用SpringBoot+Avue實(shí)現(xiàn)短信通知功能(含重要文件代碼) 一張思維導(dǎo)圖帶你學(xué)會(huì)Springboot創(chuàng)

    2024年02月12日
    瀏覽(118)
  • 基于SpringBoot實(shí)現(xiàn)文件上傳和下載(詳細(xì)講解And附完整代碼)

    目錄 一、基于SpringBoot實(shí)現(xiàn)文件上傳和下載基于理論 二、詳細(xì)操作步驟 文件上傳步驟: 文件下載步驟: 三、前后端交互原理解釋? 四、小結(jié)? 博主介紹:?專注于前后端領(lǐng)域開發(fā)的優(yōu)質(zhì)創(chuàng)作者、秉著互聯(lián)網(wǎng)精神開源貢獻(xiàn)精神,答疑解惑、堅(jiān)持優(yōu)質(zhì)作品共享。本人是掘金/騰訊

    2024年04月11日
    瀏覽(117)
  • Hadoop——HDFS的Java API操作(文件上傳、下載、刪除等)

    Hadoop——HDFS的Java API操作(文件上傳、下載、刪除等)

    1、創(chuàng)建Maven項(xiàng)目 2、修改pom.xml文件 3、添加四個(gè)配置文件 為避免運(yùn)行的一些錯(cuò)誤,我們將Hadoop的四個(gè)重要配置文件添加到resources中 4、創(chuàng)建測(cè)試文件JavaAPI 5、初始化 因?yàn)閷?duì)文件的操作我們都需要獲取hdfs對(duì)象和關(guān)閉對(duì)象,所以為避免重復(fù)編寫,將兩個(gè)操作對(duì)立成單獨(dú)方法,分別

    2024年02月06日
    瀏覽(105)
  • 使用javaAPI對(duì)HDFS進(jìn)行文件上傳,下載,新建文件及文件夾刪除,遍歷所有文件

    目錄 //通過工具類來操作hdfs ? hdfs dfs -put d:user_info.txt ?/user_info.txt? // 將文件放入到hdfs中 ?2.通過工具類來操作hdfs ? hdfs dfs -get hdfs路徑 ? 本地路經(jīng)? 將文件放入到本地Windows中 3.通過工具類來操作hdfs ? hdfs dfs -mkdir -p ?hdfs路徑 4.通過工具類來操作hdfs ?查看一個(gè)文件是否存在

    2024年02月12日
    瀏覽(23)
  • 【java】java實(shí)現(xiàn)大文件的分片上傳與下載(springboot+vue3)

    【java】java實(shí)現(xiàn)大文件的分片上傳與下載(springboot+vue3)

    源碼: https://gitee.com/gaode-8/big-file-upload 演示視頻 https://www.bilibili.com/video/BV1CA411f7np/?vd_source=1fe29350b37642fa583f709b9ae44b35 對(duì)于超大文件上傳我們可能遇到以下問題 ? 大文件直接上傳,占用過多內(nèi)存,可能導(dǎo)致內(nèi)存溢出甚至系統(tǒng)崩潰 ? 受網(wǎng)絡(luò)環(huán)境影響,可能導(dǎo)致傳輸中斷,只能重

    2024年02月02日
    瀏覽(29)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包