smbfile.java

来自「java ftp 操作代码,程序可以直接运行」· Java 代码 · 共 1,707 行 · 第 1/5 页

JAVA
1,707
字号
        connect0();
        if( tree.inDfs ) {
            exists();
        }
        if( dfsReferral == null ) {
            return null;
        }
        String path = "smb:/" + (new String( dfsReferral.node + unc )).replace( '\\', '/' );
        if (isDirectory()) {
            path += '/';
        }
        return path;
    }

/**
 * Retrieve the time this <code>SmbFile</code> was created. The value
 * returned is suitable for constructing a {@link java.util.Date} object
 * (i.e. seconds since Epoch 1970). Times should be the same as those
 * reported using the properties dialog of the Windows Explorer program.
 *
 * For Win95/98/Me this is actually the last write time. It is currently
 * not possible to retrieve the create time from files on these systems.
 *
 * @return The number of milliseconds since the 00:00:00 GMT, January 1,
 *         1970 as a <code>long</code> value
 */
    public long createTime() throws SmbException {
        if( getUncPath0().length() > 1 ) {
            exists();
            return createTime;
        }
        return 0L;
    }
/**
 * Retrieve the last time the file represented by this
 * <code>SmbFile</code> was modified. The value returned is suitable for
 * constructing a {@link java.util.Date} object (i.e. seconds since Epoch
 * 1970). Times should be the same as those reported using the properties
 * dialog of the Windows Explorer program.
 *
 * @return The number of milliseconds since the 00:00:00 GMT, January 1,
 *         1970 as a <code>long</code> value
 */
    public long lastModified() throws SmbException {
        if( getUncPath0().length() > 1 ) {
            exists();
            return lastModified;
        }
        return 0L;
    }
/**
 * List the contents of this SMB resource. The list returned by this
 * method will be;
 *
 * <ul>
 * <li> files and directories contained within this resource if the
 * resource is a normal disk file directory,
 * <li> all available NetBIOS workgroups or domains if this resource is
 * the top level URL <code>smb://</code>,
 * <li> all servers registered as members of a NetBIOS workgroup if this
 * resource refers to a workgroup in a <code>smb://workgroup/</code> URL,
 * <li> all browseable shares of a server including printers, IPC
 * services, or disk volumes if this resource is a server URL in the form
 * <code>smb://server/</code>,
 * <li> or <code>null</code> if the resource cannot be resolved.
 * </ul>
 *
 * @return A <code>String[]</code> array of files and directories,
 * workgroups, servers, or shares depending on the context of the
 * resource URL
 */
    public String[] list() throws SmbException {
        return list( "*", ATTR_DIRECTORY | ATTR_HIDDEN | ATTR_SYSTEM, null, null );
    }

/**
 * List the contents of this SMB resource. The list returned will be
 * identical to the list returned by the parameterless <code>list()</code>
 * method minus filenames filtered by the specified filter.
 *
 * @param filter a filename filter to exclude filenames from the results
 * @throws SmbException
 # @return An array of filenames
 */
    public String[] list( SmbFilenameFilter filter ) throws SmbException {
        return list( "*", ATTR_DIRECTORY | ATTR_HIDDEN | ATTR_SYSTEM, filter, null );
    }

/**
 * List the contents of this SMB resource as an array of
 * <code>SmbFile</code> objects. This method is much more efficient than
 * the regular <code>list</code> method when querying attributes of each
 * file in the result set.
 * <p>
 * The list of <code>SmbFile</code>s returned by this method will be;
 *
 * <ul>
 * <li> files and directories contained within this resource if the
 * resource is a normal disk file directory,
 * <li> all available NetBIOS workgroups or domains if this resource is
 * the top level URL <code>smb://</code>,
 * <li> all servers registered as members of a NetBIOS workgroup if this
 * resource refers to a workgroup in a <code>smb://workgroup/</code> URL,
 * <li> all browseable shares of a server including printers, IPC
 * services, or disk volumes if this resource is a server URL in the form
 * <code>smb://server/</code>,
 * <li> or <code>null</code> if the resource cannot be resolved.
 * </ul>
 *
 * @return An array of <code>SmbFile</code> objects representing file
 * and directories, workgroups, servers, or shares depending on the context
 * of the resource URL
 */
    public SmbFile[] listFiles() throws SmbException {
        return listFiles( "*", ATTR_DIRECTORY | ATTR_HIDDEN | ATTR_SYSTEM, null, null );
    }

