欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

上传文件

程序员文章站 2024-03-14 14:51:22
...

利用springboot框架里面的spring-core产品id里面已有的jar进行上传文件。

 @RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
    public ResultBean uploadFile(MultipartFile multipart){
        if(multipart == null || multipart.isEmpty()){
            return new ResultBean().error("500", "文件为空");
        }
        Pattern pattern = Pattern.compile(virFileNameRegex);
        Matcher matcher = pattern.matcher(multipart.getOriginalFilename());
        if(!matcher.matches()){
            return new ResultBean().error("500", "文件命名有误");
        }

        //获取当前用户、所在部门、归属分行
        UserInfo user = getUser();
        Department dept = departmentService.getById(user.getOrgId());
        Department cityBranch = departmentService.getCityBranchIdByPrior(dept.getId());

        //保存文件
        String dirPath = uploadTempPath + File.separator + temDir + File.separator + dept.getNum();
        File dir = new File(dirPath);
        if(!dir.exists()){
            dir.mkdirs();
        }
        File file = new File(dirPath + File.separator + multipart.getOriginalFilename());
        if(file.exists()){
            file.delete();
        }
        try {
            FileCopyUtils.copy(multipart.getInputStream(), new FileOutputStream(file));
        }catch (Exception e){}
    }