Html中圖片url獲取及修改
阿新 • • 發佈:2019-02-12
POM引用:
<dependency> <groupId>org.jsoup</groupId> <artifactId>jsoup</artifactId> <version>1.11.3</version> </dependency> <!-- struts2依賴包 --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId><version>2.3.14</version> </dependency>
首先工具類程式碼:
public class ImageUtil { /** * @param html * @return 獲得圖片列表 */ public static List<String> getImgList(String html) { String regex; List<String> list = new ArrayList<String>(); Document doc = Jsoup.parse(html, "utf-8"); Elements imgs = doc.getElementsByTag("img"); for(Element img :imgs){ String src = img.attr("src"); list.add(src); } return list; } /** * 檢測圖片路徑是否為真實有效的路徑方法 * @param src * @return */ public static boolean checkImage(String src){ //使用正則表示式,排除img標籤src屬性值已經是base64編碼的情況 Pattern pattern = Pattern.compile("^data:image/(png|gif|jpg|jpeg|bmp|tif|psd|ICO);base64,.*"); Matcher matcher = pattern.matcher(src); if(matcher.matches()) return false; //排除src路徑並非圖片格式的情況 pattern=Pattern.compile("^.*[.](png|gif|jpg|jpeg|bmp|tif|psd|ICO)$"); matcher = pattern.matcher(src); if(!matcher.matches()) return false; //排除圖片路徑不存在的情況 String path = ServletActionContext.getServletContext().getRealPath(src.substring(5, src.length())); File file = new File(path); if(!file.exists()) return false; return true; } /** * 將html中圖片路徑轉換為base64編碼路徑 * @param html * @return */ public static String html_ImgToBase64(String html){ Document doc = Jsoup.parse(html, "utf-8"); Elements imgs = doc.getElementsByTag("img"); for(Element img :imgs){ String src = img.attr("src"); if(!checkImage(src)) continue; //將有效的路徑傳入base64編碼的方法 img.attr("src", ImageBase64.imageToBase64Head(src)); } //返回base64編碼後的html文件 return doc.getElementsByTag("body").html(); } /** * 去除html中圖片路徑帶有?---的部分(蘋果手機顯示存在問題) * @param html * @return */ public static String html_ImgToNormalUrl(String html){ Document doc = Jsoup.parse(html, "utf-8"); Elements imgs = doc.getElementsByTag("img"); for(Element img :imgs){ String src = img.attr("src"); if(!checkImage(src)){ String newString = src; if(src.contains("?")){ String[] strings = src.split("\\?wx_fmt="); newString = strings[0]; } //將有效的路徑傳入base64編碼的方法 img.attr("src", newString); } } //返回base64編碼後的html文件 return doc.getElementsByTag("body").html(); } }
base64工具類:
public class ImageBase64 { /** * 將圖片轉換成Base64編碼 ,帶標頭檔案 * @param imgFile 待處理圖片 * @return */ public static String imageToBase64Head(String imgFile){ //將圖片檔案轉化為位元組陣列字串,並對其進行Base64編碼處理 String type = imgFile.substring(imgFile.length()-3,imgFile.length()); //為編碼新增標頭檔案字串 String head = "data:image/"+type+";base64,"; return head + imageToBase64(imgFile); } /** * 將圖片轉換成Base64編碼 * @param imgFile 待處理圖片 * @return */ public static String imageToBase64(String imgFile){ //將圖片檔案轉化為位元組陣列字串,並對其進行Base64編碼處理 //拿到上傳圖片後,圖片所在的專案中真實路徑 String path = ServletActionContext.getServletContext().getRealPath(imgFile.substring(5, imgFile.length())); InputStream in = null; byte[] data = null; try { //讀取圖片位元組陣列 in = new FileInputStream(path); data = new byte[in.available()]; in.read(data); in.close(); } catch (IOException e) { e.printStackTrace(); } return new String(Base64.encodeBase64(data)); } //圖片轉化成base64字串 public static String GetImageStr() {//將圖片檔案轉化為位元組陣列字串,並對其進行Base64編碼處理 String imgFile = "d://test.jpg";//待處理的圖片 InputStream in = null; byte[] data = null; //讀取圖片位元組陣列 try { in = new FileInputStream(imgFile); data = new byte[in.available()]; in.read(data); in.close(); } catch (IOException e) { e.printStackTrace(); } //對位元組陣列Base64編碼 BASE64Encoder encoder = new BASE64Encoder(); return encoder.encode(data);//返回Base64編碼過的位元組陣列字串 } //base64字串轉化成圖片 public static boolean GenerateImage(String imgStr) { //對位元組陣列字串進行Base64解碼並生成圖片 if (imgStr == null) //影象資料為空 return false; BASE64Decoder decoder = new BASE64Decoder(); try { //Base64解碼 byte[] b = decoder.decodeBuffer(imgStr); for(int i=0;i<b.length;++i) { if(b[i]<0) {//調整異常資料 b[i]+=256; } } //生成jpeg圖片 String imgFilePath = "d://222.jpg";//新生成的圖片 OutputStream out = new FileOutputStream(imgFilePath); out.write(b); out.flush(); out.close(); return true; } catch (Exception e) { return false; } } }