锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import com.tianhe.frm.http.server.Constant;
import com.tianhe.frm.utils.SocketUtil;
public class SimpleHttpClientSocket implements Runnable
{
private ResUrlBean resUrlBean;
private String resName;
private String resUrl;
public void setResUrlBean(ResUrlBean resUrlBean)
{
this.resUrlBean = resUrlBean;
}
public void setResName(String resName)
{
this.resName = resName;
}
public void setResUrl(String resUrl)
{
this.resUrl = resUrl;
}
public SimpleHttpClientSocket(ResUrlBean resUrlBean, String resUrl, String resName)
{
this.resUrlBean = resUrlBean;
this.resName = resName;
this.resUrl = resUrl;
}
public void run()
{
long beginTime = System.currentTimeMillis();
String vsTemp;
Socket socket = null;
InputStream is = null;
OutputStream os = null;
BufferedReader in = null;
int resSize = 0;
try
{
socket = createSocket(resUrlBean.getHost(), resUrlBean.getPort());
is = socket.getInputStream();
os = socket.getOutputStream();
resUrl = "
// ////////////////////////////////////////////////////////////////////////
// 閸欐垿?鐠囬攱鐪?br /> StringBuffer sb = new StringBuffer();
appendLn(sb, "GET " + resUrl + " HTTP/1.1");
appendLn(sb,
"Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
appendLn(sb, "Accept-Language: zh-cn");
appendLn(sb, "UA-CPU: x86");
appendLn(sb, "Accept-Encoding: gzip, deflate");
appendLn(sb,
"Operator-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 1.1.4322)");
appendLn(sb, "Host: " + resUrlBean.getHost());
appendLn(sb, "Connection: Keep-Alive");
appendLn(sb);
os.write(sb.toString().getBytes());
os.flush();
// ////////////////////////////////////////////////////////////////////////
// 閹恒儲鏁歸崫宥呯安
in = new BufferedReader(new InputStreamReader(is));
int contentLength = 0;
String line = in.readLine();
while ((line = in.readLine()) != null)
{
if (line.startsWith(Constant.CONTENT_LENGTH))
{
vsTemp = line.substring(Constant.CONTENT_LENGTH.length());
contentLength = Integer.valueOf(vsTemp);
}
System.out.println(line);
if (line.equals(""))
break;
}
// 閸愭瑥鍙嗛弬鍥︽
if (contentLength > 0)
{
File file = resUrlBean.getTargetFile(resName);
if (!file.exists())
{
file.createNewFile();
}
int len = -1;
byte[] buff = new byte[8192];
FileOutputStream fos = new FileOutputStream(file);
while ((len = is.read(buff)) != -1)
{
fos.write(buff, 0, len);
resSize = resSize + len;
}
fos.flush();
fos.close();
os.close();
in.close();
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
SocketUtil.close(is);
SocketUtil.close(os);
SocketUtil.close(socket);
}
long used = System.currentTimeMillis() - beginTime;
System.out.println(resUrl + " succeed, used=" + used + ", resSize=" + resSize);
}
private StringBuffer appendLn(StringBuffer sb, String xxx)
{
sb.append(xxx).append("\r\n");
return sb;
}
private StringBuffer appendLn(StringBuffer sb)
{
sb.append("\r\n");
return sb;
}
private static String getAllUrl(String[] args)
{
String temp = " if (args != null && args.length > 0)
{
temp = args[0];
}
return temp;
}
private static String getTargetDir(String[] args)
{
String temp = "d:/wallpaper";
if (args != null && args.length > 1)
{
temp = args[1];
}
return temp;
}
private static String getProxyHost(String[] args)
{
String temp = "";
if (args != null && args.length > 2)
{
temp = args[2];
}
return temp;
}
private static int getProxyPort(String[] args)
{
int temp = 80;
if (args != null && args.length > 3)
{
temp = Integer.valueOf(args[3]);
}
return temp;
}
private static ResUrlBean createResUrlBean(String proxyHost, int proxyPort, String allUrl, String targetDir)
{
ResUrlBean rub = null;
if (proxyHost != null && (!proxyHost.trim().equals("")))
{
// for url use
rub = new ResUrlBean(true);
rub.setHost(proxyHost);
rub.setPort(proxyPort);
}
else
{
// for socket use, not for url use
rub = new ResUrlBean();
rub.setHost(getHostFromUrl(allUrl));
rub.setPort(getPortFromUrl(allUrl));
}
rub.setAllUrl(allUrl);
rub.setTargetDir(targetDir);
{
int pos1 = -1;
int pos2 = -1;
int pos3 = -1;
String ts = "";
pos1 = allUrl.indexOf("://") + 3;
pos2 = allUrl.indexOf("/", pos1) + 1;
pos3 = allUrl.lastIndexOf("/");
ts = allUrl.substring(pos2, pos3);
rub.setRelativePath(ts);
}
rub.setBaseUrl(allUrl.substring(0, allUrl.lastIndexOf("/") + 1));
rub.setSuffix(allUrl.substring(allUrl.length() - 4));
rub.setStartIndex(1);
{
String ts = allUrl.substring(allUrl.lastIndexOf("/") + 1, allUrl.length() - 4);
int pos = 0;
char[] tc = ts.toCharArray();
for (int i = tc.length - 1; i > 0; i--)
{
int ti = Integer.valueOf(tc[i]);
if (ti < 48 || ti > 57)
{
pos = i + 1;
break;
}
}
rub.setPrefix(ts.substring(0, pos));
rub.setEndIndex(Integer.valueOf(ts.substring(pos)));
rub.setFormat("%0" + ts.substring(pos).length() + "d");
}
System.out.println("resUrlBean=" + rub.toString());
return rub;
}
private static String getHostFromUrl(String allUrl)
{
// allUrl = " // allUrl = "
int pos1 = -1;
int pos2 = -1;
String ts = "";
pos1 = allUrl.indexOf("://") + 3;
pos2 = allUrl.indexOf("/", pos1);
ts = allUrl.substring(pos1, pos2);
pos1 = ts.indexOf(":");
if (pos1 > 0)
{
ts = ts.substring(0, pos1);
}
// System.out.println("getHostFromUrl:" + ts);
return ts;
}
private static int getPortFromUrl(String allUrl)
{
// allUrl = " // allUrl = "
int port = 80;
int pos1 = -1;
int pos2 = -1;
String ts = "";
pos1 = allUrl.indexOf("://") + 3;
pos2 = allUrl.indexOf("/", pos1);
ts = allUrl.substring(pos1, pos2);
pos1 = ts.indexOf(":");
if (pos1 > 0)
{
ts = ts.substring(pos1) + 1;
port = Integer.valueOf(ts);
}
// System.out.println("getPortFromUrl:" + port);
return port;
}
private static String createResName(ResUrlBean rub, int i)
{
StringBuffer sb = new StringBuffer();
sb.append(rub.getPrefix());
String format = rub.getFormat();
if (format != null && (!format.trim().equals("")))
{
sb.append(String.format(format, i));
}
sb.append(rub.getSuffix());
return sb.toString();
}
private static SimpleHttpClientSocket createSimpleHttpClient(ResUrlBean rub, int i)
{
String resName = createResName(rub, i);
String resUrl = rub.getBaseUrl() + resName;
SimpleHttpClientSocket client = new SimpleHttpClientSocket(rub, resUrl, resName);
return client;
}
// http://bizhi.zhuoku.com/2012/04/11/shanshui/Shanshui01.jpg
public static void main(String[] args)
{
String allUrl = getAllUrl(args);
System.out.println("allUrl=" + allUrl);
String targetDir = getTargetDir(args);
System.out.println("targetDir=" + targetDir);
String proxyHost = getProxyHost(args);
System.out.println("proxyHost=" + proxyHost);
int proxyPort = getProxyPort(args);
System.out.println("proxyPort=" + proxyPort);
ResUrlBean rub = createResUrlBean(proxyHost, proxyPort, allUrl, targetDir);
try
{
for (int i = rub.getStartIndex(); i <= rub.getEndIndex(); i++)
{
SimpleHttpClientSocket client = createSimpleHttpClient(rub, i);
new Thread(client).start();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
private Socket createSocket(String proxyHost, int proxyPort) throws Exception
{
Socket socket = null;
try
{
socket = new Socket(proxyHost, proxyPort);
}
catch (Exception e)
{
SocketUtil.close(socket);
throw e;
}
if (socket == null)
{
System.exit(1);
}
return socket;
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import org.apache.log4j.Logger;
import com.tianhe.frm.utils.LogUtil;
import com.tianhe.frm.utils.StringUtil;
/**
*d:
*
*cd\
*
*java com.tianhe.frm.http.SimpleHttpClientURL http://bizhi.zhuoku.com/2012/04/21/shenlin/Shenlin01.jpg d:/wallpaper/
* proxycn.xxx.com 8080
*
*java com.tianhe.frm.http.SimpleHttpClientURL http://bizhi.zhuoku.com/2012/04/21/shenlin/Shenlin01.jpg d:/wallpaper/
* proxycn.xxx.com 8080
*/
public class SimpleHttpClient implements Runnable
{
private String proxyHost = "";
private String proxyPort = "80";
private String srcUrl = "http://www.aygfsteel.com/Images/adminlogo.gif";
private String dstDir = "d:/wallpaper";
public SimpleHttpClient(String proxyHost, String proxyPort, String srcUrl, String dstDir)
{
if (StringUtil.isNotEmpty(proxyHost))
{
this.proxyHost = proxyHost;
}
if (StringUtil.isNotEmpty(proxyPort))
{
this.proxyPort = proxyPort;
}
this.srcUrl = srcUrl;
this.dstDir = dstDir;
}
public SimpleHttpClient(String srcUrl, String dstDir)
{
this.srcUrl = srcUrl;
this.dstDir = dstDir;
}
public void run()
{
long beginTime = System.currentTimeMillis();
URLConnection conn = null;
InputStream is = null;
int resSize = 0;
if (StringUtil.isNotEmpty(proxyHost))
{
System.setProperty("http.proxySet", "true");
System.setProperty("http.proxyHost", proxyHost);
System.setProperty("http.proxyPort", proxyPort);
}
// ////////////////////////////////////////////////////////////////////////
try
{
URL url = new URL(srcUrl);
conn = url.openConnection();
is = conn.getInputStream();
// ////////////////////////////////////////////////////////////////////////
resSize = processResponse(is);
}
catch (IOException e)
{
throw new RuntimeException("SimpleHttpClient.run[" + srcUrl + "] failed", e);
}
long used = System.currentTimeMillis() - beginTime;
System.out.println(srcUrl + " succeed, used=" + used + ", resSize=" + resSize);
}
private int processResponse(InputStream is) throws IOException, FileNotFoundException
{
int resSize = 0;
FileOutputStream fos;
// String []df = getDirAndFile(resource);
// File file = createTargetFile(df[0], df[1]);
File file = createTargetFile("a/b", "test.gif");
int len = -1;
byte[] buff = new byte[8192];
fos = new FileOutputStream(file);
while ((len = is.read(buff)) != -1)
{
fos.write(buff, 0, len);
resSize = resSize + len;
}
fos.flush();
fos.close();
is.close();
return resSize;
}
public File createTargetFile(String relativeDir, String resName) throws IOException
{
String dir;
if (dstDir.endsWith("/"))
{
dir = dstDir + relativeDir;
}
else
{
dir = dstDir + "/" + relativeDir;
}
File fileDir = new File(dir);
if (!fileDir.exists())
{
fileDir.mkdirs();
}
String pathFile = dir + "/" + resName;
File file = new File(pathFile);
if (!file.exists())
{
file.createNewFile();
}
return file;
}
private Logger getLog()
{
return LogUtil.getLog(getClass());
}
public static void usage()
{
System.out.println("Usage: java SimpleHttpClient -url http://www.aygfsteel.com/Images/adminlogo.gif -dir d:/wallpaper");
}
// private String[] getDirAndFile(String resource)
// {
// String[] df = new String[2];
//
// String vsTemp = resource;
// int index = resource.indexOf("?");
// if (index > 0)
// {
// vsTemp = vsTemp.substring(0, index);
// int index2 = vsTemp.lastIndexOf(".");
// vsTemp = vsTemp.substring(index2 + 1);
// }
// else
// {
// int index2 = vsTemp.lastIndexOf(".");
// vsTemp = vsTemp.substring(index2 + 1);
// }
//
// return df;
// }
public static void main(String[] args)
{
try
{
String srcUrl = "http://www.aygfsteel.com/Images/adminlogo.gif";
String dstDir = "d:/wallpaper";
if (args != null && args.length > 0)
{
for (int i = 0; i < args.length; i++)
{
if (args[i].startsWith("-url"))
{
srcUrl = args[i + 1];
i++;
}
if (args[i].startsWith("-dir"))
{
dstDir = args[i + 1];
i++;
}
if (args[i].equalsIgnoreCase("-help"))
{
usage();
System.exit(0);
}
}
}
SimpleHttpClient client = new SimpleHttpClient(srcUrl, dstDir);
new Thread(client).start();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
public interface Command
{
public byte[] execute(String resource, char[] postData, String charset);
}
package com.tianhe.frm.http;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import com.tianhe.frm.context.GlobalConfig;
import com.tianhe.frm.utils.PropertiesUtil;
import com.tianhe.frm.utils.StringUtil;
public class CommandImpl implements Command
{
private String DEFAULT_RESPONSE;
private String resultConfig = "response.properties";
private Properties resultMapping;
public CommandImpl()
{
init(resultConfig);
}
private void init(String resultConfig2)
{
try
{
if (StringUtil.isNotEmpty(resultConfig))
{
this.resultMapping = PropertiesUtil.loadProperties(ProcessorImpl.class, resultConfig);
}
this.DEFAULT_RESPONSE = createDefaultResponse();
}
catch (Exception e)
{
System.err.println("ProcessorImpl.init failed, ignored!");
}
}
protected String createDefaultResponse()
{
String defaultResponse = GlobalConfig.getString("ProcessorImpl.DEFAULT_RESPONSE",
"request url[%REQUEST_URL%] is not configured in reponse config!");
return defaultResponse;
}
public byte[] execute(String resource, char[] postData, String charset)
{
String resKey = resource;
resKey = resKey.replace("=", "__");
resKey = resKey.replace(":", "--");
resKey = resKey.replace("\"", "");
byte[] resVal = createResultData(resource, resKey, charset);
return resVal;
}
protected byte[] createResultData(String resource, String resKey, String charset)
{
String resVal = resultMapping.getProperty(resKey);
if (StringUtil.isEmpty(resVal))
{
resVal = DEFAULT_RESPONSE.replaceAll("%REQUEST_URL%", resource);
}
byte[] resData = null;
try
{
// resVal = URLEncoder.encode(resVal, charset);
resData = resVal.getBytes(charset);
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
return resData;
}
public static void main(String[] args)
{
CommandImpl s = new CommandImpl();
Properties p = s.resultMapping;
for (Object key : p.keySet())
{
System.out.println(key + "<->" + p.getProperty(key.toString()));
}
}
}
public interface Processor
{
public byte[] process(String resource, char[] postData, String charset);
}
package com.tianhe.frm.http;
import com.tianhe.frm.context.GlobalConfig;
import com.tianhe.frm.utils.ObjectUtil;
public class ProcessorImpl implements Processor
{
public byte[] process(String resource, char[] postData, String charset)
{
Command command = createCommand(resource);
byte[] resVal = command.execute(resource, postData, charset);
return resVal;
}
private Command createCommand(String resource)
{
String type = getCommandType(resource);
String clazzName = GlobalConfig.getString("ProcessorImpl.command." + type + ".CLASS_NAME",
"com.tianhe.frm.http.CommandImpl");
Command command = (Command)ObjectUtil.createObject(clazzName);
return command;
}
private String getCommandType(String resource)
{
String vsTemp = resource;
int index = resource.indexOf("?");
if (index > 0)
{
vsTemp = vsTemp.substring(0, index);
int index2 = vsTemp.lastIndexOf(".");
vsTemp = vsTemp.substring(index2 + 1);
}
else
{
int index2 = vsTemp.lastIndexOf(".");
vsTemp = vsTemp.substring(index2 + 1);
}
return vsTemp;
}
public static void main(String[] args)
{
String type = null;
{
type = new ProcessorImpl().getCommandType("example/aaa.cmd1");
System.out.println(type);
}
{
type = new ProcessorImpl().getCommandType("example/aaa.cmd1?aaa.cmd=111");
System.out.println(type);
}
}
}
public interface Processor
{
public byte[] process(String resource, char[] postData, String charset);
}
package com.tianhe.frm.http;
import com.tianhe.frm.context.GlobalConfig;
import com.tianhe.frm.utils.ObjectUtil;
public class ProcessorImpl implements Processor
{
public byte[] process(String resource, char[] postData, String charset)
{
Command command = createCommand(resource);
byte[] resVal = command.execute(resource, postData, charset);
return resVal;
}
private Command createCommand(String resource)
{
String type = getCommandType(resource);
String clazzName = GlobalConfig.getString("ProcessorImpl.command." + type + ".CLASS_NAME",
"com.tianhe.frm.http.CommandImpl");
Command command = (Command)ObjectUtil.createObject(clazzName);
return command;
}
private String getCommandType(String resource)
{
String vsTemp = resource;
int index = resource.indexOf("?");
if (index > 0)
{
vsTemp = vsTemp.substring(0, index);
int index2 = vsTemp.lastIndexOf(".");
vsTemp = vsTemp.substring(index2 + 1);
}
else
{
int index2 = vsTemp.lastIndexOf(".");
vsTemp = vsTemp.substring(index2 + 1);
}
return vsTemp;
}
public static void main(String[] args)
{
String type = null;
{
type = new ProcessorImpl().getCommandType("example/aaa.cmd1");
System.out.println(type);
}
{
type = new ProcessorImpl().getCommandType("example/aaa.cmd1?aaa.cmd=111");
System.out.println(type);
}
}
}
package com.tianhe.frm.http;
public interface Constant
{
public static String CONTENT_LENGTH = "Content-Length:";
}
package com.tianhe.frm.http;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URLDecoder;
import java.util.StringTokenizer;
import org.apache.log4j.Logger;
import com.tianhe.frm.context.GlobalConfig;
import com.tianhe.frm.exception.OurRuntimeException;
import com.tianhe.frm.utils.LogUtil;
import com.tianhe.frm.utils.ObjectUtil;
import com.tianhe.frm.utils.StringUtil;
public class SimpleHttpServer implements Runnable
{
private boolean debug = false;
private String charset = "gbk";
private ServerSocket serverSocket;// 鏈嶅姟鍣⊿ocket
private static Processor processor = null;
public SimpleHttpServer(int port, String charset, boolean debug)
{
this.debug = debug;
if (StringUtil.isNotEmpty(charset))
{
this.charset = charset;
}
this.serverSocket = createServerSocket(port);
if (this.serverSocket != null)
{
new Thread(this).start();
System.out.println("SimpleHttpServer is running, port=" + port + ",charset=" + charset + ",debug=" + debug);
}
}
private ServerSocket createServerSocket(int port)
{
ServerSocket ss = null;
try
{
ss = new ServerSocket(port);
}
catch (IOException e)
{
throw new OurRuntimeException("createServerSocket failed", e);
}
return ss;
}
public void run()
{
while (true)
{
try
{
Socket socket = serverSocket.accept();
new Thread(new ServerHandler(socket)).start();
}
catch (IOException e)
{
getLog().error("SimpleHttpServer run error", e);
}
}
}
class ServerHandler implements Runnable
{
private Socket socket;
ServerHandler(Socket socket)
{
this.socket = socket;
}
public void run()
{
try
{
boolean firstFlag = false;
String resource = "";
String method = "";
int len = 0;
StringBuffer sb = new StringBuffer();
String line;
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while ((line = in.readLine()) != null)
{
sb.append(line).append("\n");
if (!firstFlag)
{
resource = line.substring(line.indexOf('/') + 1, line.lastIndexOf('/') - 5);
resource = URLDecoder.decode(resource, charset);// 鍙嶇紪鐮?URL
method = new StringTokenizer(line).nextElement().toString();// 鑾峰彇璇鋒眰鏂規硶, GET 鎴栬?POST
firstFlag = true;
}
if (line.startsWith(Constant.CONTENT_LENGTH))
{
len = Integer.valueOf(line.substring(Constant.CONTENT_LENGTH.length()));
}
if (line.equals(""))
break;
}
// 鏄劇ず POST 琛ㄥ崟鎻愪氦鐨勫唴瀹? 榪欎釜鍐呭浣嶄簬璇鋒眰鐨勪富浣撻儴鍒?br /> char[] reqData = null;
if ("POST".equalsIgnoreCase(method) && len > 0)
{
reqData = new char[len];
in.read(reqData);
sb.append(new String(reqData)).append("\n");
}
String requestString = sb.toString();
System.out.println("-------------------------begin----------------------------");
System.out.println(requestString);
System.out.println("-------------------------end------------------------------");
byte[] resData = processRequest(resource, reqData, charset);
writeResponse(resource, resData, socket);
}
catch (IOException e)
{
throw new OurRuntimeException("ServerHandler run error", e);
}
finally
{
closeSocket(socket);
}
}
}
public byte[] processRequest(String resource, char[] data, String charset)
{
if (processor == null)
{
String clazzName = GlobalConfig.getString("ProcessorImpl.CLASS_NAME", "com.tianhe.frm.http.ProcessorImpl");
processor = (Processor)ObjectUtil.createObject(clazzName);
}
byte[] resData = processor.process(resource, data, charset);
return resData;
}
private void writeResponse(String resource, byte[] resData, Socket socket)
{
try
{
PrintStream out = new PrintStream(socket.getOutputStream(), true);
out.println("HTTP/1.0 200 OK");
out.println("Content-Type:text/html;charset=" + charset);
if (resData != null)
{
out.println("Content-Length:" + resData.length);
out.println();
out.write(resData);
}
else
{
out.println("Content-Length: 0");
out.println();
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
void closeSocket(Socket socket)
{
try
{
if (socket != null)
{
socket.close();
}
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
private Logger getLog()
{
return LogUtil.getLog(getClass());
}
private static void usage()
{
System.out.println("Usage: java SimpleHttpServer -port 8080 -charset gbk -debug");
}
public static void main(String[] args)
{
try
{
String charset = "GBK";
int port = 8080;
boolean debug = false;
if (args != null && args.length > 0)
{
for (int i = 0; i < args.length; i++)
{
if (args[i].startsWith("-port"))
{
port = Integer.valueOf(args[i + 1]);
i++;
}
if (args[i].startsWith("-charset"))
{
charset = args[i + 1];
i++;
}
if (args[i].startsWith("-debug"))
{
debug = true;
// i++;
}
if (args[i].equalsIgnoreCase("-help"))
{
usage();
System.exit(0);
}
}
}
new SimpleHttpServer(port, charset, debug);
}
catch (OurRuntimeException ex)
{
ex.printStackTrace();
}
}
}