分享

snippets/438843 | CODE

 gearss 2016-04-15
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<String, String> 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("<base ") == -1) {
content = content.replaceFirst("<head>", "<head><base href=\"" + url+"\" />");
}
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<String, String> getParamters(final Socket socket) throws Exception {
Map<String, String> map = new HashMap<String, String>();
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();
}
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多