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

📄 maxifiletransfer.java

📁 How to get the java home dir.
💻 JAVA
字号:
package com.mindprod.filetransfer;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * copy or download a file. To read or write from the client's local hard disk, you will need a signed Applet and
 * security clearance. see Signed Applet in the Java glossary. To read a files from the server, the file must be given
 * public read access, usually the default. To write a file to the server, you server will have to support CGI-PUT with
 * public access. This is unusual to find. Normally you upload files with FTP. See FTP in the Java glossary.
 *
 * @author copyright (c) 1999-2008 Roedy Green, Canadian Mind Products may be copied and used freely for any purpose but
 *         military.
 * @version 2.5 2008-08-10 add setReadTimeout and setConnectTimeout methods.
 */
public final class MaxiFileTransfer extends FileTransfer
    {
    // -------------------------- PUBLIC INSTANCE  METHODS --------------------------
    /**
     * constructor
     */
    @SuppressWarnings( { "WeakerAccess" } )
    public MaxiFileTransfer()
        {
        super();
        }

    /**
     * constructor
     *
     * @param buffSize how big the i/o chunks are to copy files.
     */
    public MaxiFileTransfer( int buffSize )
        {
        super( buffSize );
        }

    /**
     * Copy a file from a local hard disk to a remote URL. This simulates the HTML PUT upload command. Unfortunately,
     * most servers do not support it, or refuse it. WARNING: I have not tested this code is untested. I have no access
     * to a server that supports PUT. WARNING: This code does not work in Netscape or IE. You must run it as a
     * standalone application.
     *
     * @param source existing file to be copied on local hard disk.
     * @param target remote URL to copy to. e.g. new URL("http://www.billabong.com:80/songs/lyrics.txt")
     * @return true if the copy was successful.
     */
    @SuppressWarnings( { "WeakerAccess", "BooleanMethodNameMustStartWithQuestion" } )
    public boolean upload( File source, URL target )
        {
        if ( source == null )
            {
            return false;
            }
        if ( target == null )
            {
            return false;
            }
        final FileInputStream is;
        final OutputStream os;
        // Netscape and IE don't support HttpURLConnection
        final HttpURLConnection urlc;
        try
            {
            // O P E N S O U R C E
            is = new FileInputStream( source );
            long length = source.length();

            // O P E N T A R G E T
            // must have file write/create permission on remote host

            // Generate a HTTP CGI PUT command
            urlc = ( HttpURLConnection ) target.openConnection();
            if ( urlc == null )
                {
                throw new IOException( "Unable to make a connection." );
                }
            urlc.setAllowUserInteraction( false );
            urlc.setDoInput( true );
            urlc.setDoOutput( true );
            urlc.setUseCaches( false );
            urlc
                    .setRequestProperty( "Content-type",
                            "application/octet-stream" );
            urlc.setRequestProperty( "Content-length",
                    Long.toString( length ) );
            urlc.setRequestMethod( "PUT" );
            urlc.connect();
            os = urlc.getOutputStream();

            // C O P Y S O U R C E T O T A R G E T
            boolean success = copy( is, os, length, true );
            if ( !success )
                {
                return false;
                }

            int statusCode = urlc.getResponseCode();

            // C L O S E
            // is and os handled by inner copy.

            return statusCode == 0;
            }
        catch ( IOException e )
            {
            return false;
            }
        }// end upload

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

    /**
     * Test driver
     *
     * @param args not used
     */
    public static void main( String[] args )
        {
        if ( DEBUGGING )
            {
            MaxiFileTransfer ft = new MaxiFileTransfer();
            System.out
                    .println( ft.copy( new File( "E:\\temp", "index.html" ),
                            new File( "C:\\temp", "a.txt" ) ) );
            try
                {
                System.out
                        .println( ft.download( new URL(
                                "http://mindprod.com/index.html" ),
                                new File( "C:\\temp",
                                        "b.txt" ) ) );
                }
            catch ( MalformedURLException e )
                {
                System.out.println( "bad download url" );
                }

            try
                {
                System.out
                        .println( ft.upload( new File( "E:\\temp",
                                "index.html" ),
                                new URL(
                                        "http://mindprod.com/uploads/c.html" ) ) );
                }
            catch ( MalformedURLException e )
                {
                System.out.println( "bad upload url" );
                }
            }// end if debugging
        }// end main
    }

⌨️ 快捷键说明

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