文件上传,保存到本地磁盘,返回前端保存地址路径

 public UploadFileVO uploadFile(UploadFileInVO vo) throws Exception {
        MultipartFile[] files = vo.getFile();
        String serverUrl = vo.getServerUrl();
        Boolean needSave = vo.getNeedSave();
        String recordId = vo.getRecordId();
        String type = vo.getType()==null?"":vo.getType();
        BGoodsInfo target = null;
        List<String> savePathList = new ArrayList<>();
        if(needSave){
            if(StringUtils.isEmpty(recordId)){
                throw new ParamException("当需要持久化的时候,recordId不能为空");
            }
            target = getById(recordId);
            if(target == null){
                throw new BizException("要操作的商品数据不存在");
            }
        }
        switch (type){
            case "1":
            case "2":
                break;
            default:
                throw new ParamException("type不合法");
        }
        UploadFileVO result = new UploadFileVO();
        for(MultipartFile file:files){
            if(file.isEmpty()){
                throw new BizException("文件内容为空,请重新选择文件!");
            }
            String fileExtension = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            String fileName = UUID.randomUUID().toString().replaceAll("-","") + fileExtension;
            Date d = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            String dateNowStr = sdf.format(d);
            String imagePath = sysConfig.getImagePath();
            String centerPath = "goods/"+dateNowStr+"/";
            //检查该路径对应的目录是否存在. 如果不存在则创建目录
            File directory=new File(imagePath+centerPath);
            if (!directory.exists()) {
                directory.mkdirs();
            }
            String savePath = centerPath + fileName;
            //保存文件
            File dest = new File(imagePath+savePath);
            if (!(dest.exists())) {
                file.transferTo(dest);
            }
            savePathList.add(savePath);
        }
        if(needSave){
            switch (type){
                case "1":
                    //确认修改商品图片
                    List<BDataInfo> dataInfos = new ArrayList<>();
                    for(String one:savePathList){
                        BDataInfo entity = new BDataInfo();
                        entity.setDataPath(one);
                        dataInfos.add(entity);
                    }
                    savePicBatch(false,dataInfos,recordId);
                    break;
                case "2":
                    //确认修改二维码图片
                    target.setGBarCodePic(savePathList.get(0));
                    updateById(target);
                    break;
            }
        }
        result.setServerUrl(serverUrl+sysConfig.getImageVisitor());
        result.setPathList(savePathList);
        return result;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值