|
| 1 | +package com.chen.api.util.regex; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | + |
| 5 | +import javax.imageio.ImageIO; |
| 6 | +import java.awt.image.BufferedImage; |
| 7 | +import java.io.BufferedReader; |
| 8 | +import java.io.FileReader; |
| 9 | +import java.io.IOException; |
| 10 | +import java.io.InputStream; |
| 11 | +import java.net.URL; |
| 12 | +import java.util.regex.Matcher; |
| 13 | +import java.util.regex.Pattern; |
| 14 | + |
| 15 | +/** |
| 16 | + * Description: |
| 17 | + * |
| 18 | + * @author chenweijie |
| 19 | + */ |
| 20 | +public class RegexTest2 { |
| 21 | + |
| 22 | + /** |
| 23 | + * 匹配图片地址中的src中的内容 |
| 24 | + */ |
| 25 | + private static Pattern SRC_PATTERN = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)"); |
| 26 | + |
| 27 | + /** |
| 28 | + * 图片中url包含的?特殊字符 |
| 29 | + */ |
| 30 | + private static String QUESTION_MARK = "?"; |
| 31 | + |
| 32 | + |
| 33 | + @Test |
| 34 | + public void test1() { |
| 35 | + |
| 36 | + Pattern pattern =Pattern.compile("//(//d{3}//)//s//d{3}-//d{4}"); |
| 37 | + try { |
| 38 | + try (BufferedReader in = new BufferedReader(new FileReader("\testPatter.txt"))) { |
| 39 | + String s ; |
| 40 | + while ((s=in.readLine())!=null){ |
| 41 | + |
| 42 | + Matcher matcher =pattern.matcher(s); |
| 43 | + if (matcher.find()){ |
| 44 | + System.out.println(matcher.group()); |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + } catch (IOException e) { |
| 49 | + e.printStackTrace(); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + |
| 54 | + /** |
| 55 | + * 解析html数据 匹配上的返回处理后的数据,匹配不上的返回 null |
| 56 | + * |
| 57 | + * @param text html数据 |
| 58 | + * @param id 主键id |
| 59 | + * @return 处理完的html数据 |
| 60 | + */ |
| 61 | + private String resolveHtml(String text, Integer id) { |
| 62 | + |
| 63 | + String img; |
| 64 | + Pattern pImage; |
| 65 | + Matcher mImage; |
| 66 | + //图片链接地址 |
| 67 | + String regExImg = "<img.*src\\s*=\\s*(.*?)[^>]*?>"; |
| 68 | + pImage = Pattern.compile(regExImg, Pattern.CASE_INSENSITIVE); |
| 69 | + mImage = pImage.matcher(text); |
| 70 | + while (mImage.find()) { |
| 71 | + // 得到<img />数据 |
| 72 | + img = mImage.group(); |
| 73 | + // 匹配<img>中的src数据 |
| 74 | + Matcher m = SRC_PATTERN.matcher(img); |
| 75 | + while (m.find()) { |
| 76 | + //取出图片的地址 |
| 77 | + String imgUlr = m.group(1); |
| 78 | + //处理图片地址加上长宽参数 |
| 79 | + String newUrl = replaceImg(imgUlr, id); |
| 80 | + //替换原内容中的标签 |
| 81 | + if (newUrl != null) { |
| 82 | + text = text.replace(imgUlr, newUrl); |
| 83 | + } else { |
| 84 | + return null; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return text; |
| 89 | + } |
| 90 | + return null; |
| 91 | + } |
| 92 | + |
| 93 | + |
| 94 | + /** |
| 95 | + * 处理某一张图片的格式 |
| 96 | + * |
| 97 | + * @param imageUrl 原图片的url |
| 98 | + * @param id 图片对应的id |
| 99 | + */ |
| 100 | + private String replaceImg(String imageUrl, Integer id) { |
| 101 | + |
| 102 | + BufferedImage image = getBufferedImage(id, imageUrl); |
| 103 | + String mewImgURL = null; |
| 104 | + if (image != null) { |
| 105 | + int height = image.getHeight(); |
| 106 | + int width = image.getWidth(); |
| 107 | + |
| 108 | + if (imageUrl.contains(QUESTION_MARK)) { |
| 109 | + mewImgURL = imageUrl + "&width=" + width + "&height=" + height; |
| 110 | + } else { |
| 111 | + mewImgURL = imageUrl + "?width=" + width + "&height=" + height; |
| 112 | + } |
| 113 | + } |
| 114 | + return mewImgURL; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * 根据imgUrl获取BufferedImage对象 |
| 119 | + * |
| 120 | + * @param id id |
| 121 | + * @param imgUrl 图片地址 |
| 122 | + * @return BufferedImage对象 |
| 123 | + */ |
| 124 | + private static BufferedImage getBufferedImage(Integer id, String imgUrl) { |
| 125 | + URL url; |
| 126 | + InputStream is = null; |
| 127 | + BufferedImage img = null; |
| 128 | + try { |
| 129 | + url = new URL(imgUrl); |
| 130 | + is = url.openStream(); |
| 131 | + img = ImageIO.read(is); |
| 132 | + } catch (IOException e) { |
| 133 | + System.out.println("handleAnalysisPicture failed"); |
| 134 | + } finally { |
| 135 | + try { |
| 136 | + if (is != null) { |
| 137 | + is.close(); |
| 138 | + } |
| 139 | + } catch (IOException e) { |
| 140 | + e.printStackTrace(); |
| 141 | + } |
| 142 | + } |
| 143 | + return img; |
| 144 | + } |
| 145 | + |
| 146 | + |
| 147 | +} |
0 commit comments