博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
突发灵感,看到某网站的搞笑图片挺多,做了一个小java,扫描抠了一些
阅读量:4125 次
发布时间: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://pvlpi.baihongyu.com/

你可能感兴趣的文章
RMRK筹集600万美元,用于在Polkadot上建立先进的NFT系统标准
查看>>
JavaSE_day12 集合
查看>>
JavaSE_day14 集合中的Map集合_键值映射关系
查看>>
Day_15JavaSE 异常
查看>>
异常 Java学习Day_15
查看>>
JavaSE_day_03 方法
查看>>
day-03JavaSE_循环
查看>>
Mysql初始化的命令
查看>>
day_21_0817_Mysql
查看>>
day-22 mysql_SQL 结构化查询语言
查看>>
MySQL关键字的些许问题
查看>>
浅谈HTML
查看>>
css基础
查看>>
HTML&CSS进阶
查看>>
Servlet进阶和JSP基础
查看>>
servlet中的cookie和session
查看>>
过滤器及JSP九大隐式对象
查看>>
软件(项目)的分层
查看>>
菜单树
查看>>
Servlet的生命周期
查看>>