文件存儲

1、controller層代碼:

 /**
     * 手動發(fā)表評論
     *
     * @param commentInfoVO 評論信息VO對象
     * @return 處理結(jié)果
     */
    @PatchMapping("/")
    public Boolean publishComment(HttpServletRequest request, CommentInfoVO commentInfoVO, MultipartFile[] files) {
        try {
            // 為評論設(shè)置是否曬圖
            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);

            // 更新評論統(tǒng)計信息
            commentAggregateService.refreshCommentAggregate(commentInfoDTO);
            // 通知訂單中心訂單已經(jīng)發(fā)表了評論
            orderFacadeService.informPublishCommentEvent(commentInfoDTO.getOrderInfoId());

            // 通知會員中心用戶已經(jīng)發(fā)表了評論
            membershipFacadeService.informPublishCommentEvent(
                    commentInfoDTO.getUserAccountId(), ShowPictures.YES.equals(showPictures));
        } catch (Exception e) {
            logger.error("發(fā)表評論失敗", e);
            return false;
        }
        return true;
    }

2、service層代碼實現(xiàn)

 /**
     * 保存評論曬圖
     *
     * @param appBasePath   當前應(yīng)用的根路徑
     * @param commentInfoId 評論信息id
     * @param files         評論曬圖
     * @return 處理結(jié)果
     */
    @Override
    public Boolean saveCommentPictures(String appBasePath, Long commentInfoId, MultipartFile[] files) {
        //處理上傳路徑
        if (CommentPictureUploadDirType.RELATIVE.equals(uploadDirType)) {
            uploadDirPath = appBasePath + uploadDirPath;
        }
        //將圖片上傳到指定目錄去
        try {
            // 如果上傳目錄不存在,則自動創(chuàng)建該目錄
            File uploadDir = new File(uploadDirPath);
            if (!uploadDir.exists()) {
                uploadDir.mkdir();
            }
            for (MultipartFile file : files) {
                if (file == null) {
                    continue;
                }
                // 如果目標文件路徑已經(jīng)存在,則刪除目標文件
                String targetFilePath = uploadDirPath + file.getOriginalFilename();
                File targetFile = new File(targetFilePath);
                if (targetFile.exists()) {
                    targetFile.delete();
                }
                // 將上傳上來的文件保存到指定的文件中去
                file.transferTo(targetFile);
                String format = dateTimeFormatter.format(LocalDateTime.now());
                // 將評論曬圖信息保存到數(shù)據(jù)庫中去
                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/
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容