當前位置:首頁 » 編程語言 » java解析url

java解析url

發布時間: 2023-01-15 23:44:05

A. 求用java得到URL相應源文件的方法

Java可以通過鏈接的mime類型來判斷源文件的類型,從而得到源文件內容,示例如下:

URLConnection提供了兩種方法可以猜測(根據實測結果,這個猜測是相當的准)數據的MIME類型。
第一個是:
(Stringname)

這個方法根據URL文件部分的後綴名來判斷類型,跟之前我的方法一樣。這個不能解決上面那個問題。
第二個是:(InputStreamin)
這個方法是根據流的前面幾個位元組來判斷類型,這個就不需要文件後綴名了,完全可以解決上面那個問題。

測試代碼如下:BufferedInputStreambis=null;=null;URLurl=null;url=newURL(strUrl);urlconnection=(HttpURLConnection)url.openConnection();urlconnection.connect();bis=newBufferedInputStream(urlconnection.getInputStream());System.out.println("filetype:"+HttpURLConnection.guessContentTypeFromStream(bis));

B. Java:解析URL發來的JSON,為什麼只能解析到第一個值呢

這種方法沒試過,一般都是把這些數據當成一個對象傳到後台,然後直接將這個對象轉成json數據,不過如果傳的是對象那就不需要轉成json數據再去取了。

C. java怎樣獲取url參數

