1. 程式人生 > 其它 >java html 轉word_java後端實現word上傳並轉html格式

java html 轉word_java後端實現word上傳並轉html格式

技術標籤:java html 轉word

最近有一個業務是前端要上傳word格式的文稿,然後使用者上傳完之後,可以用瀏覽器直接檢視該文稿,並且可以在富文字框直接引用該文稿,所以上傳word文稿之後,後端儲存到db的必須是html格式才行,所以涉及到word格式轉html格式。

通過調查,這個word和html的處理,有兩種方案,方案1是前端做這個轉換。方案2是把word文件上傳給後臺,後臺轉換好之後再返回給前端。至於方案1,看到大家的反饋都說很多問題,所以就沒采用前端轉的方案,最終決定是後端轉化為html格式並返回給前段預覽,待客戶預覽的時候,確認格式沒問題之後,再把html儲存到後臺(因為word涉及到的格式太多,比如圖片,visio圖,表格,圖片等等之類的複雜元素,轉html的時候,可能會很多格式問題,所以要有個預覽的過程)。

對於word中普通的文字,問題倒不大,主要是文字之外的元素的處理,比如圖片,視訊,表格等。針對我本次的文章,只處理了圖片,處理的方式是:後臺從word中找出圖片(當然引入的jar包已經帶了獲取word中圖片的功能),上傳到伺服器,拿到絕對路徑之後,放入到html裡面,這樣,返回給前端的html內容,就可以直接預覽了。

maven引入相關依賴包如下:

 3.143.141.0.63.141.31.11.3
org.apache.poi            poi-scratchpad            ${poi-scratchpad.version}org.apache.poi            poi-ooxml            ${poi-ooxml.version}fr.opensagres.xdocreport            xdocreport            ${xdocreport.version}org.apache.poi            poi-ooxml-schemas            ${poi-ooxml-schemas.version}org.apache.poi            ooxml-schemas            ${ooxml-schemas.version}org.jsoup            jsoup            ${jsoup.version}

word轉html,對於word2003和word2007轉換方式不一樣,因為word2003和word2007的格式不一樣,工具類如下:

使用方法如下:

public String uploadSourceNews(MultipartFile file)  {        String fileName = file.getOriginalFilename();        String suffixName = fileName.substring(fileName.lastIndexOf("."));        if (!".doc".equals(suffixName) && !".docx".equals(suffixName)) {            throw new UploadFileFormatException();        }        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMM");        String dateDir = formatter.format(LocalDate.now());        String directory = imageDir + "/" + dateDir + "/";        String content = null;        try {            InputStream inputStream = file.getInputStream();            if ("doc".equals(suffixName)) {                content = wordToHtmlUtil.Word2003ToHtml(inputStream, imageBucket, directory, Constants.HTTPS_PREFIX + imageVisitHost);            } else {                content = wordToHtmlUtil.Word2007ToHtml(inputStream, imageBucket, directory, Constants.HTTPS_PREFIX + imageVisitHost);            }        } catch (Exception ex) {            logger.error("word to html exception, detail:", ex);            return null;        }        return content;    }

關於doc和docx的一些儲存格式介紹:

docx 是微軟開發的基於 xml 的文書處理檔案。docx 檔案與 doc 檔案不同, 因為 docx 檔案將資料儲存在單獨的壓縮檔案和資料夾中。早期版本的 microsoft office (早於 office 2007) 不支援 docx 檔案, 因為 docx 是基於 xml 的, 早期版本將 doc 檔案另存為單個二進位制檔案。

DOCX is an XML based word processing file developed by Microsoft. DOCX files are different than DOC files as DOCX files store data in separate compressed files and folders. Earlier versions of Microsoft Office (earlier than Office 2007) do not support DOCX files because DOCX is XML based where the earlier versions save DOC file as a single binary file.

可能你會問了,明明是docx結尾的文件,怎麼成了xml格式了?

很簡單:你隨便選擇一個docx檔案,右鍵使用壓縮工具開啟,就能得到一個這樣的目錄結構:

ec8347b792efb7cb1aa5c761b4b9e68e.png

所以你以為docx是一個完整的文件,其實它只是一個壓縮檔案。

參考:

https://www.cnblogs.com/ct-csu/p/8178932.html