分片上传文件的实现

分片上传的实现也比较简单,

主要实现就是对分片的判断,

然后对分片上传,

上传完成之后对分片进行合并,删除分片信息,只保留原始文件。


基本实现

本地文件配置路径实体---base_localfilepathconfig

private String ipAddress;
private String filePath;
private String monitorPath;
private String zipPath;
private String unZipPath;
private int consumerThreadNum;
private String backUpPath;
private String failZipPath;
private int state;

Service核心代码

/**gn
* @param file   文件
* @param chunks 分片总数
* @param chunk  当前分片
* @param name   文件名
* @param md5    文件md5值
* @return
* @Description: 手动上传
* @date 2022/8/8 11:42
* @author Qifan
*/
public String uploadFile(MultipartFile file, String chunks, String chunk, String name, String md5) {
    
    //数据库获取实体
    List localPathInfoList = localFilePathService.findLocalPathInfo();
    //准备返回结果
    Map resultMap;
    String returnJsonStr;
    
    
    if (!CollectionUtils.isEmpty(localPathInfoList) && !StringUtils.isBlank(localPathInfoList.get(0).getZipPath())) {
        //准备对象
        LocalFilePathEntity filePathEntity = localPathInfoList.get(0);
        String packFilePath = filePathEntity.getZipPath();
        //文件名
        String str = name.substring(0, name.lastIndexOf("."));
        //创建目录
        FileCommonUtils.createFileDirectory(packFilePath);
        
        //对分片进行判断,存在走分片
        if (chunks != null && chunk != null) {
            //创建分片路径--->.../文件名/md5/
            File dir = new File(packFilePath + str + "/" + md5);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            try {
                byte[] bytes = file.getBytes();
                Path path = Paths.get(packFilePath + str + "/" + md5 + "/" + chunk);
                //写入分片下路径
                Files.write(path, bytes, new OpenOption[0]);
                //合并分片另外写了接口,等多个分片上传完成之后,再进行合并接口,并保存入库
                log.info("文件分片上传成功!");
                resultMap = this.getReturnMap(name, "文件上传成功");
                returnJsonStr = DataUtils.success(resultMap);
            } catch (IOException var15) {
                log.error("文件分片失败 失败原因" + var15.getMessage());
                resultMap = this.getReturnMap(name, "分片失败,失败原因:" + var15.getMessage());
                returnJsonStr = DataUtils.error(resultMap);
            }
        } else {//分片为null,不存在,一般文件
            try {
                byte[] bytes = file.getBytes();
                Path path = Paths.get(packFilePath + name);
                System.err.println("----------------");
                Files.write(path, bytes, new OpenOption[0]);
                
                //一.保存上传记录表
                //UploadFileHisEntity uploadHis = ftpGetFileService.saveUploadHisByFileName(0, name);
                //if (uploadHis.getState().equals(Lcu3Constants.FileUploadState.FAIL.getState())) {
                //    resultMap = this.getReturnMap(name, uploadHis.getErrorMsg());
                //    returnJsonStr = DataUtils.error(resultMap);
                //    return returnJsonStr;
                //}
                
                //二.解析文件并入库
                //dataAnalysisService.analysisByFileName(name, uploadHis, filePathEntity);
                
                log.info("文件上传成功!");
                resultMap = this.getReturnMap(name, "文件上传成功");
                returnJsonStr = DataUtils.success(resultMap);
            } catch (IOException var14) {
                log.error("文件上传失败 失败原因" + var14.getMessage());
                resultMap = this.getReturnMap(name, "上传失败,失败原因:" + var14.getMessage());
                returnJsonStr = DataUtils.error(resultMap);
            }
        }
        
        return returnJsonStr;
    } else {//没有配置路径,不允许上传
        resultMap = this.getReturnMap(name, "本机配置没有配置上传压缩包文件路径");
        returnJsonStr = DataUtils.error(resultMap);
        return returnJsonStr;
    }
    }

通用返回值

private Map getReturnMap(String fileName, String msg) {
    Map mapResult = new HashMap(2);
    mapResult.put("fileName", fileName);
    mapResult.put("msg", msg);
    return mapResult;
    }

文件合并处理和数据解析

