本文主要是介绍突发灵感,看到某网站的搞笑图片挺多,做了一个小java,扫描抠了一些,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
突发灵感,看到某网站的搞笑图片挺多,做了一个小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("<img src=\"")+10);
return imgStr;
}
解析页面代码,将图片的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://download.csdn.net/detail/songylwq/4738238
这篇关于突发灵感,看到某网站的搞笑图片挺多,做了一个小java,扫描抠了一些的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!