java获取图片的路径
❶ 关于java获取图片路径问题
但是编译的时候报空指针找不到图片 --- 你把相应错误信息贴一下?
为什么不是全路径,而是D:axx.png --- 这不是全路径是什么
❷ JAVA 如何从HTML代码中获取图片路径
String str ="<p align='center'><img height='200' width='300' alt='' src='/esou/UpLoadFile/Image/[1].jpg' /></p><p align='left'>"
String[] s=str.split("src");
String[] sr = s[1].split("\/>");
String si = sr[0].split("\'");
si[1];
si[1]就是图片路径
❸ 求助一下关于java怎么读取图片路径
图像放错地方了,最好是放在根目录下面(reboot下面),搜寻时从根目录开始的,直接把image放在这里就可以了。
❹ 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上传图片获得项目的路径
servletContext.getRealPath(File.separator)
❻ 在java中怎么获得图片的路径
你这是相对路径,在项目文件夹下建一个image文件夹,然后那几个对应的gif格式的图片.也可以在那位置写绝对路径,D:/image/图片名
❼ java中如何获取特定目录下的图片
ClassLoader/Class的getResource/getResourceAsStream 是从CLASSPATH中获取资源。
可以在eclipse的项目下新建一个resources目录,把images文件夹放到这个目录下,然后把resources加到Build Path里。
❽ JAVA 如何获取,我的java文件就在桌面上,图片在桌面上的image文件夹里,那么调用图片的路径怎么写
java
awt
ToolKit = ToolKit.getDefaultToolKit()
ToolKit.createImage("图片路径");
❾ java可以获取到打开图片的路径吗
看你怎么去调用了。理论上是都可以的。