因业务需要将上传的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()){
上传word文档,将word转换成html
最新推荐文章于 2024-07-13 02:21:45 发布
这篇博客介绍了一个利用Java技术将Word文档转换为HTML格式的解决方案。内容包括解析Word文件、保持样式和格式的一致性以及转换过程中的关键步骤。
摘要由CSDN通过智能技术生成