導航:首頁 > 編程語言 > java正則圖片

java正則圖片

發布時間:2024-05-07 13:48:38

java中如何使用正則匹配一對中括弧中的引號冒號(":),目的是在冒號後加個空格

我覺得樓上那位說把中括弧里的取出來處理了再弄回去可行啊,把原字元串一分為三,把中間處理了再組合起來,但是我寫的這個很low只能處理一次中括弧:

❷ Java中什麼是正則表達式

正則表達式 就是將一個類型的數據 按照一定方式 寫成正則表達式的形式 這樣可以辨別數據是否符合這一類型的數據 比如我們的身份證號碼 就是有一定的規則的 這樣我們寫一個 正則表達式 和別人輸入的身份證號碼 比對 如果符合這個正則表達式的規則 就是身份證號碼

❸ 急 java里用正則替換掉img標簽的src里的內容

我覺得可以先用正則把先匹配出imgName = 「圖片名.gif」,然後再替換:

content = content.replaceAll("(.*?)src=\"(.*?)","$1src=\"../image/" + imgName);

❹ 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)

用字元串判斷性能比正則好,
String imgExt = "jpg|jpeg|png|bmp|GIF|JPG|PNG|JPEG";
imgExt .indexOf("圖片後綴") >= 0
或者 imgExt .contains("圖片後綴")
這兩種就可以

閱讀全文

與java正則圖片相關的資料

熱點內容
連續加減乘除法的演算法 瀏覽:652
用mfc編程實現dda演算法 瀏覽:41
linux命令打開應用 瀏覽:146
改造後的程序員 瀏覽:270
數控編程變數 瀏覽:785
江門哪裡有plc編程系統 瀏覽:378
安卓手機如何下載外服b站 瀏覽:700
pythonetree庫 瀏覽:759
數據插值演算法 瀏覽:723
澳大利亞加密貨幣逃稅 瀏覽:482
pdf文檔如何壓縮 瀏覽:329
java單例模式線程安全 瀏覽:646
特種pdf 瀏覽:160
加油什麼app劃算 瀏覽:715
開服要什麼樣的伺服器 瀏覽:33
pdf文件太大怎麼壓縮 瀏覽:29
UK開票顯示文件夾不存在 瀏覽:668
無錫江蘇大容量伺服器公司雲主機 瀏覽:503
pdf朱紹侯 瀏覽:227
編程貓演算法求和 瀏覽:556