当前位置:首页 » 编程语言 » 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信息

这个信息就是完整路径

热点内容
诺基亚密码忘了打什么电话 发布:2024-09-17 03:27:09 浏览:555
树深度优先算法 发布:2024-09-17 03:26:58 浏览:472
跳转页源码 发布:2024-09-17 03:13:05 浏览:543
html文件上传表单 发布:2024-09-17 03:08:02 浏览:785
聊天软件编程 发布:2024-09-17 03:00:07 浏览:726
linuxoracle安装路径 发布:2024-09-17 01:57:29 浏览:688
两个安卓手机照片怎么同步 发布:2024-09-17 01:51:53 浏览:207
cf编译后没有黑框跳出来 发布:2024-09-17 01:46:54 浏览:249
安卓怎么禁用应用读取列表 发布:2024-09-17 01:46:45 浏览:524
win10设密码在哪里 发布:2024-09-17 01:33:32 浏览:662