/**
     * @param data 包括 fileName:文件名 fileMd5:文件MD5值
     * @return
     * @Description: 文件合并处理和数据解析
     * @date 2022/8/8 17:37
     * @author Qifan
     */
    public String fileHandler(String data) {

        //准备数据
        List localPathInfoList = localFilePathService.findLocalPathInfo();
        LocalFilePathEntity filePathEntity = localPathInfoList.get(0);
        String packFilePath = localPathInfoList.get(0).getZipPath();
        Map map = (Map) JSON.parseObject(data, Map.class);
        String fileName = map.get("fileName").toString();
        String fileMd5 = map.get("fileMd5").toString();
        String name = fileName.substring(0, fileName.lastIndexOf("."));
        File dir = new File(packFilePath + name + "/" + fileMd5);

        //结果集
        String returnJsonStr = DataUtils.success(null);
        Map resultMap;
        if (dir.exists() && dir.isDirectory()) {    //确认目录
            List fileList = this.fileSort(dir);
            //合并分片
            String mergeStr = this.mergeFile(fileList, fileName, packFilePath);
            if (StringUtils.isNotEmpty(mergeStr)) {
                resultMap = this.getReturnMap(fileName, "文件合并异常,异常原因:" + mergeStr);
                returnJsonStr = DataUtils.error(resultMap);
                return returnJsonStr;
            }
        }
        //合并完成,删除目录
        try {
            if (dir.exists() && dir.isDirectory())  FileUtils.deleteDirectory(dir.getParentFile());
        } catch (IOException e) {
            log.error("删除分片文件路径异常" + e.getMessage());
        }

        //一.保存上传记录表
        //UploadFileHisEntity uploadHis = ftpGetFileService.saveUploadHisByFileName(Lcu3Constants.FileUploadType.MANUAL.getType(), fileName);
        //if (uploadHis.getState().equals(Lcu3Constants.FileUploadState.FAIL.getState())) {
        //    resultMap = this.getReturnMap(fileName, "文件合并异常,异常原因:" + uploadHis.getErrorMsg());
        //    returnJsonStr = DataUtils.error(resultMap);
        //    return returnJsonStr;
        //}
        
        //二.解析文件并入库
        //dataAnalysisService.analysisByFileName(fileName, uploadHis, filePathEntity);
        return returnJsonStr;
    }

合并

 /**
     * @param fileList     文件列表
     * @param fileName     文件名
     * @param packFilePath 包文件路径
     * @return 异常信息
     * @Description: 合并分片文件
     * @date 2022/8/8 17:40
     * @author Qifan
     */
    private String mergeFile(List fileList, String fileName, String packFilePath) {
        FileChannel inChannel = null;
        FileChannel outChannel = null;
        File fileMergePath = new File(packFilePath + fileName);

        String exceptionInfo;
        try {
            if (!fileMergePath.exists()) {
                fileMergePath.createNewFile();
            }
            outChannel = (new FileOutputStream(fileMergePath)).getChannel();
            Iterator iterator = fileList.iterator();
            while (iterator.hasNext()) {
                File file = (File) iterator.next();
                inChannel = (new FileInputStream(file)).getChannel();
                inChannel.transferTo(0L, inChannel.size(), outChannel);
                inChannel.close();
            }
            return null;
        } catch (IOException e) {
            log.error("文件合并异常" + e.getMessage());
            exceptionInfo = e.getMessage();
            return exceptionInfo;
        } finally {
            if (inChannel != null) {
                IOUtils.closeQuietly(inChannel);
            }
            if (outChannel != null) {
                IOUtils.closeQuietly(outChannel);
            }
        }

    }

文件上传之前的必要校验

 /**
     * @param
     * @return
     * @Description: 判断文件是否上传
     * @date 2022/8/8 17:26
     * @author Qifan
     */
    public String fileRulesVerify(String fileName, long fileSize) {
        //文件类型校验
        String result = this.fileTypeFormatVerify(fileName);
        if (StringUtils.isNotEmpty(result)) {
            return result;
        } else {
            //文件大小校验
            result = this.fileSizeLimit(fileSize);
            if (StringUtils.isNotEmpty(result)) {
                return result;
            } else {
                //查一下文件是否存在
                boolean isRepeat = this.findByFileName(fileName);
                return !isRepeat ? "文件重复上传" : result;
            }
        }
    }

    //必要校验
    public String fileTypeFormatVerify(String fileName) {return null;}
    public String fileSizeLimit(long fileSize){return null;}
    public boolean findByFileName(String fileName) {return false;}

你可能感兴趣的:(web开发,servlet,java,数据库)