當前位置:首頁 » 編程語言 » java圖片路徑

java圖片路徑

發布時間: 2024-07-25 15:28:52

java截取網址圖片路徑到指定目錄。並改寫路徑地址

1你想把src裡面的jpg圖片保存到本地某個目錄路徑裡面

2你再把你保存的這個目錄路徑設置回去

3抓取網頁圖片
importjava.io.BufferedReader;
importjava.io.File;
importjava.io.FileOutputStream;
importjava.io.InputStream;
importjava.io.InputStreamReader;
importjava.io.OutputStream;
importjava.net.URL;
importjava.net.URLConnection;
importjava.util.ArrayList;
importjava.util.Calendar;
importjava.util.List;
importjava.util.regex.Matcher;
importjava.util.regex.Pattern;
importcom.sun.xml.internal.fastinfoset.stax.events.Util;

publicclassCatchPicture{

publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
//定義抓取圖片的正則表達式
Stringregular="[*]<b>.*?</b><br/><imgsrc="(.*?)"border=0alt='(.*?)'style=".*?"class=".*?">
";
List<Picture>list=newCatchPicture().lookWeiboPic("http://gaoxiao.jokeji.cn/GrapHtml/dongtai/20120921221658.htm","GBK",regular,"2,1");
System.out.println(list.size());
}
//根據URL查看網站上的圖片
publicList<Picture>lookWeiboPic(Stringurl,Stringcharset,Stringregular,StringattIndex){
List<Picture>list=newArrayList<Picture>();
try{
//獲取填寫的url
//判斷所屬網站獲取正則表達式
//獲取圖片存放到list集合
if(!Util.isEmptyString(url)){
Stringhtmls=getPageSource(url.trim(),charset);
Patternpattern=null;
pattern=Pattern.compile(regular.trim());
if(!Util.isEmptyString(htmls)){
Matchermatcher=pattern.matcher(htmls);

//得到參數屬性順序
String[]sort=regular.trim().split(",");//下標:0表示標題title,1表示圖片路徑
//判斷後綴後得到網站的請求頭部http://www.moonbasa.com/p-032111106.html-->得到http://www.moonbasa.com
String[]suffix;
suffix=url.trim().split("cn");
Stringhttphread="";
if(suffix.length>1){
httphread=suffix[0]+"cn";

}else{
suffix=url.trim().split("com");
httphread=suffix[0]+"com";
}
//循環匹配找到的
while(matcher.find()){
Picturepicture=newPicture();

//匹配出title
if(-1==Integer.parseInt(sort[0])){
//頁面上抓不到標題
picture.setTitle("");
}else{
//去標題的#
Stringtitle=matcher.group(Integer.parseInt(sort[0])).replace("#","");
picture.setTitle(title);
}

//匹配出source
if(-1==Integer.parseInt(sort[1])){
//頁面上抓不到圖片路徑
picture.setSource("");
}else{
StringwebImgUrl=matcher.group(Integer.parseInt(sort[1]));
//判斷是絕對路徑還是相對路徑
String[]pathType=webImgUrl.split(":");
if(pathType.length>1){
//絕對路徑
picture.setSource(webImgUrl);
}else{
//判斷相對路徑是否含有..
pathType=webImgUrl.split("\.\.");
if(pathType.length>1){
picture.setSource(httphread+pathType[1]);
}else{
if(webImgUrl.startsWith("/")){
picture.setSource(httphread+pathType[0]);
}else{
picture.setSource(httphread+"/"+pathType[0]);
}
}
}
}
StringupPath=upload(picture.getSource(),"d:\image\");
picture.setUpPath(upPath);
list.add(picture);
}//--endwhile
}

}
}catch(Exceptione){
e.printStackTrace();
}
returnlist;
}

/**
*根據網路路徑獲取頁面源碼
*@parampageUrl
*@paramencoding
*@return
*/
publicStringgetPageSource(StringpageUrl,Stringencoding){
StringBuffersb=newStringBuffer();
try{
//構建一URL對象
URLurl=newURL(pageUrl);
//使用openStream得到一輸入流並由此構造一個BufferedReader對象
BufferedReaderin=newBufferedReader(newInputStreamReader(url
.openStream(),encoding));
Stringline;
//讀取www資源
while((line=in.readLine())!=null){
sb.append(line);
sb.append(" ");
}
in.close();
}catch(Exceptionex){
System.err.println(ex);
}
returnsb.toString();
}

/**
*上傳圖片
*@paramurlStr
*@parampath
*@return
*@throwsException
*/
publicStringupload(StringurlStr,Stringpath)throwsException{
Calendarcalendar=Calendar.getInstance();
Stringmonth=calendar.get(Calendar.YEAR)+"/"
+(calendar.get(Calendar.MONTH)+1);
Stringfilename=java.util.UUID.randomUUID().toString()
+getExtension(urlStr);
path=path+month+"/";
download(urlStr,path,filename);
returnpath+month+"/"+filename;
}
/**
*根據路徑下載圖片然後保存到對應的目錄下
*@paramurlString
*@paramfilename
*@paramsavePath
*@return
*@throwsException
*/
publicvoiddownload(StringurlString,Stringfilename,StringsavePath)throwsException{
//構造URL
URLurl=newURL(urlString);
//打開連接
URLConnectioncon=url.openConnection();
//設置請求的路徑
con.setConnectTimeout(5*1000);
//輸入流
InputStreamis=con.getInputStream();

//1K的數據緩沖
byte[]bs=newbyte[1024];
//讀取到的數據長度
intlen;
//輸出的文件流
Filesf=newFile(savePath);
if(!sf.exists()){
sf.mkdirs();
}
OutputStreamos=newFileOutputStream(sf.getPath()+"\"+filename);
//開始讀取
while((len=is.read(bs))!=-1){
os.write(bs,0,len);
}
//完畢,關閉所有鏈接
os.close();

is.close();
}

/**
*根據文件名獲取文件的後綴名
*@paramfileUrl
*@return
*/
publicStringgetExtension(StringfileUrl){
returnfileUrl.substring(fileUrl.lastIndexOf("."),fileUrl.length());
}
}

㈡ 在java中怎麼獲得圖片的路徑

你這是相對路徑,在項目文件夾下建一個image文件夾,然後那幾個對應的gif格式的圖片.也可以在那位置寫絕對路徑,D:/image/圖片名

㈢ java 通過 路徑 來調用已有圖片(或其他任何文件)的代碼怎麼寫

Class.getResource("") 返回的是當前Class這個類所在包開始的位置

舉例

	URLurl=XXXX.class.getResource("");
System.out.println(url);
//輸出file:/D:/space/workspace/JDK8/bin/a/
所以a文件下的圖片不需要寫/1.jpg而直接寫1.jpg
URLurl=XXXX.class.getResource("1.jpg");

ImageIcon對象的構造方法很多

比如寫圖片路徑來構造

相對路徑的寫法

ImageIconicon=newImageIcon("src\a\50.png");

完整路徑,

ImageIconicon=newImageIcon("D:\space\workspace\JDK8\src\a\50.png");
使用完整路徑,可以載入電腦上的其他位置的圖片
比如ImageIconicon=newImageIcon("c:\50.png");

在圖片上右鍵單擊------>選擇Properties------->彈出對話框里有Location信息

這個信息就是完整路徑

熱點內容
ios應用上傳 發布:2024-09-08 09:39:41 瀏覽:439
ios儲存密碼哪裡看 發布:2024-09-08 09:30:02 瀏覽:871
opensslcmake編譯 發布:2024-09-08 09:08:48 瀏覽:653
linux下ntp伺服器搭建 發布:2024-09-08 08:26:46 瀏覽:744
db2新建資料庫 發布:2024-09-08 08:10:19 瀏覽:173
頻率計源碼 發布:2024-09-08 07:40:26 瀏覽:780
奧迪a6哪個配置帶後排加熱 發布:2024-09-08 07:06:32 瀏覽:101
linux修改apache埠 發布:2024-09-08 07:05:49 瀏覽:209
有多少個不同的密碼子 發布:2024-09-08 07:00:46 瀏覽:566
linux搭建mysql伺服器配置 發布:2024-09-08 06:50:02 瀏覽:995