上传word文档,将word转换成html

这篇博客介绍了一个利用Java技术将Word文档转换为HTML格式的解决方案。内容包括解析Word文件、保持样式和格式的一致性以及转换过程中的关键步骤。
摘要由CSDN通过智能技术生成
因业务需要将上传的word文档(包含图片)转换成html在页面展示,所以编写以下代码,红色内容为主要内容,可忽略掉业务代码
/**
 * 新增活动,新闻,通过上传word文件的方式
 * @throws BusinessException
 * @throws IOException
 */
@PostMapping(value = "add/{utype}")
public JsonResult<ContentInfo> add(
        ContentInfo contentInfo,
        @PathVariable(name = "utype") Integer utype,
        MultipartFile file,
        @RequestParam(name = "preview",required = false) String preview //null不预览
) throws BusinessException, IOException {
    JsonResult<ContentInfo> jsonResult = new JsonResult<>();
    ExtUser curUser = usersService.findCurrentUser();
    if (curUser == null) {
        throw new BusinessException(ErrorEnum.AUTH_NOT_LOGIN);
    }
    switch (utype){
        case 1:
            if(contentInfo.getType() == 0){
                throw new BusinessException(ErrorEnum.ERROR_PARAM, "内容类型不能为空!");
            }
            if(null == contentInfo.getCategoryId()){
                throw new BusinessException(ErrorEnum.ERROR_PARAM, "类别不能为空!");
            }
            //活动,新闻,招聘
            if(contentInfo.getType() == 1 || contentInfo.getType() == 2 || contentInfo.getType() == 3){
                if(StringUtils.isEmpty(contentInfo.getTitle())){
                    throw new BusinessException(ErrorEnum.ERROR_PARAM, "标题不能为空!");
                }
                if(null == file && StringUtils.isEmpty(contentInfo.getContent())){
                    throw new BusinessException(ErrorEnum.ERROR_PARAM, "内容不能为空!");
                }else{
                    if(null != file && !file.isEmpty()){
                        FileUpload fileUpload = fileUploadService.upLoad(file);
                        contentInfo.setFileId(fileUpload.getId());
                        String filePath =  FilenameUtils.concat(UtilsFile.getUrl(), file.getOriginalFilename());
                        // 转存文件
                        file.transferTo(new File(filePath));
                        String htmlStr = UtilsWord.toHtml(filePath);
                        if(htmlStr.contains("./word/media")){
                            htmlStr = htmlStr.replaceAll("./word/media",fileConfig.getServerUrl()+"/tempFanyu/word/media");
                        }
                        contentInfo.setContent(htmlStr);
                    }
                }
            }
            //活动,新闻,banner
            if(contentInfo.getType() == 1 || contentInfo.getType() == 2 || contentInfo.getType() == 4){
                if(null == contentInfo.getCoverFileId()){
                    throw new BusinessException(ErrorEnum.ERROR_PARAM, "封面图片不能为空!");
                }
                if(null == contentInfo.getSort()){
         
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值