[原创]从web站点copy文件

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

[原创]从web站点copy文件

/**

* @(#) copyURL.java

*

* Copyright 2004 Opensource Develop Team. All rights reserved.

*/

// package

package com.opensource.url;

// import classes

import java.net.*;

import java.io.*;

import java.util.Date;

import java.util.StringTokenizer;

/**

* Utility for copying files from the Internet to local disk

* Example: 1. java copyURL http://www.patriot.net/users/anil/resume/resume.gif

*

* 2. java copyURL http://www.ibm.com/index.html abcd.html

*/

public class copyURL

{

public static void main(String args[])

{

if (args.length < 1)

{

System.err.println("usage: java copyURL URL [LocalFile]");

System.exit(1);

}

try

{

URL url = new URL(args[0]);

System.out.println("Opening connection to " + args[0] + "...");

URLConnection urlC = url.openConnection();

// Copy resource to local file, use remote file

// if no local file name specified

InputStream is = url.openStream();

// Print info about resource

System.out.print("Copying resource (type: " +

urlC.getContentType());

Date date=new Date(urlC.getLastModified());

System.out.println(", modified on: " + date.toLocaleString() + ")...");

System.out.flush();

FileOutputStream fos=null;

if (args.length < 2)

{

String localFile=null;

// Get only file name

StringTokenizer st=new StringTokenizer(url.getFile(), "/");

while (st.hasMoreTokens())

localFile=st.nextToken();

fos = new FileOutputStream(localFile);

}

else

fos = new FileOutputStream(args[1]);

int oneChar, count=0;

while ((oneChar=is.read()) != -1)

{

fos.write(oneChar);

count++;

}

is.close();

fos.close();

System.out.println(count + " byte(s) copied");

}

catch (MalformedURLException e)

{

System.err.println(e.toString());

}

catch (IOException e)

{

System.err.println(e.toString());

}

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航