liunx实现FTP下载功能

1.因为liunx服务器没法实现http协议的访问,因此没法直接读取视频,从而也没法运用httpurl实现爬虫抓取,因此百度整合了一个ftp下载的工具类,侵删。java

package com.itsht.adapter.socket;apache

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;服务器

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;socket

public class Ftpdownload {
    /**
     * Description: 从FTP服务器下载文件
     * 
     * @Version. Jul , :: PM by 崔红保(cuihongbao@d-heaven.com)建立
     * @param url
     *            FTP服务器hostname
     * @param port
     *            FTP服务器端口
     * @param username
     *            FTP登陆帐号
     * @param password
     *            FTP登陆密码
     * @param remotePath
     *            FTP服务器上的相对路径
     * @param fileName
     *            要下载的文件名
     * @param localPath
     *            下载后保存到本地的路径
     * @return
     */
    public static boolean downFile(String url, int port, String username,
            String password, String remotePath, String fileName,
            String localPath) {
        boolean success = false;
        FTPClient ftp = new FTPClient();
        try {
            int reply;
            ftp.connect(url, port);
            // 若是采用默认端口,能够使用ftp.connect(url)的方式直接链接FTP服务器
            ftp.login(username, password);// 登陆
            reply = ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftp.disconnect();
                return success;
            }
            ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录
            FTPFile[] fs = ftp.listFiles();
            for (FTPFile ff : fs) {
                if (ff.getName().equals(fileName)) {
                    File localFile = new File(localPath + "/" + ff.getName());
                    OutputStream is = new FileOutputStream(localFile);
                    ftp.retrieveFile(ff.getName(), is);
                    is.close();
                }
            }
            ftp.logout();
            success = true;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ftp.isConnected()) {
                try {
                    ftp.disconnect();
                } catch (IOException ioe) {
                }
            }
        }
        return success;
    }
    
    
    public static void main(String[] args) {
        String ftpurl = "ftp://road:road@192.168.9.53/detectdata/video/00/00/01.mp4";
        
        String url = ftpurl.substring(ftpurl.indexOf("@")+1,ftpurl.indexOf("/",ftpurl.indexOf("/",ftpurl.indexOf("/")+1)+1));
        int port = 21; 
        String username = ftpurl.substring(ftpurl.indexOf("/",ftpurl.indexOf("/")+1)+1,ftpurl.indexOf(":",ftpurl.indexOf(":")+1)); 
        String password = ftpurl.substring(ftpurl.indexOf(":",ftpurl.indexOf(":")+1)+1,ftpurl.indexOf("@")); 
        String remotePath = ftpurl.substring(ftpurl.indexOf("/",ftpurl.indexOf("/",ftpurl.indexOf("/")+1)+1),ftpurl.lastIndexOf("/")); 
        String fileName = ftpurl.substring(ftpurl.lastIndexOf("/")+1,ftpurl.length()); 
        String localPath = "";
        
        downFile(url, port, username, password, remotePath, fileName, localPath);
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}ide

 

===========================================================
jdk都有自带的包,因此无需下载工具包。工具

也有其余的方法实现下载可是会出现乱码或者数据量大的文件下载不了,通常ftp默认端口为21。ui

main方法是动态处理截取信息url

相关文章
相关标签/搜索