Ⅰ 如何使用java調用url介面
原貼地址http://yuanlijia1.iteye.com/blog/1088088
一、在java中調用url,並打開一個新的窗口
Java代碼
Stringurl="http://10.58.2.131:8088/spesBiz/test1.jsp";
Stringcmd="cmd.exe/cstart"+url;
try{
Processproc=Runtime.getRuntime().exec(cmd);
proc.waitFor();
}
catch(Exceptione)
{
e.printStackTrace();
}
二、在java中調用url,後台調用。並取得返回值
Java代碼
URLU=newURL("http://10.58.2.131:8088/spesBiz/test1.jsp");
URLConnectionconnection=U.openConnection();
connection.connect();
BufferedReaderin=newBufferedReader(newInputStreamReader(connection.getInputStream()));
Stringline;
while((line=in.readLine())!=null)
{
result+=line;
}
in.close();
Ⅱ 關於java中URL的使用問題
getResource("image/1.jpg");應該是這里路徑問題。以eclipse環境為例
首先,你的project中必須把image文件夾設為源碼或資源目錄(重要)
這樣image本身就成了資源的根目錄/
在IDE編譯輸出/打包時,1.jpg會和你的AppletTest.class放在同個文件夾中
所以,getResource("image/1.jpg");必須改成getResource("/1.jpg");
而要使getResource("image/1.jpg");起作用就可能要放在 image/image/1.jpg(首個image做資源目錄)
另外,如果你不用IDE——手工編譯class,就必須把1.jpg放在相對AppletTest.class所在目錄下image/1.jpg下,這樣才能getResource("/image/1.jpg")
總之,getResource()中的路徑,由編譯後的class文件的所在位置相對於資源文件的位置決定。而不由java源碼決定。
Ⅲ Java中可以使用URL定位到本地的某個文件嗎
可以的,直接通過URL類實現即可。
舉例:URL fileUrl = new URL("file:///E:/tmp/test.txt");
備註:引入的是「java.net.URL「類。
Ⅳ Java如何利用url下載MP3保存到本地
Java如何利用url下載MP3保存的方法:
1 /** ;
2 * TODO 下載文件到本地 ;
3 * @author nadim ;
4 * @date Sep 11, 2015 11:45:31 AM ;
5 * @param fileUrl 遠程地址 ;
6 * @param fileLocal 本地路徑 ;
7 * @throws Exception ;
8 */ ;
9 public void downloadFile(String fileUrl,String fileLocal) throws Exception {;
10 URL url = new URL(fileUrl);
11 HttpURLConnection urlCon = (HttpURLConnection) url.openConnection();
12 urlCon.setConnectTimeout(6000);
13 urlCon.setReadTimeout(6000);
14 int code = urlCon.getResponseCode();
15 if (code != HttpURLConnection.HTTP_OK) {
16 throw new Exception("文件讀取失敗");
17 }
18 //讀文件流;
19 DataInputStream in = new DataInputStream(urlCon.getInputStream());
20 DataOutputStream out = new DataOutputStream(new FileOutputStream(fileLocal));
21 byte[] buffer = new byte[2048];
22 int count = 0;
23 while ((count = in.read(buffer)) > 0) {;
24 out.write(buffer, 0, count);
25 }
26 out.close();
27 in.close();
28 }。
Java是一門面向對象編程語言,不僅吸收了C++語言的各種優點,還摒棄了C++里難以理解的多繼承、指針等概念,因此Java語言具有功能強大和簡單易用兩個特徵。
Java語言作為靜態面向對象編程語言的代表,極好地實現了面向對象理論,允許程序員以優雅的思維方式進行復雜的編程 。
Ⅳ java中如何實現URL類
java中實現URL類,可以使用java工具類中的URL的類,實例如下:
importjava.io.*;
importjava.net.*;
publicclassURLTest
{
publicstaticvoidmain(String[]args)
{
try
{
URLurl=newURL("http://sports.163.com:80/nba/");//創建資源類型
Stringprotocol=url.getProtocol();//獲取資源類型
Stringhost=url.getHost();//獲取域名
intport=url.getPort();//獲取埠
Stringfile=url.getFile();//獲取路徑
System.out.println("url地址的資源類型為:"+protocol+"域名為:"+host+"埠為:"+port+"路徑為:"+file);
InputStreamis=url.openStream();//獲取頁面信息流
BufferedReaderbfr=newBufferedReader(newInputStreamReader(is));//封裝成字元流
Stringlen;
while((len=bfr.readLine())!=null)
{
System.out.println(len);
}
bfr.close();
is.close();
}
catch(MalformedURLExceptione)
{
System.out.println("創建URL對象發生異常");
}
catch(IOExceptione)
{
System.out.println("發生IO操作異常");
}
}
}
Ⅵ 怎樣在java里用URL引入圖片
讀取圖片可以有以下兩種方法:
①:ImageIO.read(new File("這里可以寫目錄,比如您提到的src/images/某張圖片名"));
②:new ImageIcon("目錄").getImage();
這兩個方法都返回一個圖片對象。可以用一個Image對象接收一下。
相對路徑是指您所運行的程序的包 所在的文件夾開始的路徑。
一般來說,上面兩種讀取方法讀取時,是從項目的目錄下開始找文件的。
所以,您把圖片放在src下的images包中,正確的讀取方法應該是:
Image img=ImageIO.read(new File("src/images/圖片名"));或者
Image img=new ImageIcon("src/images/圖片名").getImage();
得到這樣一個Image對象後,就可以使用了。
Ⅶ java獲取伺服器文件,怎樣用url返回
下面提供二種方法會使用java發送url請求,並獲取伺服器返回的值
第一種方法:
代碼如下:
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.util.EntityUtils;
(StringurlStr,Stringparam1,Stringparam2)throwsException{
StringtempStr=null;
HttpClienthttpclient=newDefaultHttpClient();
Propertiesproperties=newProperties();
HttpEntityentity=null;
StringxmlContent="";
try
{
//設置超時時間
httpclient.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,20000);
httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,20000);
//封裝需要傳遞的參數
List<NameValuePair>nvps=newArrayList<NameValuePair>();
nvps.add(newBasicNameValuePair("mainMemoCode",strmainMemoCode));
nvps.add(newBasicNameValuePair("recordPassWord",strrecordPassWord));
//客戶端的請求方法類型
HttpPosthttpPost=newHttpPost(urlStr);
httpPost.setEntity(newUrlEncodedFormEntity(nvps,"GBK"));
HttpResponseresponse=httpclient.execute(httpPost);
//獲取伺服器返回Http的Content-Type的值
tempStr=response.getHeaders("Content-Type")[0].getValue().toString();
//獲取伺服器返回頁面的值
entity=response.getEntity();
xmlContent=EntityUtils.toString(entity);
Stringstrmessage=null;
System.out.println(xmlContent);
System.out.println(response.getHeaders("Content-Type")[0].getValue().toString());
httpPost.abort();
}
catch(SocketTimeoutExceptione)
{
}
catch(Exceptionex)
{
ex.printStackTrace();
}
finally{
httpclient.getConnectionManager().shutdown();
}
第二種方法:
代碼如下:
(StringurlStr,Stringparam1,Stringparam2)throwsException{
HttpURLConnectionurl_con=null;
try{
URLurl=newURL(urlStr);
StringBufferbankXmlBuffer=newStringBuffer();
//創建URL連接,提交到數據,獲取返回結果
HttpURLConnectionconnection=(HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setRequestProperty("User-Agent","directclient");
PrintWriterout=newPrintWriter(newOutputStreamWriter(connection.getOutputStream(),"GBK"));
out.println(param);
out.close();
BufferedReaderin=newBufferedReader(newInputStreamReader(connection
.getInputStream(),"GBK"));
StringinputLine;
while((inputLine=in.readLine())!=null){
bankXmlBuffer.append(inputLine);
}
in.close();
tempStr=bankXmlBuffer.toString();
}
catch(Exceptione)
{
System.out.println("發送GET請求出現異常!"+e);
e.printStackTrace();
}finally{
if(url_con!=null)
url_con.disconnect();
}
returntmpeStr;
}
總結:多練習代碼,熟練之後才能更快速的去了解代碼的學習的方法。多去獲取一些思維方面的書籍可以看看。
Ⅷ Java Applet 中使用URL
用記事本編譯生成.class,然後編寫.html文件,可以執行
Ⅸ java關於URL類的調用。。。
你上面的代碼中一共有兩個操作,1,向一個url請求數據;2,獲得數據,並解析成html。你遇到的響應慢的問題不在你這個代碼中,而是在你發起url請求時,url的伺服器給你響應的速度。url的伺服器可能有資料庫的操作或其它耗時操作,當它沒有給你返回數據時,你這個方法是阻塞的,並不會立刻返回。