美文网首页
文件存储

文件存储

作者: c_gentle | 来源:发表于2022-05-05 21:23 被阅读0次

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/

相关文章

  • 数据库安装与配置

    安装目录简介 bin: 存储可执行文件 data: 存储数据文件 include:存储包含的头文件 lib:存储库...

  • iOS数据存储

    iOS开发中数据存储有两类:一、存储为文件 如归档、解归档、plist文件存储及偏好设置都是存储为文件二、存储到...

  • MySQL常用存储引擎之CSV

    存储特点 数据以文本方式存储在文件中 .csv文件存储表内容 .csv文件存储表的元数据如表状态和数据...

  • mysql 高级

    关键文件 日志文件 数据文件 配置文件 存储引擎 mysql使用插件式的存储引擎,MySQL存储引擎有InnoDB...

  • 20170104第一行代码第六章数据存储

    持久化技术:文件存储、SharedPreference存储和数据库存储。 一、文件存储 不对存储内容进行任何格式化...

  • 文件存储

    1)打开文件: 在python中用open()这个函数来打开文件并返回文件对象,open()函数有很多参数,其中第...

  • 文件存储

    一:硬盘的存储结构 磁盘的最小存储单位是扇区(Sector ),大小是512B(0.5K)。 连续的8个扇区组成一...

  • 文件存储

    文件存储是 Android 中最基本的一种数据存储方式。 它不对存储的内容进行任何的格式化处理,所有数据都是原封不...

  • 文件存储

    1.openFileInput和openFileOutput的使用 文件的使用,注意最后要用finally给关闭掉...

  • 文件存储

    将数据储存到文件中 将数据从文件中取出

网友评论

      本文标题:文件存储

      本文链接:https://www.haomeiwen.com/subject/phvoyrtx.html