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