[原创]从web站点copy文件
2004-09-01 20:15
232 查看
[原创]从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());
}
}
}
/**
* @(#) 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());
}
}
}
相关文章推荐
- [原创]java WEB学习笔记99:Spring学习---Spring Bean配置:自动装配,配置bean之间的关系(继承/依赖),bean的作用域(singleton,prototype,web环境作用域),使用外部属性文件
- 【原创】MVC +WebUploader 实现分片上传大文件
- 利用XML序列化和Asp.Net Web缓存实现站点配置文件
- [原创] Web站点数据库分布存储浅谈
- 在根 web.config 文件之外使用注册为 allowDefinition='MachineToWebRoot' 的节是错误的[原创]
- 为Windows Azure Web站点添加MIME类型解决文件下载失败的问题
- 在 Web.config 文件中配置多个站点地图
- web站点下robots.txt文件的书写与注意事项
- [原创] Web站点数据库分布存储浅谈
- 如何把web站点的所有.aspx.cs文件编译为.dll文件?
- 关于copy了相同web project文件要怎么改properties配置
- 使用Frontpage RPC管理web站点的文件
- web站点和windows服务项目发布时如何排除指定文件
- 一个WEB项目安装包,自动配置数据库,config文件和虚拟目录。。(原创)
- web配置站点访问固定文件url不变
- 如何把web站点的所有.aspx.cs文件编译为.dll文件?
- 微软ASP.NET站点部署指南(3):使用Web.Config文件的Transformations
- iis中web站点无法启动:另一程序正在使用该文件,进程无法访问--解决
- JSP中站点中的WEB-INF下的web.xml文件有什么用呢?
- 【转】 VS2005 ASP.NET2.0安装项目的制作(包括数据库创建、站点创建、IIS属性修改、Web.Config文件修改)