如果是javaweb 項目,那麼非常簡單,直接調用 HttpServletRequest 對象的 .getParamter("參數名稱") 方法即可得到。
如果是普通java 項目:
/**
* 獲取網址的指定參數值
*
* @param url
* 網址
* @param parameter
* 參數名稱
* @author cevencheng
* @return
*/
public static String getParameter(String url, String parameter, String defaultValue) {
try {
final String charset = "utf-8";
url = URLDecoder.decode(url, charset);
if (url.indexOf('?') != -1) {
final String contents = url.substring(url.indexOf('?') + 1);
HashMap<String, String> map = new HashMap<String, String>();
String[] keyValues = contents.split("&");
for (int i = 0; i < keyValues.length; i++) {
String key = keyValues[i].substring(0, keyValues[i].indexOf("="));
String value = keyValues[i].substring(keyValues[i].indexOf("=") + 1);
if (key.equals(parameter)) {
if (value == null || "".equals(value.trim())) {
return defaultValue;
}
return value;
}
map.put(key, value);
}
}
return null;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

D. java 怎麼獲取一個url最終指向了哪裡

java中確定url指向最終是靠頁面跳轉實現的。

一、跳轉到新頁面,並且是在新窗口中打開頁面:
function openHtml()
{
//do someghing here...
window.open("xxxx.html");
}
window是一個javascript對象,可以用它的open方法,需要注意的是,如果這個頁面不是一相相對路徑,那麼要加「http://」,比如:
function openHtml()
{
window.open("http://www..com");
}

二、在本頁面窗口中跳轉:
function totest2()
{
window.location.assign("test2.html");
}
如果直接使用location.assgin()也可以,但是window.location.assign()更合理一些,當前窗口的location對象的assign()方法。
另外,location對象還有一個方法replace()也可以做頁面跳轉,它跟assign()方法的區別在於:
replace() 方法不會在 History 對象中生成一個新的紀錄。當使用該方法時,新的 URL 將覆蓋 History 對象中的當前紀錄。

E. java 讀取遠程url文件

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

/**
* @author lmq
*
*/
public class RemoteFile {

public static void main(String[] args) throws Exception {
File remoteFile = new File("//192.168.7.146/test/1.txt");// 192.168.7.146是對方機器IP,test是對方那個共享文件夾名字,如果沒有共享是訪問不到的
//遠程文件其實主要是地址,地址弄對了就和本地文件沒什麼區別 ,windows裡面//或者\\\\開頭就表示這個文件是網路路徑了其實這個地址就像我們再windows裡面,點擊開始
//然後點擊運行,然後輸入 \\192.168.7.146/test/1.txt訪問遠程文件一樣的

BufferedReader br = new BufferedReader(new FileReader(remoteFile));
String str;
while ((str = br.readLine()) != null) {
System.out.println(str);
}
br.close();
}
}

F. java爬蟲讀取某一張指定圖片的url,求解答

package pers.jiaming.download.main;import java.io.*; //io包import java.util.regex.*; //正則包import java.net.*; //網路包/** 下載圖片類* */public final class DownloadPictures implements Runnable{
private URL url = null; //URL private URLConnection urlConn = null; //url連接 private BufferedReader bufIn = null; //緩沖讀取器,讀取網頁信息
private static final String IMG_REG = "<img.*src\\s*=\\s*(.*?)[^>]*?>"; //img標簽正則 private static final String IMG_SRC_REG = "src\\s*=\\s*\"?(.*?)(\"|>|\\s+)"; //img src屬性正則
private String downloadPath = null; //保存路徑
//構造,參數:想要下載圖片的網址、下載到的圖片存放的文件路徑 public DownloadPictures(String urlStr, String downloadPath)
{
createFolder(downloadPath); //創建文件夾
try {
url = new URL(urlStr);
urlConn = url.openConnection();
//設置請求屬性,有部分網站不加這句話會拋出IOException: Server returned HTTP response code: 403 for URL異常 //如:b站 urlConn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
bufIn = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
}
catch (Exception e) {
e.printStackTrace();
}

this.downloadPath = downloadPath;
}

//檢測路徑是否存在,不存在則創建 private void createFolder(String path)
{
File myPath = new File(path);

if (!myPath.exists()) //不存在則創建文件夾 myPath.mkdirs();
}

//下載函數 public void Download()
{
final int N = 20; //每一次處理的文本行數,這個數越小越容易遺漏圖片鏈接,越大效率越低 (理論上)
String line = "";
String text = "";

while (line != null) //網頁內容被讀完時結束循環 {
for(int i = 0; i < N; i++) //讀取N行網頁信息存入到text當中,因為src內容可能分為多行,所以使用這種方法 try {
line = bufIn.readLine(); //從網頁信息中獲取一行文本
if(line != null) //判斷防止把null也累加到text中 text += line;
}
catch (IOException e) {
e.printStackTrace();
}

//將img標簽正則封裝對象再調用matcher方法獲取一個Matcher對象 final Matcher imgM = Pattern.compile(IMG_REG).matcher(text);

if(!imgM.find()) //如果在當前text中沒有找到img標簽則結束本次循環 continue;

//將img src正則封裝對象再調用matcher方法獲取一個Matcher對象 //用於匹配的文本為找到的整個img標簽 final Matcher imgSrcM = Pattern.compile(IMG_SRC_REG).matcher(imgM.group());

while (imgSrcM.find()) //從img標簽中查找src內容 {
String imageLink = imgSrcM.group(1); //從正則中的第一個組中得到圖片鏈接
print(imageLink); //列印一遍鏈接
//如果得到的src內容沒有寫協議,則添加上// if(!imageLink.matches("https://[\\s\\S]*")) //這里有問題// imageLink = "https://" + imageLink;
print(imageLink); //列印一遍鏈接
try
{
//緩沖輸入流對象,用於讀取圖片鏈接的圖片數據 //在鏈接的圖片不存在時會拋出未找到文件異常 final BufferedInputStream in = new BufferedInputStream(new URL(imageLink).openStream());

//文件輸出流對象用於將從url中讀取到的圖片數據寫入到本地 //保存的路徑為downloadPath,保存的圖片名為時間戳+".png" final FileOutputStream file = new FileOutputStream(new File(downloadPath + System.currentTimeMillis() + ".png"));

int temp; //用於保存in從圖片連接中獲取到的數據 while ((temp = in.read()) != -1)
file.write(temp); //將數據寫入到本地路徑中
//關閉流 file.close();
in.close();

//下載完一張圖片後休息一會 try {
Thread.sleep(800);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}

//將text中的文本清空 text = "";
}
}

//run @Override
public void run()
{
Download(); //下載函數 }

//列印語句 public void print(Object obj)
{
System.out.println(obj);
}}

熱點內容
雲伺服器的ip固定的嗎 發布:2025-07-15 19:26:12 瀏覽:934
怎麼建立電腦配置文件 發布:2025-07-15 19:23:57 瀏覽:672
手機導航源碼下載 發布:2025-07-15 19:18:29 瀏覽:499
什麼是原生態安卓機 發布:2025-07-15 19:16:52 瀏覽:686
linux的安裝目錄在哪 發布:2025-07-15 19:10:04 瀏覽:724
2008編程入門經典 發布:2025-07-15 18:58:44 瀏覽:603
艾派密碼是什麼 發布:2025-07-15 18:47:40 瀏覽:588
密碼鎖如何在裡面開門 發布:2025-07-15 18:35:00 瀏覽:521
額溫演算法 發布:2025-07-15 18:18:14 瀏覽:728
ie客戶端事件腳本執行異常 發布:2025-07-15 18:10:13 瀏覽:26