[原创]从web站点copy文件

该博客展示了从Web站点复制文件到本地磁盘的Java代码。代码通过URL连接到指定的Web资源,获取资源信息,将资源内容复制到本地文件。若未指定本地文件名,则使用远程文件名。同时处理了可能出现的URL格式错误和IO异常。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[原创]从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());
           }
     }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值