用java下载1个网页

参考了开源软件code。在自己机器上测试通过。

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

public class CopyHtm {
  public static void main(String[] args) throws Exception {
    String sourceUrlString = "sample/some.html";
    String currentDir=System.getProperty("user.dir").replace('\\', '/');
    download("file:///" +currentDir+"/"+sourceUrlString, "tmp");
  }
  final static int TRANSFER_SIZE = 4096;
  public static void download(String link, String dest) {
    File file;
    URL source;
    byte[] data;
    InputStream in;
    FileOutputStream out;
    int read;
    String fname = dest+"/"+genFileName(link);
    File ddest = new File(dest);
    if (!ddest.exists()) {
      ddest.mkdirs();
    }
    
    file = new File(fname);
    if(file.exists()){
      return;
    }
    try {
      source = new URL(link);
      data = new byte[TRANSFER_SIZE];
      try {
        in = source.openStream();
        try {
          out = new FileOutputStream(file);
          try {
            while (-1 != (read = in.read(data, 0, data.length))) {
              out.write(data, 0, read);
            }
          }catch(Exception e){
            System.err.println("broken link=" + link);
          }finally {
            out.close();
          }
        } catch (FileNotFoundException fnfe) {
          fnfe.printStackTrace();
        } finally {
          in.close();
        }
      } catch (FileNotFoundException fnfe) {
        System.err.println("broken link " + fnfe.getMessage() + " ignored");
      }
    } catch (MalformedURLException murle) {
      murle.printStackTrace();
    } catch (IOException ioe) {
      ioe.printStackTrace();
    }
  }

  public static String genFileNameExt(String url, String ext) {
    return genFileNameNoExt(url) + "." + ext;
  }

  public static String genFileNameNoExt(String url) {
    int ilast = url.lastIndexOf("/");
    String fname = url.substring(ilast);
    int didx = fname.lastIndexOf('.');
    if (didx > 0) {
      fname = fname.substring(0, didx);//+"."+ext;
    }
    return fname;
  }

  public static String genFileName(String url) {
    return genFileNameExt(url, "html");
  }
}

 

九霄云外
九霄云外

猜你喜欢

转载自bg090721.iteye.com/blog/1525885
今日推荐