浏览器离线保存数据

先找到 网址,然后google 浏览器访问,到fiddler2 里面 just url 复制txt,然后java 读取

--处理代码

/**
	 * 功能: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(".")) {
				// 当前处于html
				System.out.println("html:" + str);
			} else {
				tmp = savepath + tmp;
				// 3ceng
				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) {
						// 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();
		}
	}


-下载代码

	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();
	}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值