根据url输出为html

1 根据url获取html文件

public static String getHtmlFile(String fileUrl) {
		System.out.println("--- file url: " + fileUrl);

		Date before = new Date();
		Long start = before.getTime();
		StringBuffer htmlCode = new StringBuffer();
		try {
			InputStream in;
			URL url = new java.net.URL(fileUrl);
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection = (HttpURLConnection) url.openConnection();
			connection.setRequestProperty("User-Agent", "Mozilla/4.0");
			connection.setRequestProperty("Content-type", "text/html");
			connection.setRequestMethod("GET");
			connection.setConnectTimeout(30000);  
			connection.setReadTimeout(30000); 
			connection.connect();
			in = connection.getInputStream();
			BufferedReader breader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
			String currentLine;
			while ((currentLine = breader.readLine()) != null) {
				htmlCode.append(currentLine + "\n");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			Date after = new Date();
			Long end = after.getTime();
			Long elapse = end - start;
			System.out.println("Download HTML file time :" + elapse + " ms");
		}
		return htmlCode.toString();
	}

2 放入到指定的位置

public static synchronized void writeHtml(String filePath, String info, String prefix, String flag) {
		PrintWriter pw = null;
		try {
			File writeFile = new File(filePath);
			boolean isExit = writeFile.exists();
			if (isExit != true) {
				writeFile.createNewFile();
			} else {
				if (!flag.equals("NO")) {
					writeFile.delete();
					writeFile.createNewFile();
				}
			}
			// pw = new PrintWriter(new FileOutputStream(filePath, true));

			OutputStreamWriter outstream = new OutputStreamWriter(new FileOutputStream(filePath), "UTF-8");
			pw = new PrintWriter(outstream);
			String finalBody = processHtmlBody(prefix, info);
			pw.println(finalBody);
			pw.close();
		} catch (Exception ex) {
			System.out.println(ex.getMessage());
		} finally {
			pw.close();
		}
	}

3 根据需要设置输出内容

private static String processHtmlBody(String prefix, String body) {
		// change link path
		Pattern patternImg = Pattern.compile("<img\\s+src\\s*=\\s*\"\\s*/", Pattern.DOTALL);
		Matcher matcher = patternImg.matcher(body);
		String finalBody = matcher.replaceAll("<img src=" + prefix);
		
		Pattern patternScript = Pattern.compile("<script\\s+src\\s*=\\s*\"\\s*/", Pattern.DOTALL);
		matcher = patternScript.matcher(finalBody);
		finalBody = matcher.replaceAll("<script src=" + prefix);
		
		Pattern patternLink = Pattern.compile("<link\\s+href\\s*=\\s*\"\\s*/", Pattern.DOTALL);
		matcher = patternLink.matcher(finalBody);
		finalBody = matcher.replaceAll("<link href=" + prefix);
		return finalBody;
	}

4 测试

public static void main(String[] args) {
		String url = "http://www.baidu.com";
		writeHtml("C:/Users/wang_/Desktop/test/test.html", getHtmlFile(url), null, "NO");
	}

5 小结

一般需要做导出此url的pdf或者图片,先将此url转为html文件,然后再转为pdf或者image。后续会写一个html转pdf或者image的方法

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值