/**
 * The CIFS protocol provides for DOS "wildcards" to be used as
 * a performance enhancement. The client does not have to filter
 * the names and the server does not have to return all directory
 * entries.
 * <p>
 * The wildcard expression may consist of two special meta
 * characters in addition to the normal filename characters. The '*'
 * character matches any number of characters in part of a name. If
 * the expression begins with one or more '?'s then exactly that
 * many characters will be matched whereas if it ends with '?'s
 * it will match that many characters <i>or less</i>.
 * <p>
 * Wildcard expressions will not filter workgroup names or server names.
 *
 * <blockquote><pre>
 * winnt> ls c?o*
 * clock.avi                  -rw--      82944 Mon Oct 14 1996 1:38 AM
 * Cookies                    drw--          0 Fri Nov 13 1998 9:42 PM
 * 2 items in 5ms
 * </pre></blockquote>
 *
 * @param wildcard a wildcard expression
 * @throws SmbException
 * @return An array of <code>SmbFile</code> objects representing file
 * and directories, workgroups, servers, or shares depending on the context
 * of the resource URL
 */

    public SmbFile[] listFiles( String wildcard ) throws SmbException {
        return listFiles( wildcard, ATTR_DIRECTORY | ATTR_HIDDEN | ATTR_SYSTEM, null, null );
    }
/**
 * List the contents of this SMB resource. The list returned will be
 * identical to the list returned by the parameterless <code>listFiles()</code>
 * method minus files filtered by the specified filename filter.
 *
 * @param filter a filter to exclude files from the results
 * @return An array of <tt>SmbFile</tt> objects
 * @throws SmbException
 */
    public SmbFile[] listFiles( SmbFilenameFilter filter ) throws SmbException {
        return listFiles( "*", ATTR_DIRECTORY | ATTR_HIDDEN | ATTR_SYSTEM, filter, null );
    }
