栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Ant Design Vue <a-upload>上传文件

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Ant Design Vue <a-upload>上传文件

使用SpringBoot+Vue+Antd 编写上传文件功能

前端:使用Vue组件库



 后端:使用SpringBoot 

Controller层使用file接收

所以前端请求的时候也要用file

 public ResponseEntity createModelTrain(@RequestParam("qdtzCode") String qdtzCode, @RequestParam("file") MultipartFile file) throws IOException {
        InputStream inputStream = file.getInputStream();
        ModelTrainingAnnex modelTrainingAnnex = StorageUtil.touch(inputStream, qdtzCode, file.getOriginalFilename(), false);

        return null;
    }
 public static ModelTrainingAnnex touch(InputStream source, String qdtzCode, String targetPath, Boolean ignoreSuffix) {
        // 文件流是否存在
        if (source == null) {
            log.error("文件流为 null !");
            throw new RuntimeException("文件流为 null !");
        }
        // 处理文件路径
        targetPath = targetPathHandle(targetPath);
        if (targetPath == null) {
            log.error("未拼接文件名");
            throw new RuntimeException("未拼接文件名");
        }
        // 文件名
        String name = FileNameUtil.getName(targetPath);
        String suffix = FileNameUtil.getSuffix(targetPath);
        if (!name.contains(".") && !ignoreSuffix) {
            log.error("文件无后缀名");
            throw new RuntimeException("文件无后缀名");
        }
        // 保存文件的绝对路径
        String path = StrUtil.format("{}{}{}.{}", GLOBAL_ROOT_PATH, qdtzCode + "\", targetPath.replace(name, String.valueOf(IdUtil.getSnowflakeNextId())), suffix);

        // 保存文件
        OutputStream target = FileUtil.getOutputStream(FileUtil.touch(path));
        Long fileSize = 0L;
        try {
            fileSize = NioUtil.copyByNIO(source, target, NioUtil.DEFAULT_MIDDLE_BUFFER_SIZE, null) / 1024;
        } catch (Exception e) {
            log.error("保存文件异常:", e);
        } finally {
            IoUtil.close(target);
        }

        // 最大排序号
        ModelTrainingAnnex maxSeq = modelTrainingAnnexRepository.findTopByOrderBySequenceDesc();
        // 当前登录用户
        Optional userName = SecurityUtils.getCurrentUserName();

        // 保存到数据库
        ModelTrainingAnnex ModelTrainingAnnex = null;
        try {
            ModelTrainingAnnex = new ModelTrainingAnnex();
            ModelTrainingAnnex.setId(IdUtil.simpleUUID().toUpperCase(Locale.ROOT));
            ModelTrainingAnnex.setName(name);
            ModelTrainingAnnex.setFileType(suffix);
            ModelTrainingAnnex.setFilePath(path);
            ModelTrainingAnnex.setFileSize(fileSize);
            ModelTrainingAnnex.setIsInternetUrl(false);
            ModelTrainingAnnex.setInternetiUrlPrefix(null);
            ModelTrainingAnnex.setUploadTime(DateUtil.toInstant(new Date()));
            ModelTrainingAnnex.setCreator(userName.orElse(null));
            ModelTrainingAnnex.setCreatedTime(DateUtil.toInstant(new Date()));
            ModelTrainingAnnex.setModifiedTime(DateUtil.toInstant(new Date()));
            ModelTrainingAnnex.setSequence(maxSeq == null ? 0 : maxSeq.getSequence() + 1);
            ModelTrainingAnnex.setQdtzCode(qdtzCode);
            if (suffix.equals("jsonl")) {
                ModelTrainingAnnex.setModelState("0");
            } else if (suffix.equals("xls") || suffix.equals("xlsx")) {
                ModelTrainingAnnex.setModelState("3");
            }
            modelTrainingAnnexRepository.save(ModelTrainingAnnex);
        } catch (Exception e) {
            ModelTrainingAnnex = null;
            // 删除文件
            rm(path);
            log.error("保存到数据库异常,删除文件", e);
        }
        return ModelTrainingAnnex;
    }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/967153.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号