文件存储

简介: 文件存储

1、controller层代码:


/**
     * 手动发表评论
     *
     * @param commentInfoVO 评论信息VO对象
     * @return 处理结果
     */
    @PatchMapping("/")
    public Boolean publishComment(HttpServletRequest request, CommentInfoVO commentInfoVO, MultipartFile[] files) {
        try {
            // 为评论设置是否晒图
            Integer showPictures = ShowPictures.NO;
            if (files != null && files.length > 0) {
                for (MultipartFile file : files) {
                    if (file != null) {
                        showPictures = ShowPictures.YES;
                        break;
                    }
                }
            }
            commentInfoVO.setShowPictures(showPictures);
            //保存评论信息
            CommentInfoDTO commentInfoDTO = commentInfoVO.clone(CommentInfoDTO.class);
            commentInfoService.saveManualPublishedCommentInfo(commentInfoDTO);
            // 上传评论晒图图片
            String appBasePath = request.getSession().getServletContext().getRealPath("/");
            commentPictureService.saveCommentPictures(appBasePath, commentInfoDTO.getId(), files);
            // 更新评论统计信息
            commentAggregateService.refreshCommentAggregate(commentInfoDTO);
            // 通知订单中心订单已经发表了评论
            orderFacadeService.informPublishCommentEvent(commentInfoDTO.getOrderInfoId());
            // 通知会员中心用户已经发表了评论
            membershipFacadeService.informPublishCommentEvent(
                    commentInfoDTO.getUserAccountId(), ShowPictures.YES.equals(showPictures));
        } catch (Exception e) {
            logger.error("发表评论失败", e);
            return false;
        }
        return true;
    }


2、service层代码实现


/**
     * 保存评论晒图
     *
     * @param appBasePath   当前应用的根路径
     * @param commentInfoId 评论信息id
     * @param files         评论晒图
     * @return 处理结果
     */
    @Override
    public Boolean saveCommentPictures(String appBasePath, Long commentInfoId, MultipartFile[] files) {
        //处理上传路径
        if (CommentPictureUploadDirType.RELATIVE.equals(uploadDirType)) {
            uploadDirPath = appBasePath + uploadDirPath;
        }
        //将图片上传到指定目录去
        try {
            // 如果上传目录不存在,则自动创建该目录
            File uploadDir = new File(uploadDirPath);
            if (!uploadDir.exists()) {
                uploadDir.mkdir();
            }
            for (MultipartFile file : files) {
                if (file == null) {
                    continue;
                }
                // 如果目标文件路径已经存在,则删除目标文件
                String targetFilePath = uploadDirPath + file.getOriginalFilename();
                File targetFile = new File(targetFilePath);
                if (targetFile.exists()) {
                    targetFile.delete();
                }
                // 将上传上来的文件保存到指定的文件中去
                file.transferTo(targetFile);
                String format = dateTimeFormatter.format(LocalDateTime.now());
                // 将评论晒图信息保存到数据库中去
                CommentPictureDO commentPictureDO = new CommentPictureDO();
                commentPictureDO.setCommentPicturePath(targetFilePath);
                commentPictureDO.setCommentInfoId(commentInfoId);
                commentPictureDO.setGmtCreate(format);
                commentPictureDO.setGmtModified(format);
                commentPictureDAO.saveCommentPicture(commentPictureDO);
            }
        } catch (Exception e) {
            logger.error("保存图片失败", e);
            return false;
        }
        return true;
    }


3、配置文件


comment.picture.upload.dir.type=relative
comment.picture.upload.dir=/upload/



相关文章
|
3月前
|
存储 文件存储 对象存储
块存储、文件存储和对象存储特点对比
块存储、文件存储和对象存储特点对比
252 2
|
2月前
|
文件存储 块存储 对象存储
对象存储、文件存储与块存储:了解基本差异
【6月更文挑战第3天】了解数据存储的“家”:对象存储如同杂物间,适合大量非结构化数据;文件存储像文件柜,便于管理结构化数据;块存储是积木箱,提供高性能、低延迟的存储空间。通过Python代码示例展示了三者使用场景。选择合适存储方式,让数据找到舒适“家”!
109 4
|
3月前
|
存储 文件存储 数据库
对象存储、块存储、文件存储他们都有什么不通的作用?
对象存储、块存储、文件存储他们都有什么不通的作用?
233 2
|
存储 数据挖掘 数据管理
文件存储可以用于以下场景:
文件存储可以用于以下场景:
297 1
|
存储 安全 网络协议
文件存储NAS
文件存储NAS
111 0
|
存储 人工智能 Cloud Native
《阿里云存储手册》——文件存储NAS
《阿里云存储手册》——文件存储NAS
230 1
|
XML 存储 Java
数据存储之文件存储
数据存储之文件存储
114 0
|
存储 固态存储 大数据
「存储架构」块存储、文件存储和对象存储(第1节)
「存储架构」块存储、文件存储和对象存储(第1节)
|
存储 人工智能 缓存
《阿里云存储手册》——文件存储CPFS
《阿里云存储手册》——文件存储CPFS
378 0
|
存储 数据采集 Kubernetes
文件存储CPFS最佳实践-轻舟智航
通过与阿里云建立合作,轻舟智航可以更加专注地进行业务场景研发。轻舟智航通过使用阿里云CPFS和OSS数据流动解决方案,打造一体化的自动驾驶数据底座,并且依托CPFS的高性能和高吞吐,很好地支持轻舟智航业务的快速增长。
768 2
文件存储CPFS最佳实践-轻舟智航