离线保存浏览器网页
--離線保存瀏覽器網頁
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:讀取到輸入流后,需要讀取生成字節流 4:一行一行的輸出。readline()。 備注:需要考慮的是異常情況* * @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(".")) {// 當前處于htmlSystem.out.println("html:" + str);} else {tmp = savepath + tmp;// 3cengtmp = tmp.substring(0, tmp.lastIndexOf("/"));String last = tmp;String lastone = last;if (last.contains("/")) {try {last = last.substring(0, last.lastIndexOf("/"));} catch (Exception e) {// System.out.println("-----------");}lastone = last;try {lastone = lastone.substring(0, lastone.lastIndexOf("/"));} catch (Exception e) {// System.out.println("-----------");}}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) {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("finsh.." + url);}private static String sendImageFile(String url, String path) {String subUrl = "";// System.out.println("test.sendImageFile()"+url+"\t"+path);try {URL url2 = new URL(url);// Log.i("http:send", "http send"+url);HttpURLConnection httpUrl = (HttpURLConnection) url2.openConnection();// 設置通用的請求屬性httpUrl.setRequestProperty("accept", "*/*");httpUrl.setRequestProperty("connection", "Keep-Alive");httpUrl.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1;SV1)");// httpUrl.setRequestProperty("Content-type", "application/text");httpUrl.setRequestProperty("Content-Type", "image/*");// 連接指定的網絡資源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();}// System.out.println();return subUrl;}/*** 將圖片寫入到磁盤* * @param img* 圖片數據流* @param fileName* 文件保存時的名稱*/public static void writeImageToDisk(byte[] img, String fileName)throws Exception {File file = new File("" + fileName);FileOutputStream fops = new FileOutputStream(file);fops.write(img);fops.flush();fops.close();} }總結
- 上一篇: C字符操作函数大全
- 下一篇: smartadmin mysql_Sma