离线保存浏览器网页
发布日期:2021-05-14 05:15:24 浏览次数:18 分类:精选文章

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

--���������������������������

1.fiddler ���������Google ���������������������������������

3.java saving ������������

package c;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class test {
public static void main(String argv[]) {
String filePath = "C:\\Users\\Administrator\\Desktop\\feed.txt";
// String filePath = "C:\\Users\\Administrator\\Desktop\\error.txt";
readTxtFile(filePath);
}
/**
* ���������Java������txt���������������
* ���������
* 1������������������������
* 2���������������������������������������������������������
* 3������������������������������
* ���������������������������������
* @param filePath
*/
public static void readTxtFile(String filePath) {
try {
String encoding = "GBK";
File file = new File(filePath);
if (file.isFile() && file.exists()) {
InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
String tmp = "";
while ((lineTxt = bufferedReader.readLine()) != null) {
doline(lineTxt);
}
read.close();
} else {
System.out.println("������������������������");
}
} catch (Exception e) {
System.out.println("������������������������");
e.printStackTrace();
}
}
public static void doline(String str) {
try {
String savepath = "C:\\Users\\Administrator\\Desktop\\";
String home = "http://grouek.com/";
String name = str.replace(home, "");
String tmp = name;
if (!tmp.contains(".")) {
// ������������html������
System.out.println("html:" + str);
} else {
tmp = savepath + tmp;
// ������������������������������������
tmp = tmp.substring(0, tmp.lastIndexOf("/"));
String last = tmp;
String lastone = last;
if (last.contains("/")) {
try {
last = last.substring(0, last.lastIndexOf("/"));
} catch (Exception e) {
// ���������������������������
}
lastone = last;
try {
lastone = lastone.substring(0, lastone.lastIndexOf("/"));
} catch (Exception e) {
// ���������������������������
}
}
// ���������������
if (!new File(lastone).isDirectory()) {
new File(lastone).mkdir();
}
if (!new File(last).isDirectory()) {
new File(last).mkdir();
}
if (!new File(tmp).isDirectory()) {
new File(tmp).mkdir();
}
System.out.println("..."+name);
sendImageFile(home + name, savepath + name);
}
} catch (Exception e) {
System.out.println("test.doline()" + str);
e.printStackTrace();
}
}
public static void deal(String url, String path) {
try {
String tmp = path;
// ������������������������������������
tmp = tmp.substring(0, tmp.lastIndexOf("/"));
String last = tmp;
tmp = tmp.substring(0, tmp.lastIndexOf("/"));
if (!new File(tmp).isDirectory()) {
new File(tmp).mkdir();
}
if (!new File(last).isDirectory()) {
new File(last).mkdir();
}
sendImageFile(url, path);
System.out.print("������������... " + url);
} catch (Exception e) {
System.out.println("���������������" + e.getMessage());
e.printStackTrace();
}
}
private static String sendImageFile(String url, String path) {
try {
// ������������������������������������������������������
URL url2 = new URL(url);
HttpURLConnection httpUrl = (HttpURLConnection) url2.openConnection();
httpUrl.setRequestProperty("Accept", "*/*");
httpUrl.setRequestProperty("Connection", "Keep-Alive");
httpUrl.setRequestProperty("User-Agent", "Mozilla/4.0 (Windows NT 5.1;SV1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.0.0 Safari/537.36");
// ������������������������
httpUrl.setRequestProperty("Content-Type", "image/jpeg");
httpUrl.connect();
InputStream inStream = httpUrl.getInputStream();
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[4 * 1024];
int len = 0;
while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
inStream.close();
writeImageToDisk(outStream.toByteArray(), path);
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
private static void writeImageToDisk(byte[] img, String fileName) {
try {
File file = new File(fileName);
FileOutputStream fops = new FileOutputStream(file);
fops.write(img);
fops.flush();
fops.close();
} catch (Exception e) {
e.printStackTrace();
}
}

上一篇:ANDROID 内存分析
下一篇:浏览器离线保存数据

发表评论

最新留言

表示我来过!
[***.240.166.169]2025年04月19日 04时41分57秒