博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
突发灵感,看到某网站的搞笑图片挺多,做了一个小java,扫描抠了一些
阅读量:4124 次
发布时间:2019-05-25

本文共 2446 字,大约阅读时间需要 8 分钟。

突发灵感,看到某网站的搞笑图片挺多,做了一个小java,扫描抠了一些

这里分享一下

/**	 * 取得文件的后缀名	 * @Description: TODO	 * @param countStr	 * @return	 */	private static String getFileExt(String fileStr){		return  fileStr.substring(fileStr.lastIndexOf(".") + 1).toLowerCase();	}

得到文件后缀,保存用

/**	 * 取得不同的页面地址	 * @Description: TODO	 * @param countStr	 * @return	 */	private static String getPagePath(String countStr){		String pagPath = "http://xxx.xxxx.xxxx.xxx/thread-2:count-1-1.html";		return pagPath.replaceAll(":count", countStr);	}

取得页面地址

/**	 * 取得图片的url地址	 * @Description: TODO	 * @param contextStr	 * @return	 */	private static String getImgSrc(String contextStr){		if(contextStr.indexOf("\" οnlοad=\"thumbImg(this)\"")<0){			return null;		}		String bigStr = contextStr.substring(contextStr.indexOf("\" οnlοad=\"thumbImg(this)\"")-74,contextStr.indexOf("\" οnlοad=\"thumbImg(this)\""));		String imgStr = bigStr.substring(bigStr.indexOf("

解析页面代码,将图片的url地址取出

/**     * 下载图片     * @param f 保存的文件     * @param imgUrl 图片地址     */    public static void downloadFile(File f, String imgUrl) {            byte[] buffer = new byte[8 * 1024];            URL u;            URLConnection connection = null;            try {                    u = new URL(imgUrl);                    connection = u.openConnection();            } catch (Exception e) {                    System.out.println("ERR:" + imgUrl);                    return;            }            InputStream is = null;            FileOutputStream fos = null;            try {                    f.createNewFile();                    is = connection.getInputStream();                    fos = new FileOutputStream(f);                    int len = 0;                    while ((len = is.read(buffer)) != -1) {                            fos.write(buffer, 0, len);                    }            } catch (Exception e) {            	e.printStackTrace();                f.delete();            } finally {                    if (fos != null) {                            try {                                    fos.close();                            } catch (IOException e) {                            }                    }                    if (is != null) {                            try {                                    is.close();                            } catch (IOException e) {                            }                    }            }            buffer = null;            // System.gc();    }

将url图片下载到本地

完整代码下载:

转载地址:http://zelpi.baihongyu.com/

你可能感兴趣的文章
百度产品经理群面
查看>>
去哪儿一面+平安科技二面+hr面+贝贝一面+二面产品面经
查看>>
element ui 弹窗在IE11中关闭时闪现问题修复
查看>>
vue 遍历对象并动态绑定在下拉列表中
查看>>
Vue动态生成el-checkbox点击无法选中的解决方法
查看>>
python __future__
查看>>
MySQL Tricks1
查看>>
python 变量作用域问题(经典坑)
查看>>
pytorch
查看>>
pytorch(三)
查看>>
pytorch(5)
查看>>
ubuntu相关
查看>>
C++ 调用json
查看>>
nano中设置脚本开机自启动
查看>>
动态库调动态库
查看>>
Kubernetes集群搭建之CNI-Flanneld部署篇
查看>>
k8s web终端连接工具
查看>>
手绘VS码绘(一):静态图绘制(码绘使用P5.js)
查看>>
手绘VS码绘(二):动态图绘制(码绘使用Processing)
查看>>
基于P5.js的“绘画系统”
查看>>