curlforjava
❶ centos6.5怎樣裝java se32位
方法一:手動解壓JDK的壓縮包,然後設置環境變數
1.在/usr/目錄下創建java目錄
[root@localhost ~]# mkdir/usr/java
[root@localhost ~]# cd /usr/java
2.下載jdk,然後解壓
[root@localhost java]# curl -O http://download.Oracle.com/otn-pub/java/jdk/7u79-b15/jdk-7u79-linux-x64.tar.gz (自行上 oracle.com 官網查看x86 for linux 32位連接)
[root@localhost java]# tar -zxvf jdk-7u79-linux-x64.tar.gz
3.設置環境變數
[root@localhost java]# vi /etc/profile
在profile中添加如下內容:
#set java environment
JAVA_HOME=/usr/java/jdk1.7.0_79
JRE_HOME=/usr/java/jdk1.7.0_79/jre
CLASS_PATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
export JAVA_HOME JRE_HOME CLASS_PATH PATH
讓修改生效:
[root@localhost java]# source /etc/profile
4.驗證JDK有效性
[root@localhost java]# java -version
java version "1.7.0_79"
Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)
方法二:用yum安裝JDK
1.查看yum庫中都有哪些jdk版本(暫時只發現了openjdk)
[root@localhost ~]# yum search java|grep jdk
ldapjdk-javadoc.x86_64 : Javadoc for ldapjdk
java-1.6.0-openjdk.x86_64 : OpenJDK Runtime Environment
java-1.6.0-openjdk-demo.x86_64 : OpenJDK Demos
java-1.6.0-openjdk-devel.x86_64 : OpenJDK Development Environment
java-1.6.0-openjdk-javadoc.x86_64 : OpenJDK API Documentation
java-1.6.0-openjdk-src.x86_64 : OpenJDK Source Bundle
java-1.7.0-openjdk.x86_64 : OpenJDK Runtime Environment
java-1.7.0-openjdk-demo.x86_64 : OpenJDK Demos
java-1.7.0-openjdk-devel.x86_64 : OpenJDK Development Environment
java-1.7.0-openjdk-javadoc.noarch : OpenJDK API Documentation
java-1.7.0-openjdk-src.x86_64 : OpenJDK Source Bundle
java-1.8.0-openjdk.x86_64 : OpenJDK Runtime Environment
java-1.8.0-openjdk-demo.x86_64 : OpenJDK Demos
java-1.8.0-openjdk-devel.x86_64 : OpenJDK Development Environment
java-1.8.0-openjdk-headless.x86_64 : OpenJDK Runtime Environment
java-1.8.0-openjdk-javadoc.noarch : OpenJDK API Documentation
java-1.8.0-openjdk-src.x86_64 : OpenJDK Source Bundle
ldapjdk.x86_64 : The Mozilla LDAP Java SDK
2.選擇版本,進行安裝
//選擇1.7版本進行安裝
[root@localhost ~]# yum install java-1.7.0-openjdk
//安裝完之後,默認的安裝目錄是在: /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.75.x86_64
3.設置環境變數
[root@localhost ~]# vi /etc/profile
在profile文件中添加如下內容
#set java environment
JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.75.x86_64
JRE_HOME=$JAVA_HOME/jre
CLASS_PATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
export JAVA_HOME JRE_HOME CLASS_PATH PATH
讓修改生效
[root@localhost java]# source /etc/profile
4.驗證(同上一方法)
方法三:用rpm安裝JDK
1.下載rpm安裝文件
[root@localhost ~]$ curl -O http://download.oracle.com/otn-pub/java/jdk/7u79-b15/jdk-7u79-linux-x64.rpm
2.使用rpm命令安裝
[root@localhost ~]# rpm -ivh jdk-7u79-linux-x64.rpm
3.設置環境變數
[root@localhost java]# vi /etc/profile
在打開的profile文件中添加如下內容
#set java environment
JAVA_HOME=/usr/java/jdk1.7.0_79
JRE_HOME=/usr/java/jdk1.7.0_79/jre
CLASS_PATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
export JAVA_HOME JRE_HOME CLASS_PATH PATH
讓修改生效
[root@localhost java]# source /etc/profile
4.驗證(同上一方法)
注:和yum安裝類似,不用設置環境變數就可以運行java命令。rpm安裝方式默認會把jdk安裝到/usr/java/jdk1.7.0_79,然後通過三層鏈接,鏈接到/usr/bin,具體鏈接如下:
[root@localhost ~]# cd /bin
[root@localhost bin]# ll|grep java
lrwxrwxrwx. 1 root root 25 Mar 28 11:24 jar ->/usr/java/default/bin/jar
lrwxrwxrwx. 1 root root 26 Mar 28 11:24 java -> /usr/java/default/bin/java
lrwxrwxrwx. 1 root root 27 Mar 28 11:24 javac ->/usr/java/default/bin/javac
lrwxrwxrwx. 1 root root 29 Mar 28 11:24 javadoc ->/usr/java/default/bin/javadoc
lrwxrwxrwx. 1 root root 28 Mar 28 11:24 javaws ->/usr/java/default/bin/javaws
lrwxrwxrwx. 1 root root 30 Mar 28 11:24 jcontrol ->/usr/java/default/bin/jcontrol
[root@localhost bin]# cd /usr/java/
[root@localhost java]# ll
total 4
lrwxrwxrwx. 1 root root 16 Mar 28 11:24 default-> /usr/java/latest
drwxr-xr-x. 8 root root 4096 Mar 28 11:24 jdk1.7.0_79
lrwxrwxrwx. 1 root root 21 Mar 28 11:24 latest -> /usr/java/jdk1.7.0_79
❷ javacurlhttp請求時間細節,怎麼實現
以下代碼是Java實現的Post、Get、代理訪問請求,可以參考一下
packagecom.snowfigure.kits.net;
importjava.io.BufferedReader;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.InputStreamReader;
importjava.io.OutputStreamWriter;
importjava.io.UnsupportedEncodingException;
importjava.net.HttpURLConnection;
importjava.net.InetSocketAddress;
importjava.net.Proxy;
importjava.net.URL;
importjava.net.URLConnection;
importjava.util.List;
importjava.util.Map;
/**
*Http請求工具類
*@authorsnowfigure
*@since2014-8-2413:30:56
*@versionv1.0.1
*/
publicclassHttpRequestUtil{
staticbooleanproxySet=false;
staticStringproxyHost="127.0.0.1";
staticintproxyPort=8087;
/**
*編碼
*@paramsource
*@return
*/
publicstaticStringurlEncode(Stringsource,Stringencode){
Stringresult=source;
try{
result=java.net.URLEncoder.encode(source,encode);
}catch(UnsupportedEncodingExceptione){
e.printStackTrace();
return"0";
}
returnresult;
}
(Stringsource){
Stringresult=source;
try{
result=java.net.URLEncoder.encode(source,"GBK");
}catch(UnsupportedEncodingExceptione){
e.printStackTrace();
return"0";
}
returnresult;
}
/**
*發起http請求獲取返回結果
*@paramreq_url請求地址
*@return
*/
publicstaticStringhttpRequest(Stringreq_url){
StringBufferbuffer=newStringBuffer();
try{
URLurl=newURL(req_url);
HttpURLConnectionhttpUrlConn=(HttpURLConnection)url.openConnection();
httpUrlConn.setDoOutput(false);
httpUrlConn.setDoInput(true);
httpUrlConn.setUseCaches(false);
httpUrlConn.setRequestMethod("GET");
httpUrlConn.connect();
//將返回的輸入流轉換成字元串
InputStreaminputStream=httpUrlConn.getInputStream();
=newInputStreamReader(inputStream,"utf-8");
BufferedReaderbufferedReader=newBufferedReader(inputStreamReader);
Stringstr=null;
while((str=bufferedReader.readLine())!=null){
buffer.append(str);
}
bufferedReader.close();
inputStreamReader.close();
//釋放資源
inputStream.close();
inputStream=null;
httpUrlConn.disconnect();
}catch(Exceptione){
System.out.println(e.getStackTrace());
}
returnbuffer.toString();
}
/**
*發送http請求取得返回的輸入流
*@paramrequestUrl請求地址
*@returnInputStream
*/
(StringrequestUrl){
InputStreaminputStream=null;
try{
URLurl=newURL(requestUrl);
HttpURLConnectionhttpUrlConn=(HttpURLConnection)url.openConnection();
httpUrlConn.setDoInput(true);
httpUrlConn.setRequestMethod("GET");
httpUrlConn.connect();
//獲得返回的輸入流
inputStream=httpUrlConn.getInputStream();
}catch(Exceptione){
e.printStackTrace();
}
returninputStream;
}
/**
*向指定URL發送GET方法的請求
*
*@paramurl
*發送請求的URL
*@paramparam
*請求參數,請求參數應該是name1=value1&name2=value2的形式。
*@returnURL所代表遠程資源的響應結果
*/
publicstaticStringsendGet(Stringurl,Stringparam){
Stringresult="";
BufferedReaderin=null;
try{
StringurlNameString=url+"?"+param;
URLrealUrl=newURL(urlNameString);
//打開和URL之間的連接
URLConnectionconnection=realUrl.openConnection();
//設置通用的請求屬性
connection.setRequestProperty("accept","*/*");
connection.setRequestProperty("connection","Keep-Alive");
connection.setRequestProperty("user-agent",
"Mozilla/4.0(compatible;MSIE6.0;WindowsNT5.1;SV1)");
//建立實際的連接
connection.connect();
//獲取所有響應頭欄位
Map<String,List<String>>map=connection.getHeaderFields();
//遍歷所有的響應頭欄位
for(Stringkey:map.keySet()){
System.out.println(key+"--->"+map.get(key));
}
//定義BufferedReader輸入流來讀取URL的響應
in=newBufferedReader(newInputStreamReader(
connection.getInputStream()));
Stringline;
while((line=in.readLine())!=null){
result+=line;
}
}catch(Exceptione){
System.out.println("發送GET請求出現異常!"+e);
e.printStackTrace();
}
//使用finally塊來關閉輸入流
finally{
try{
if(in!=null){
in.close();
}
}catch(Exceptione2){
e2.printStackTrace();
}
}
returnresult;
}
/**
*向指定URL發送POST方法的請求
*
*@paramurl
*發送請求的URL
*@paramparam
*請求參數,請求參數應該是name1=value1&name2=value2的形式。
*@paramisproxy
*是否使用代理模式
*@return所代表遠程資源的響應結果
*/
publicstaticStringsendPost(Stringurl,Stringparam,booleanisproxy){
OutputStreamWriterout=null;
BufferedReaderin=null;
Stringresult="";
try{
URLrealUrl=newURL(url);
HttpURLConnectionconn=null;
if(isproxy){//使用代理模式
@SuppressWarnings("static-access")
Proxyproxy=newProxy(Proxy.Type.DIRECT.HTTP,newInetSocketAddress(proxyHost,proxyPort));
conn=(HttpURLConnection)realUrl.openConnection(proxy);
}else{
conn=(HttpURLConnection)realUrl.openConnection();
}
//打開和URL之間的連接
//發送POST請求必須設置如下兩行
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("POST");//POST方法
//設置通用的請求屬性
conn.setRequestProperty("accept","*/*");
conn.setRequestProperty("connection","Keep-Alive");
conn.setRequestProperty("user-agent",
"Mozilla/4.0(compatible;MSIE6.0;WindowsNT5.1;SV1)");
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
conn.connect();
//獲取URLConnection對象對應的輸出流
out=newOutputStreamWriter(conn.getOutputStream(),"UTF-8");
//發送請求參數
out.write(param);
//flush輸出流的緩沖
out.flush();
//定義BufferedReader輸入流來讀取URL的響應
in=newBufferedReader(
newInputStreamReader(conn.getInputStream()));
Stringline;
while((line=in.readLine())!=null){
result+=line;
}
}catch(Exceptione){
System.out.println("發送POST請求出現異常!"+e);
e.printStackTrace();
}
//使用finally塊來關閉輸出流、輸入流
finally{
try{
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
}
catch(IOExceptionex){
ex.printStackTrace();
}
}
returnresult;
}
publicstaticvoidmain(String[]args){
//demo:代理訪問
Stringurl="http://api.adf.ly/api.php";
Stringpara="key=youkeyid&youuid=uid&advert_type=int&domain=adf.ly&url=http://somewebsite.com";
Stringsr=HttpRequestUtil.sendPost(url,para,true);
System.out.println(sr);
}
}