java Downloader.java

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java Downloader.java相关的知识,希望对你有一定的参考价值。


import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * The Class URLDownloader.
 */
public class Downloader {

    /**
     * The Constant BUFF_SIZE.
     */
    static final int BUFF_SIZE = 2 * 1024 * 1024; // 2MB

	private static final boolean verbose = true;
    
    /**
     * The Constant OUTPUTDIR.
     */
    private static String outDir = ".";

    /**
     * Just in time logger util for debugging.
     *
     * @param string the string
     */
    public static void log(final String string) {
        if (verbose) {
            System.out.println(new java.util.Date() + " " + string);
        }
    }


    /**
     * Main method.
     *
     * @param args the arguments
     */
    public static void main(final String args[]) {
        log("START:::");

        if (args.length == 0) {
            System.out.println("Usage:\n\t Downloader <URL-to-download>");
            System.exit(1);
        } else {
        	String url = args[0];
        	String fileName = getFileNameFromURL(url);
            log("Using " + args[0] + " as URL.");
            log("Using " + args[0] + " as URL.");
            getResponse(url, fileName);
        }
        

    }
    /**
     * Make HTP Request.
     *
     * @param urlString the URL that needs to be invoked.
     * @param filename  the local file name that needs to be saved
     * @return the response
     */
    public static void getResponse(final String urlString, final String filename) {
        if (null == filename) {
            throw new RuntimeException(
                    "Missling filename to be saved. Or Try using getResponse(String urlString)");
        }
        FileOutputStream out = null;
        InputStream in = null;
        try {
            final URL url = new URL(urlString);
            log("Opening connection to " + urlString + "...");
            final URLConnection urlC = url.openConnection();
            // Copy resource to local file, use remote file
            // if no local file name specified
            in = url.openStream();
            // Print info about resource
            log("Copying resource (type: " + urlC.getContentType());
            out = new FileOutputStream(filename);

            final byte[] buffer = new byte[BUFF_SIZE];
            int count = 0;
            long size = 0;
            while ((count = in.read(buffer, 0, buffer.length)) > 0) {
                size += count;
                out.write(buffer, 0, count);
            }
            log("Read..." + size + "..bytes, [" + size / 1024 + "KB]");
        } catch (final MalformedURLException e) {
            System.err.println(e.toString());
            e.printStackTrace();
        } catch (final IOException e) {
            System.err.println(e.toString());
            e.printStackTrace();
        } finally {
            if (null != in) {
                try {
                    in.close();
                } catch (final IOException e) {
                    // Ignore
                }
            }
            if (null != out) {
                try {
                    out.close();
                } catch (final IOException e) {
                    // Ignore
                }
            }
        }
    }
    
    public static String getFileNameFromURL(String url) {
    	int slashIndex = url.lastIndexOf('/');
    	String filenameWithoutExtension = url.substring(slashIndex + 1);
    	return filenameWithoutExtension;
    }
}

以上是关于java Downloader.java的主要内容,如果未能解决你的问题,请参考以下文章

Java Math

Java 布尔运算

java [Java] Java常用代码#java

Java - 35 Java 实例

Java While 循环

Java 字符串