springboot上传本地图片到数据库

    @PostMapping("inster")
    public BaseResponse insterCourse(@RequestParam(value = "courseUrl") MultipartFile courseUrl){
        BaseResponse baseResponse = BaseResponse.initialize();
        if (StringUtils.isEmpty(courseUrl)){
            baseResponse.setCode(1);
            baseResponse.setMsg("文件不能为空");
            return baseResponse;
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String date = df.format(new Date());
        String path = "E:/var/uploaded_files/"+date+"/";
        UUID uuid = UUID.randomUUID();
        String originalFilename = courseUrl.getOriginalFilename();
        String extendName = originalFilename.substring(originalFilename.lastIndexOf("."), originalFilename.length());
        String fileName = uuid.toString() + extendName;
        File dir = new File(path, fileName);
        File filepath = new File(path);
        if (!filepath.exists()){
            filepath.mkdirs();
        }
        try {
            courseUrl.transferTo(dir);
            baseResponse.setResult(path+fileName);
        }catch (Exception e){
            System.out.println(e);
        }
        return baseResponse;
    }

你可能感兴趣的:(SpringCloud)