import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class openUrl {
private static ServerSocket server;
private final static int port = 80;
public static void main(String[] args) {
try {
server = new ServerSocket(port);
System.out.println("server port: " + port + " start...");
} catch (Throwable t) {
t.printStackTrace();
return;
}
nextServerThreadStart();
}
private static Thread nextServerThreadStart() {
Thread t = new Thread(new Runnable() {
public void run() {
Socket socket = null;
try {
socket = server.accept();
} catch (Throwable t) {
t.printStackTrace();
} finally {
nextServerThreadStart();
}
if (socket != null) {
// System.out.println(socket.getPort());
Thread t = sendThreadStart(socket);
while (t.isAlive()) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
t.start();
return t;
}
private static Thread sendThreadStart(final Socket socket) {
Thread t = new Thread(new Runnable() {
public void run() {
BufferedReader is = null;
OutputStream os = null;
try {
socket.setSoTimeout(20000);
Map
paramters = getParamters(socket);
String encoding = paramters.get("encoding");
String url = paramters.get("url");
String content = getURLContent(url, encoding);
if (content == null)
return;
if (content.indexOf("
", "
");
}
Writer out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), encoding));
out.write(content);
out.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
/*
* if (out != null){ try { out.close(); } catch (Exception
* e) {} }
*/
/*
* if (is != null) { try { is.close(); } catch (Exception e)
* { } }
*/
if (os != null) {
try {
os.close();
} catch (Exception e) {
}
}
}
}
});
t.start();
return t;
}
private static String gethead(final Socket socket) throws Exception {
InputStream reader = socket.getInputStream();
int available;
ByteArrayOutputStream data = new ByteArrayOutputStream();
while ((available = reader.available()) > 0) {
byte[] b = new byte[available];
reader.read(b);
data.write(b);
}
// System.out.println(data.toString());
return data.toString();
}
private static Map
getParamters(final Socket socket) throws Exception {
Map
map = new HashMap
();
String head = gethead(socket);
if (head.split(" ").length < 2)
return map;
String GETStr = head.split(" ")[1];
String[] split = GETStr.replaceFirst("%\\?", "").split("[&?]+");
for (String str : split) {
int i = str.indexOf("=");
if (i >= 1 && i <= str.length() - 2) {
map.put(str.substring(0, i), str.substring(i + 1));
}
}
return map;
}
public static String getURLContent(String url, String encoding) {
if (url == null || "".equals(url.trim()))
return null;
StringBuffer content = new StringBuffer();
try {
// 新建URL对象
URL u = new URL(url);
InputStream in = new BufferedInputStream(u.openStream());
InputStreamReader theHTML = new InputStreamReader(in, encoding != null ? encoding : "gb2312");
int c;
while ((c = theHTML.read()) != -1) {
content.append((char) c);
}
}
// 处理异常
catch (MalformedURLException e) {
System.err.println(e);
} catch (IOException e) {
System.err.println(e);
}
return content.toString();
}
}
java 获取网页json数据 java获取网页指定内容
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
java抓取网页json数据 java获取网站内容
在网络爬虫这一方面,java并不如Python好用。本文只用正则表达式提取信息,如果想要更精确地从html文件中提取信息,必须使用网页地解析器。可以通过第三方库,比如Jsoup等。
java抓取网页json数据 正则表达式 java .net System