Java Programming Tricks 2
1. Properties ResourceBundle类的路径问题
System.out.println(System.getProperty("user.dir")); //这个是去工程的绝对路径的
System.out.println(Thread.currentThread().getContextClassLoader().getResource(""));//这个是去当 前classpath的uri的!
new Properties().load(new FileInputStream("test.properties"));//这里说明test文件在工程的跟目录下!
new Properties().load(new FileInputStream("test/test.properties"));//说明,在工程底下的test的文件夹底下的文件test.properties!
ResourceBundle rs = ResourceBundle.getBundle("org.hello");//这里说明,hello文件就是在于classpath底下的,org包底下有这个文件hello.properties~也就是说.../bin/这个是classpath绝对路径!
2、一个http请求的头部
GET /index.html HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, */*
Accept-Language: en-us,zh-cn;q=0.5
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 2.0.50727)
Host: localhost:8080
Connection: Keep-Alive
out.println("GET http://fxcm.fx-performance.com/SystemTickets.aspx?bkr=1"
+ "&SystemID=" + systemID + "&InstrumentID=" + instrumentID
+ "&FromDate=20090101&ToDate=20090701 HTTP/1.0");
out.println("Host: " + "fxcm.fx-performance.com");
out.println("User-Agent: " + "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.8.1.9) Gecko/20071025 Firefox/2.0.0.9");
out.println("Accept: " + "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
out.println("Accept-Language: " + "en-us,zh-cn;q=0.5");
//out.println("Accept-Encoding: " + "gzip,deflate");
out.println("Accept-Charset: " + "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
out.println("Keep-Alive: " + "300");
out.println("Proxy-Connection: " + "keep-alive");
//out.println("Cookie: " + "BCSI-CSA9B5F27E=2; BCSI-CSA9B5CE25=2");
out.println("Cache-Control: " + "max-age=0");
out.println("");// 这句很关键,告诉server这段话说完了。然后server端的in.readLine的while循环才能跳出向后执行。
private void fetchData() {
Socket socket = null;
PrintWriter out = null;
BufferedReader in = null;
String fromServer = "";
while (1 == 1) {
try {
Thread.sleep(10000);
socket = new Socket("webproxy.ssmb.com", 8080);
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out.println("GET http://www.dailyfx.com.hk/informer/fxcm_pricedde_sc.php?temp=1");
out.println("Host: www.dailyfx.com.hk");
out.println("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
out.println("Accept-Language: en-us");
out.println("Accept-Encoding: gzip,deflate");
out.println("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7");
out.println("Keep-Alive: 300");
out.println("Proxy-Connection: keep-alive");
out.println("");
while ((fromServer = in.readLine()) != null) {
System.out.print(fromServer);
}
System.out.println();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
in.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
3、inputstream to string
input是一个InputStream的对象。
public void parse() { // Read a set of characters from the socket StringBuffer request = new StringBuffer(2048); int i; byte[] buffer = new byte[2048]; try { i = input.read(buffer); } catch (IOException e) { e.printStackTrace(); i = -1; } for (int j = 0; j < i; j++) { request.append((char) buffer[j]); } System.out.print(request.toString()); uri = parseUri(request.toString()); }