導航:首頁 > 編程語言 > 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正則圖片相關的資料

熱點內容
三星qx2是什麼安卓系統 瀏覽:203
保鮮膜解壓球教學視頻 瀏覽:601
多媒體演算法工程師camera 瀏覽:987
電腦下載的歌可以拉到文件夾嗎 瀏覽:722
千鋒3g學院android 瀏覽:445
linux中的yum命令 瀏覽:239
壓縮面膜有幾種 瀏覽:575
怎麼更改安卓程序級別 瀏覽:393
安卓系統運行慢怎麼辦呢 瀏覽:808
外地人在買車本地可以解壓嘛 瀏覽:907
相冊軟體加密怎麼取消 瀏覽:251
麥克風app怎麼打開 瀏覽:22
java泛型t和 瀏覽:356
計算機英文pdf 瀏覽:587
單片機控制的直流調速系統 瀏覽:130
抖音上解壓視頻書單號怎麼做 瀏覽:165
軟體加密之後忘了密碼怎麼辦 瀏覽:944
文件夾怎麼彈出來的 瀏覽:209
51單片機引腳圖電路 瀏覽:214
麥當勞員工怎麼登錄app 瀏覽:530