Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
近有一個業務是前端要上傳word格式的文稿,然后用戶上傳完之后,可以用瀏覽器直接查看該文稿,并且可以在富文本框直接引用該文稿,所以上傳word文稿之后,后端保存到db的必須是html格式才行,所以涉及到word格式轉html格式。
通過調查,這個word和html的處理,有兩種方案,方案1是前端做這個轉換。方案2是把word文檔上傳給后臺,后臺轉換好之后再返回給前端。至于方案1,看到大家的反饋都說很多問題,所以就沒采用前端轉的方案,最終決定是后端轉化為html格式并返回給前段預覽,待客戶預覽的時候,確認格式沒問題之后,再把html保存到后臺(因為word涉及到的格式太多,比如圖片,visio圖,表格,圖片等等之類的復雜元素,轉html的時候,可能會很多格式問題,所以要有個預覽的過程)。
對于word中普通的文字,問題倒不大,主要是文本之外的元素的處理,比如圖片,視頻,表格等。針對我本次的文章,只處理了圖片,處理的方式是:后臺從word中找出圖片(當然引入的jar包已經帶了獲取word中圖片的功能),上傳到服務器,拿到絕對路徑之后,放入到html里面,這樣,返回給前端的html內容,就可以直接預覽了。
maven引入相關依賴包如下:
<poi-scratchpad.version>3.14</poi-scratchpad.version>
<poi-ooxml.version>3.14</poi-ooxml.version>
<xdocreport.version>1.0.6</xdocreport.version>
<poi-ooxml-schemas.version>3.14</poi-ooxml-schemas.version>
<ooxml-schemas.version>1.3</ooxml-schemas.version>
<jsoup.version>1.11.3</jsoup.version>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>${poi-scratchpad.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${poi-ooxml.version}</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>xdocreport</artifactId>
<version>${xdocreport.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>${poi-ooxml-schemas.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>${ooxml-schemas.version}</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>${jsoup.version}</version>
</dependency>
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文件,右鍵使用壓縮工具打開,就能得到一個這樣的目錄結構:
所以你以為docx是一個完整的文檔,其實它只是一個壓縮文件。
參考:
https://www.cnblogs.com/ct-csu/p/8178932.html
了編寫一個Java爬蟲,你需要了解以下幾個步驟:
下面是一個基本的Java爬蟲代碼示例,它使用Jsoup解析器和URLConnection庫連接到目標網站并提取標題和鏈接信息:
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class SimpleWebCrawler {
public static void main(String[] args) {
String url = "https://www.example.com/";
try {
URLConnection conn = new URL(url).openConnection();
conn.addRequestProperty("User-Agent", "Mozilla/5.0");
Scanner scanner = new Scanner(conn.getInputStream());
String html = scanner.useDelimiter("\\Z").next();
scanner.close();
Document doc = Jsoup.parse(html);
Elements links = doc.select("a[href]");
for (Element link : links) {
System.out.println(link.attr("href") + " - " + link.text());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Jsoup是一款用于解析HTML和XML文檔的Java庫。它提供了類似于jQuery的語法來操作文檔,使得解析和處理文檔變得非常簡單。
以下是Jsoup解析器的一些常用功能:
總之,Jsoup是一款非常實用的HTML和XML解析器,可以幫助Java開發者快速、簡單地解析和處理HTML文檔,使得爬蟲開發變得更加容易。
使用Jsoup解析器需要先將其添加到項目的依賴中。可以通過Maven或者Gradle來添加依賴。
例如,使用Maven添加Jsoup的依賴:
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.14.3</version>
</dependency>
添加依賴之后,就可以在Java代碼中使用Jsoup了。以下是使用Jsoup解析器獲取HTML文檔中所有鏈接的示例代碼:
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class JsoupExample {
public static void main(String[] args) {
String html = "<html><head><title>Jsoup Example</title></head>"
+ "<body><p>Jsoup is a Java library for working with real-world HTML.</p>"
+ "<a href=\"http://example.com\">Example</a></body></html>";
Document doc = Jsoup.parse(html); // 將HTML字符串解析為文檔對象
Elements links = doc.select("a"); // 獲取所有的鏈接元素
for (Element link : links) {
String href = link.attr("href"); // 獲取鏈接的URL地址
String text = link.text(); // 獲取鏈接的文本內容
System.out.println(href + ": " + text);
}
}
}
以上代碼使用Jsoup將HTML字符串解析為文檔對象,然后使用選擇器語法獲取所有的鏈接元素,并輸出它們的URL地址和文本內容。
除此之外,Jsoup還有很多其他的功能,例如修改元素、過濾HTML文檔等等,可以根據具體需求靈活運用。
1.獲取網頁的 Title:
Document doc = Jsoup.connect("http://example.com/").get();
String title = doc.title();
2.獲取指定標簽的文本內容:
Element element = doc.select("div.content").first();
String text = element.text();
3.獲取指定屬性的值:
Element element = doc.select("img").first();
String src = element.attr("src");
4.過濾 HTML 標簽:
String html = "<p>這是一段 <b>加粗</b> 的文本。</p>";
String text = Jsoup.parse(html).text();
5.修改 HTML 內容:
Element element = doc.select("div.content").first();
element.append("<p>這是新增的文本內容。</p>");
6.提取網頁中的鏈接:
Elements links = doc.select("a[href]");
for (Element link : links) {
String href = link.attr("href");
System.out.println(href);
}
7.提取網頁中的圖片:
Elements imgs = doc.select("img[src~=(?i)\\.(png|jpe?g|gif)]");
for (Element img : imgs) {
String src = img.attr("src");
System.out.println(src);
}
這些只是 Jsoup 解析器的常見用法之一。Jsoup 還有更多的功能,如解析 XML、處理表單、處理 Cookie 等,大家可以自己去了解!
有不足之處大家也可以在評論區指出!
近在工作中遇到一個bug,將word轉換成html,轉換成功之后在瀏覽器中打開其中圖片不顯示,使用img標簽,src指定圖片相對地址又是能顯示的,排除圖片問題。
網上能搜索到的demo
打開轉碼之后的html代碼發現,生成的是vml圖片標簽,這個在IE9以后就不支持了,更別說現在的主流瀏覽器了。
生成的html中使用的是vml標簽
將這個跟大佬分析分析,各種文檔一查,咔咔咔大致分析出問題所在。原來jacob使用的是word本身自帶的功能,相當于把word打開另存為html,于是手動將word轉為html試了一下,果然效果與代碼轉換一致,這時候注意到word另存為時有一個web選項,里面有個使用vml渲染圖片默認是選中的,去掉這個選項,再次生成,圖片正常顯示。
到這里基本已經確定了問題的解決思路,另存為時不勾選這個選項,那么問題來了,怎么利用jacob操作另存為時去掉這個選項呢,想去搜搜看jacob相關的文檔,結果不知道是不是因為這個很老了,網上大多數都是demo,根本沒有相關的文檔可看,Github上也是只言片語,根本無從查起。
jacob github 地址:https://github.com/joval/jacob
微軟官網文檔
官方文檔連接:https://docs.microsoft.com/zh-cn/office/vba/api/word.weboptions.relyonvml
微軟官網查詢相關文檔,發現其實是可以關閉的,于是代碼變成這樣
關閉relyOnVml
再次運行程序,這次轉出來的html就能在瀏覽器打開了。
總結,在這次解決問題的過程中,學會了往更深層次去想問題,找對方向,迎難而上。
記錄一下這個問題解決的經驗,也希望能幫到同樣遇到這個問題的人。
*請認真填寫需求信息,我們會在24小時內與您取得聯系。