取出字段中的图片文件。。 本帖最后由 gaulhor 于 2009-11-26 23:47:33 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我理解的是你要抓某一结定文件内容中的图片链接,并将这些图片下载到本地..1. 分析文件内容使用正则抓取图片链接,统一存储到某一集合中.2. //imageUrl 将是通过正则表达式抓取的相应图片地址. Url urlObj = new Url(imageUrl); URLConnection urlConObj = urlObj.openConnection(); urlConObj.setDoOutput(true); ReadableByteChannel readableChannelObj = Channels.newChannel(urlConObj.getInputStream()); File fileObj = new File(imageUrl.replaceAll("http:.*/", "")); if (!fileObj.exists()) { fileObj.createNewFile(); } FileChannel fileChannelObj = new FileOutputStream(fileObj).getChannel(); ByteBuffer byteBuffer = ByteBuffer.allocate(1024 * 10); while(readableChannelObj.read(byteBuffer) != -1) { byteBuffer.flip(); fileChannelObj.write(byteBuffer); byteBuffer.clear(); } 楼主,你的几个链接我都试了,没法下下来。我下mp3没问题的。如果你确定可以下,只要解析好那个文件,然后循环掉一下就可以了。package test;import java.io.BufferedInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;public class DownloadFile { public static void getSong(String _path, String _savePath) { String savePath = _savePath; String path = _path; int BYTE_SIZE = 1; int SAVE_SIZE = 1024; byte[] buff = new byte[BYTE_SIZE]; byte[] save = new byte[SAVE_SIZE]; BufferedInputStream bf = null; FileOutputStream file; URL url = null; HttpURLConnection httpUrl; try { url = new URL(path); httpUrl = (HttpURLConnection) url.openConnection(); System.out.println("已经打开连接...."); bf = new BufferedInputStream(httpUrl.getInputStream()); System.out.println("已经获取资源......"); file = new FileOutputStream(savePath); System.out.println("准备保存到:" + savePath); System.out.println("开始读入......"); int i = 0; while (bf.read(buff) != -1) { save[i] = buff[0]; if (i == SAVE_SIZE - 1) { file.write(save, 0, SAVE_SIZE); save = new byte[SAVE_SIZE]; i = 0; } else { i++; } } if (i > 0) { file.write(save, 0, i - 1); } System.out.println("下载成功!!!"); httpUrl.disconnect(); bf.close(); file.close(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { DownloadFile .getSong( " http://cdn1-20.projectplaylist.com/e1/files/cdn7/mp3_new/2584535.mp3", "D:\\daoxiang.mp3"); }} 以上代码测试过下载csdn的我自己的头像。 网络文件不存在, 为什么要createNewFile?如果仅仅只是一个小应用: 这样写是没有问题的, 如果是一个抓取图片的工具,还是建议用HTTPClient之类的开源API去做, 因为你下载比较多的情况下, 网站会限制你的下载行为, 而HTTPClient可以模拟浏览器, 网站无法发现。 crazylaa你这个是下载文件,我知道。怎么样能把context字段里面的内容中的所有图片取出来呀 String str="content";Pattern p = Pattern.compile("<img src[=]'([^']+)'[^>]+>");Matcher m = p.matcher(str);while(m.find()){ System.out.println(m.group(1));} 线程监控的问题 谁知道哪个软件具有图象对比功能 使用secure shell协议包的开发的问题 怎样让Jbutton.setText();的文字不受Jbutton的大小影响啊? 关于网络BUFFER的问题!! 急!!大家看看我写的程序错哪里了? Swill的问题,为什么我点击按钮后,JTree没有创建相应结点呢? 一道考算法的题 oracle下如何得到表的主键??急急急啊!!!!!!! 新手上路``````````撒200分算了~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ jaxb将java对象映射成xml 无法回滚的问题
2. //imageUrl 将是通过正则表达式抓取的相应图片地址.
Url urlObj = new Url(imageUrl);
URLConnection urlConObj = urlObj.openConnection();
urlConObj.setDoOutput(true);
ReadableByteChannel readableChannelObj =
Channels.newChannel(urlConObj.getInputStream());
File fileObj = new File(imageUrl.replaceAll("http:.*/", ""));
if (!fileObj.exists()) {
fileObj.createNewFile();
}
FileChannel fileChannelObj = new FileOutputStream(fileObj).getChannel();
ByteBuffer byteBuffer = ByteBuffer.allocate(1024 * 10);
while(readableChannelObj.read(byteBuffer) != -1) {
byteBuffer.flip();
fileChannelObj.write(byteBuffer);
byteBuffer.clear();
}
我下mp3没问题的。如果你确定可以下,只要解析好那个文件,然后循环掉一下就可以了。
package test;import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;public class DownloadFile { public static void getSong(String _path, String _savePath) {
String savePath = _savePath;
String path = _path;
int BYTE_SIZE = 1;
int SAVE_SIZE = 1024;
byte[] buff = new byte[BYTE_SIZE];
byte[] save = new byte[SAVE_SIZE];
BufferedInputStream bf = null;
FileOutputStream file;
URL url = null;
HttpURLConnection httpUrl;
try {
url = new URL(path);
httpUrl = (HttpURLConnection) url.openConnection();
System.out.println("已经打开连接....");
bf = new BufferedInputStream(httpUrl.getInputStream());
System.out.println("已经获取资源......");
file = new FileOutputStream(savePath);
System.out.println("准备保存到:" + savePath); System.out.println("开始读入......");
int i = 0;
while (bf.read(buff) != -1) {
save[i] = buff[0];
if (i == SAVE_SIZE - 1) {
file.write(save, 0, SAVE_SIZE);
save = new byte[SAVE_SIZE];
i = 0;
} else {
i++;
}
}
if (i > 0) {
file.write(save, 0, i - 1);
}
System.out.println("下载成功!!!");
httpUrl.disconnect();
bf.close();
file.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public static void main(String[] args) {
DownloadFile
.getSong(
" http://cdn1-20.projectplaylist.com/e1/files/cdn7/mp3_new/2584535.mp3",
"D:\\daoxiang.mp3"); }}
网络文件不存在, 为什么要createNewFile?
如果仅仅只是一个小应用: 这样写是没有问题的, 如果是一个抓取图片的工具,还是建议用HTTPClient之类的开源API去做, 因为你下载比较多的情况下, 网站会限制你的下载行为, 而HTTPClient可以模拟浏览器, 网站无法发现。
Pattern p = Pattern.compile("<img src[=]'([^']+)'[^>]+>");
Matcher m = p.matcher(str);
while(m.find()){
System.out.println(m.group(1));
}