一种用于springboot的文件上传方法

一种用于springboot的文件上传方法

在写springboot的项目中,我们总会涉及到一些文件上传的场景,所以这里有一种文件上传的方法供选择,不多解释,看代码(doge)

    @Autowired
    private MyFileService myFileService;

    @RequestMapping("/upload")
    public JsonResoult<MyFile> uploadFile(@RequestParam("file") MultipartFile file,
                                          @RequestParam("contentid") String contentid,
                                          @RequestParam("contenttype") String contenttype){
        //在保存文件之前对文件做检查
        //判断文件是否为空
        if (file.isEmpty()){
            throw new IFileEmptyException("上传文件为空,上传失败");
        }

        //判断文件是否超过限制
        if (file.getSize() >Integer.valueOf(100000000) ){
            throw new IFileSizeException("文件过大,上传失败");
        }

        //判断上传的文件是否为图片类型 file.getContentType()获取的是这种形式 --> text/html
        if (!file.getContentType().contains(file.getContentType())){
            throw new IFileTypeNotMatchException("文件类型不符,上传失败");
        }

        //获取上传文件的原始名字
        String originalFilename = file.getOriginalFilename();

        //获取文件的后缀名
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));

        //使用时间戳为文件定义新的名字
        String uuidName = Timer.getTimeStamp().toString();

        //定义文件最终的名字
        String fileName = uuidName + suffix;

        //获取项目在服务器上的真实位置并拼凑文件最终的保存位置
        String realPath = System.getProperty("user.dir") + "/src/main/resources/static/upload/" + fileName;

        /**
         * 这里其实有两个选择:①直接将文件写入硬盘 这里选择这种
         * ②先获取存储文件的文件夹的路径,判断存储文件的文件夹是否存在,不存在则创建
         * 最后再将文件名和文件夹路径进行拼凑出最终文件的保存路径
         **/
        //虚拟创建目标文件
        File destFile = new File(realPath);

        //获取目标文件的上级目录
        File parentFile = destFile.getParentFile();

        if (!parentFile.exists()){
            //代表文件的上级目录不存在,进行创建
            parentFile.mkdirs();
        }

        //选择第一种方法,直接写入目标位置
        try {
            file.transferTo(destFile);
        }catch (IFileStatusException e) {
            throw new IFileStatusException("文件状态异常,写入失败");
        }catch (IOException e) {
            throw new IFileUploadIOException("服务器或数据库写入文件失败");
        }



        //将文件的下载路径写入数据库
        String fileAccessPath = "/upload/" + fileName;

        MyFile myFile = new MyFile();
        String id  = Timer.getTimeStamp().toString();

        myFile.setId(id);
        myFile.setStatus("1");
        myFile.setType(suffix.substring(1,suffix.length()));
        myFile.setUrl(fileAccessPath);
        myFile.setContentid(contentid);
        myFile.setContenttype(contenttype);
//        写入数据库
       if(myFileService.upLoadFile(myFile)){
           return new JsonResoult<MyFile>(ServerStatus.SUCCESS,"",myFile);
       }
        return new JsonResoult<MyFile>(ServerStatus.Fail);

    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

肖叶茂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值