⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 download.java

📁 How to get the java home dir.
💻 JAVA
字号:
/**
 * Automated Download a file from the Internet
 */
package com.mindprod.filetransfer;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Download a file from the Internet use:
 * <p/>
 * java com.mindprod.filetransfer.Download http://x.com/y.html C:\y.txt
 *
 * @author Roedy Green
 *         <p/>
 *         2.5 2008-08-10 add setReadTimeout and setConnectTimeout methods.
 *         .
 */
public final class Download
    {
    // ------------------------------ FIELDS ------------------------------

    /**
     * 64K is size buffer for file transfer
     */
    private final static int BUFFSIZE = 64 * 1024;

    // -------------------------- STATIC METHODS --------------------------

    /**
     * Download one file
     *
     * @param urlString source URL of file to download
     *                  e.g."http://www.billabong.com:80/songs/lyrics.txt"
     *                  works with  http:, https:,  file: and possibly others.
     * @param filename  target name of file to save on hard disk.
     * @return true if download succeeded
     * @see "com.mindprod.http.Fetch"
     */
    @SuppressWarnings( { "BooleanMethodNameMustStartWithQuestion" } )
    private static boolean downloadOneFile( String urlString, String filename )
        {
        URL url;
        try
            {
            url = new URL( urlString );
            }
        catch ( MalformedURLException e )
            {
            throw new IllegalArgumentException( "Malformed URL: " + urlString );
            }
        File file;
        try
            {
            file = new File( filename );
            if ( file.exists() )
                {
                if ( !file.canWrite() )
                    {
                    throw new IllegalArgumentException( "Cannot write file: "
                                                        + filename );
                    }
                }
            else
                {
                // create file safely before we fool with URL
                file.createNewFile();
                }
            }
        catch ( IOException e )
            {
            throw new IllegalArgumentException( "Cannot write file: "
                                                + filename );
            }
        FileTransfer downloader = new FileTransfer( BUFFSIZE/* buffsize */ );
        // FileTransfer.download does the actual work.
        return downloader.download( url, file );
        }

    // --------------------------- CONSTRUCTORS ---------------------------

    /**
     * private constructor not used. Only use this class via static main.
     */
    private Download()
        {
        }

    // --------------------------- main() method ---------------------------

    /**
     * Command line Download
     *
     * @param args source url, target file
     *             source URL of file to download
     *             e.g."http://www.billabong.com:80/songs/lyrics.txt"
     *             works with  http:, https:,  file: and possibly others.
     */
    public static void main( String[] args )
        {
        if ( args.length != 2 )
            {
            throw new IllegalArgumentException(
                    "Download needs source URL of file to download and target file name." );
            }
        boolean success = downloadOneFile( args[ 0 ], args[ 1 ] );

        if ( success )
            {
            System.out
                    .println( "Download of "
                              + args[ 0 ]
                              + " to "
                              + args[ 1 ]
                              + " succeeded." );
            }
        else
            {
            System.err
                    .println( "Download of "
                              + args[ 0 ]
                              + " to "
                              + args[ 1 ]
                              + " failed." );
            System.exit( 1 );
            }
        }
    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -