用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");
  }
}

 

九霄云外
九霄云外

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值