java程序,一直图片的网络路径,怎么样吧这个路径的这张照片取下来并保存在服务器上面?就这样一个地址
http://g3.ykimg.com/1100641F4651E8AB8200290955DEA7F39F0E12-D63B-A85A-C8BD-7919DD69CAB9高手求解答 谢谢图片java抓取图片
http://g3.ykimg.com/1100641F4651E8AB8200290955DEA7F39F0E12-D63B-A85A-C8BD-7919DD69CAB9高手求解答 谢谢图片java抓取图片
解决方案 »
- Google App Engine在MyEclipse插件问题!
- 关于jms出现的bug,谁能解决呢?
- Hibernate中Expression.like方法对于整型变量为什么不能用?
- Struts控件样式的问题,谢谢高手指教!
- 需求Tomcat Web Server(jakarta-tomcat-connectors-jk2.0.4-win32-IIS.zip),在线,急!
- 关于文件对话框的一个问题,请各位指教!
- 为什么我的serversocket监听程序只能得到一个连接?
- FreeMaker的循环问题请教写法。
- springmvc通过@requestBody javaBean接收注入跨域请求,但是报错415
- JPA 接收参数不匹配
- ExtJS 同一个页面下 有两个table 如果通过table中ID的不同获得该Table
- spring设置多数据源,但数据源的配置数据都写在一个配置库里如何实现
// TODO Auto-generated method stub
try {
URL url = new URL("http://g3.ykimg.com/1100641F4651E8AB8200290955DEA7F39F0E12-D63B-A85A-C8BD-7919DD69CAB9");
URLConnection conn = url.openConnection();
byte[] byteBuffer = new byte[4096];
InputStream is = conn.getInputStream();
FileOutputStream fos = new FileOutputStream("C:/Users/zhangxinxin/Desktop/test.jpg");
int len = 0;
int off =0;
while((len=is.read(byteBuffer))!=-1)
{
fos.write(byteBuffer, off, len);
}
is.close();
fos.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public boolean getPic(String content,String filepath){
boolean b =false;
String searchImgReg = "(?x)(src|SRC|background|BACKGROUND)=('|\")(http://([\\w-]+\\.)+[\\w-]+(:[0-9]+)*(/[\\w-]+)*(/[\\w-]+\\.(jpg|JPG|png|PNG|gif|GIF)))('|\")";
// String content = ts.getHtmlCode("http://www.infosec.org.cn/news/news_view.php?newsid=17118");
Pattern pattern = Pattern.compile(searchImgReg);
Matcher matcher = pattern.matcher(content);
String filename="";
while(matcher.find()){
//替换文本值
String str =matcher.group(3);
//获取src的文本
filename=str.substring(str.lastIndexOf("/")+1,str.length());
String path = this.getClass().getClassLoader().getResource("").getPath();
String uploadPath = path.substring(1,path.lastIndexOf("WEB-INF/classes")) +filepath;//选定上传的目录此处为当前目录
if(!new File(uploadPath).isDirectory())//选定上传的目录此处为当前目录,没有则创建
new File(uploadPath).mkdirs();
try {
String upPath=new CatchPicture().upload(matcher.group(3),uploadPath,filename);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
b = true;
}
return b;
}
/**
* 上传 图片
* @param urlStr
* @param path
* @return
* @throws Exception
*/
public String upload(String urlStr,String path,String filename) throws Exception{
Calendar calendar = Calendar.getInstance();
String month = calendar.get(Calendar.YEAR) + "/"
+ (calendar.get(Calendar.MONTH) + 1);
download(urlStr,path,filename);
return path+month + "/" + filename;
}
/**
* 根据路径 下载图片 然后 保存到对应的目录下
* @param urlString
* @param savePath
* * @param filename
* @return
* @throws Exception
*/
public void download(String urlString, String savePath,String filename) throws Exception {
// 构造URL
URL url = new URL(urlString);
// 打开连接
URLConnection con = url.openConnection();
//设置请求的路径
con.setConnectTimeout(5*1000);
// 输入流
InputStream is = con.getInputStream();
// 1K的数据缓冲
byte[] bs = new byte[1024];
// 读取到的数据长度
int len;
// 输出的文件流
File sf=new File(savePath);
if(!sf.exists()){
sf.mkdirs();
}
OutputStream os = new FileOutputStream(sf.getPath()+"\\"+filename);
// 开始读取
while ((len = is.read(bs)) != -1) {
os.write(bs, 0, len);
}
// 完毕,关闭所有链接
os.close();
is.close();
}
/**
* 根据URL获得所有的html信息
*
* @param url
* @return
*/
public static String getHtmlByUrl(String url) {
String html = null;
HttpClient httpClient = new DefaultHttpClient(); // 创建httpClient对象
HttpGet httpget = new HttpGet(url); // 以get方式请求该URL
try {
HttpResponse responce = httpClient.execute(httpget); // 得到responce对象
int resStatu = responce.getStatusLine().getStatusCode(); // 返回码
if (resStatu == HttpStatus.SC_OK) { // 200正常 其他就不对
HttpEntity entity = responce.getEntity(); // 获得相应实体
String charset = "UTF-8"; //设置编码
if (entity != null) {
html = EntityUtils.toString(entity,charset); // 获得html源代码
}
}
} catch (Exception e) {
System.out.println("访问【" + url + "】出现异常!");
e.printStackTrace();
} finally {
httpClient.getConnectionManager().shutdown();
}
return html;
}
上面 方法 调用!!