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

vue上传功能实现 java后台代码的实现

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

vue上传功能实现 java后台代码的实现

具体功能如图:上传按钮

        

vue前台代码:

        

                    
                        上传
                     

vue绑定方法:

handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handleProgress(event, file, fileList){
      this.loading = Loading.service({
        lock: true,
        text: '程序正在升级,请耐心等待;或者完毕后手动刷新',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
    },
    handlePreview(file) {
      console.log(file);
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}?`);
    },
    handleSuccess(res, file, fileList) {
      console.log(res);
      if (res.flag) {
        this.$message.success(res.message);
        this.findUpgradeFileVersion();
      } else {
        this.$message.error(res.message);
      }
    },
    handleError(res, file, fileList) {
          let errorStr = res.toString();
          let errorStr2 = errorStr.replace("Error:","");
          const Error = JSON.parse(errorStr2);
          this.$message.error(Error.error_description);
          this.loading.close()
    },

Java后台代码实现:代码仅供参考,根据具体业务,再进行修改

        

public Result getUploadFile(@RequestBody MultipartFile file)
      throws IOException {
    if (file != null) {
      //文件大小 KB单位
      long size = file.getSize();
      //获取上传文件的原文件名
      String dirPath = System.getProperty("user.dir") + File.separator + "client_upgrade";
      String src_file_name = file.getOriginalFilename();
      //储存文件
      String savePath = dirPath + File.separator + src_file_name;
      boolean saveFile = upgradeUtil.saveFileForClient(file, savePath);
      if (!saveFile) {
        throw new FortException(new FortError(StatusCode.ERROR, "文件存储失败"));
      }
      //声明json序列化对象
//      ObjectMapper mapper = new ObjectMapper();
      //解压客户端升级包,读取新版本号和版本描述
      Map map = upgradeUtil.getClientVerAndDes(savePath, dirPath, src_file_name);
      if (map == null) {
        throw new FortException(new FortError(StatusCode.ERROR, "获取版本信息及其升级文件名失败"));
      }
      //获取当前登录用户名
      //在security中获取认证后的对象
      Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
      //获取当前登录的对象
      User user = (User) authentication.getPrincipal();
      return new Result(true, StatusCode.OK, "上传成功");
    } else {
      return new Result(false, StatusCode.ERROR, "上传失败");
    }
  }

  @RequestMapping("/findUpgradeFileVersion")
  public Result findUpgradeFileVersion() {
    List upgradeFileList = upgradeFileService
        .findUpgradeFile();
    return new Result(true, StatusCode.OK, "成功", upgradeFileList);
  }

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

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

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