需求如题.
代码如下
- //filePath 格式为 ""src='文件路径'""
- public void Test(String filePath){
- String filePath = GethtmlImageSrcList(custom.getDescription()).get(0);// 将文件路径通过正则表达式转换为 http://XXX/XX 的形式
- URL url = new URL(filePath);
- HttpURLConnection connection = (HttpURLConnection)url.openConnection();
- connection.setConnectTimeout(30000);
- connection.setReadTimeout(30000);
- connection.connect();
- String fileName = filePath.substring(filePath.lastIndexOf("."));
- String photoUrl =
- OSSClientUtil.uploadFile2OSS(connection.getInputStream(),
- "scene/" + dateTimeSSSFormat.format(new Date()) + fileName);// 框架已存在的文件上传方法, 在此不赘述
- }
- /**
- * 获取 IMG 标签的 SRC 地址
- */
- public static List<String> GetHtmlImageSrcList(String htmlText) {
- List<String> imgSrc = new ArrayList<String>();
- Matcher m = Pattern.compile("src=\"?(.*?)(\"|>|\\s+)").matcher(htmlText);
- while (m.find()) {
- imgSrc.add(m.group(1));
- }
- return imgSrc;
- }
1, 其中
//URL aURL = new URL("http://www.mycompany.com:8080/index.html");
我们创建了一个使用完整 URL 的 URL class, 其中明确指出了使用的协议是 http, 主机名称是 www.mycompany.com, 端口号码为 8080, 文件 / 资源为 index.HTML. 如果组成 URL 的语法发生了错误, 那么构造器就会发出 MalformedURLException.
openConnection 并不需要参数, 并且在操作成功之后, 它会返回一个 URLConnection class 的实例.
后续还有对 URL 内容的读写操作, 可参考 https://www.cnblogs.com/blackiesong/p/6182038.html 中的解释.
2,GetHtmlImageSrcList 方法中用到了 Pattern 和 Matcher 两个类, 这两个都在 java 提供的 java.util.regex 类工具包中.
详细内容参考 http://www.cnblogs.com/ggjucheng/p/3423731.html 中的解释.
3, 正则表达式: 可以用来检查一个串是否含有某种子串, 将匹配的子串替换或者从某个串中取出符合某个条件的子串等.
语法参考 http://www.runoob.com/regexp/regexp-syntax.html.
https://baijiahao.baidu.com/s?id=1588848792548192879&wfr=spider&for=pc
来源: http://www.bubuko.com/infodetail-2921156.html