/**
 * List the contents of this SMB resource. The list returned will be
 * identical to the list returned by the parameterless <code>listFiles()</code>
 * method minus filenames filtered by the specified filter.
 *
 * @param filter a file filter to exclude files from the results
 * @return An array of <tt>SmbFile</tt> objects
 */
    public SmbFile[] listFiles( SmbFileFilter filter ) throws SmbException {
        return listFiles( "*", ATTR_DIRECTORY | ATTR_HIDDEN | ATTR_SYSTEM, null, filter );
    }
    String[] list( String wildcard, int searchAttributes,
                SmbFilenameFilter fnf, SmbFileFilter ff ) throws SmbException {
        ArrayList list = new ArrayList();

        try {
            if( url.getHost().length() == 0 || share == null ) {
                doNetEnum( list, false, wildcard, searchAttributes, fnf, ff );
            } else {
                doFindFirstNext( list, false, wildcard, searchAttributes, fnf, ff );
            }
        } catch( UnknownHostException uhe ) {
            throw new SmbException( url.toString(), uhe );
        } catch( MalformedURLException mue ) {
            throw new SmbException( url.toString(), mue );
        }

        return (String[])list.toArray(new String[list.size()]);
    }
    SmbFile[] listFiles( String wildcard, int searchAttributes,
                SmbFilenameFilter fnf, SmbFileFilter ff ) throws SmbException {
        ArrayList list = new ArrayList();

        if( ff != null && ff instanceof DosFileFilter ) {
            DosFileFilter dff = (DosFileFilter)ff;
            if( dff.wildcard != null ) {
                wildcard = dff.wildcard;
            }
            searchAttributes = dff.attributes;
        }

        try {
            if( url.getHost().length() == 0 || share == null ) {
                doNetEnum( list, true, wildcard, searchAttributes, fnf, ff );
            } else {
                doFindFirstNext( list, true, wildcard, searchAttributes, fnf, ff );
            }
        } catch( UnknownHostException uhe ) {
            throw new SmbException( url.toString(), uhe );
        } catch( MalformedURLException mue ) {
            throw new SmbException( url.toString(), mue );
        }

        return (SmbFile[])list.toArray(new SmbFile[list.size()]);
    }
    void doNetEnum( ArrayList list,
                boolean files,
                String wildcard,
                int searchAttributes,
                SmbFilenameFilter fnf,
                SmbFileFilter ff ) throws SmbException,
                        UnknownHostException, MalformedURLException {
        SmbComTransaction req;
        SmbComTransactionResponse resp;
        int listType = url.getHost().length() == 0 ? 0 : getType();
        String p = url.getPath();

        if( p.lastIndexOf( '/' ) != ( p.length() - 1 )) {
            throw new SmbException( url.toString() + " directory must end with '/'" );
        }

        switch( listType ) {
            case 0:
                connect0();
                req = new NetServerEnum2( tree.session.transport.server.oemDomainName,
                        NetServerEnum2.SV_TYPE_DOMAIN_ENUM );
                resp = new NetServerEnum2Response();
                break;
            case TYPE_WORKGROUP:
                req = new NetServerEnum2( url.getHost(), NetServerEnum2.SV_TYPE_ALL );
                resp = new NetServerEnum2Response();
                break;
            case TYPE_SERVER:
                req = new NetShareEnum();
                resp = new NetShareEnumResponse();
                break;
            default:
                throw new SmbException( "The requested list operations is invalid: " + url.toString() );
        }

            boolean more;
        do {
            int n;

            send( req, resp );

            more = resp.status == SmbException.ERROR_MORE_DATA;

            if( resp.status != SmbException.ERROR_SUCCESS &&
                    resp.status != SmbException.ERROR_MORE_DATA ) {
                throw new SmbException( resp.status, true );
            }

            n = more ? resp.numEntries - 1 : resp.numEntries;
            for( int i = 0; i < n; i++ ) {
                FileEntry e = resp.results[i];
                String name = e.getName();
                if( fnf != null && fnf.accept( this, name ) == false ) {
                    continue;
                }
                if( name.length() > 0 ) {
                    SmbFile f = new SmbFile( this, name,
                                e.getType(),
                                ATTR_READONLY | ATTR_DIRECTORY, 0L, 0L, 0L );
                    if( ff != null && ff.accept( f ) == false ) {
                        continue;
                    }
                    if( files ) {
                        list.add( f );
                    } else {
                        list.add( name );
                    }
                }
            }
            if( listType != 0 && listType != TYPE_WORKGROUP ) {
                break;
            }
            req.subCommand = (byte)SmbComTransaction.NET_SERVER_ENUM3;
            req.reset( 0, ((NetServerEnum2Response)resp).lastName );
            resp.reset();
        } while( more );
    }
    void doFindFirstNext( ArrayList list,
                boolean files,
                String wildcard,
                int searchAttributes,
                SmbFilenameFilter fnf,
                SmbFileFilter ff ) throws SmbException, UnknownHostException, MalformedURLException {
        SmbComTransaction req;
        Trans2FindFirst2Response resp;
        int sid;
        String path = getUncPath0();
        String p = url.getPath();

        if( p.lastIndexOf( '/' ) != ( p.length() - 1 )) {
            throw new SmbException( url.toString() + " directory must end with '/'" );
        }

        req = new Trans2FindFirst2( path, wildcard, searchAttributes );
        resp = new Trans2FindFirst2Response();

        if( log.level > 2 )
            log.println( "doFindFirstNext: " + req.path );

        send( req, resp );

        sid = resp.sid;
        req = new Trans2FindNext2( sid, resp.resumeKey, resp.lastName );

        /* The only difference between first2 and next2 responses is subCommand
         * so let's recycle the response object.
         */
        resp.subCommand = SmbComTransaction.TRANS2_FIND_NEXT2;

        for( ;; ) {
            for( int i = 0; i < resp.numEntries; i++ ) {
                FileEntry e = resp.results[i];
                String name = e.getName();
                if( name.length() < 3 ) {
                    int h = name.hashCode();
                    if( h == HASH_DOT || h == HASH_DOT_DOT ) {
                        continue;
                    }
                }
                if( fnf != null && fnf.accept( this, name ) == false ) {
                    continue;
                }
        

⌨️ 快捷键说明

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