本帖最后由 gaulhor 于 2009-11-26 23:47:33 编辑

解决方案 »

  1.   

    我理解的是你要抓某一结定文件内容中的图片链接,并将这些图片下载到本地..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();
       }
      

  2.   

    楼主,你的几个链接我都试了,没法下下来。
    我下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"); }}
      

  3.   

    以上代码测试过下载csdn的我自己的头像。
      

  4.   


    网络文件不存在, 为什么要createNewFile?
    如果仅仅只是一个小应用: 这样写是没有问题的, 如果是一个抓取图片的工具,还是建议用HTTPClient之类的开源API去做, 因为你下载比较多的情况下, 网站会限制你的下载行为, 而HTTPClient可以模拟浏览器, 网站无法发现。
      

  5.   

    crazylaa你这个是下载文件,我知道。怎么样能把context字段里面的内容中的所有图片取出来呀
      

  6.   

    String str="content";
    Pattern p = Pattern.compile("<img src[=]'([^']+)'[^>]+>");
    Matcher m = p.matcher(str);
    while(m.find()){
    System.out.println(m.group(1));
    }