第一种方法连接中带有账号密码直接远程下载:
public Result<?> download(){
//进行下载文件---------------------------------开始
//远程服务器下载地址
String fileurl = "ftp://superAdmin:superAdmin123@192.168.1.8:8888/usr/mp4pt/rec/CHN0/10110120011100/00001.mp4";
//文件物理路径
String realfilepath = "F:/rhsftdemo/00001.mp4";
//本地中新建文件夹目录
File file = new File("F:/rhsftdemo/");
if (!file.exists()) {
file.mkdirs();
}
log.info("存储路径realfilepath:" + realfilepath + "下载路径:" + fileurl);
download(fileurl,realfilepath);
return Result.ok();
}
private static final int BUFFER_SIZE = 4096;
public void download(String ftpUrl,String savePath){
long startTime = System.currentTimeMillis();
// String ftpUrl = "ftp://ftp.f-secure.com/misc/unixutil/skeysrcs.zip";
String file = "";
// name of the file which has to be download
String host = ""; // ftp server
String user = ""; // user name of the ftp server
String pass = ""; // password of the ftp server
// String savePath = "F:\\skeysrcs.zip";
ftpUrl = String.format(ftpUrl, user, pass, host);
System.out.println("Connecting to FTP server");
try {
URL url = new URL(ftpUrl);
URLConnection conn = url.openConnection();
InputStream inputStream = conn.getInputStream();
long filesize = conn.getContentLength();
System.out.println("Size of the file to download in kb is:-" + filesize / 1024);
FileOutputStream outputStream = new FileOutputStream(savePath);
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead = -1;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
long endTime = System.currentTimeMillis();
System.out.println("File downloaded");
System.out.println("Download time in sec. is:-" + (endTime - startTime) / 1000);
outputStream.close();
inputStream.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
第二种方法:通过账号密码登录后进行下载:
public Result<?> download(){
//进行下载文件---------------------------------开始
//远程服务器下载地址
String fileurl = "ftp://192.168.1.8:8888/usr/mp4pt/rec/CHN0/20220420095100/00001.mp4";
//本地文件物理路径
String realfilepath = "F:/rhsftdemo/00001.mp4";
//本地中新建文件夹目录
File file = new File("F:/rhsftdemo/");
if (!file.exists()) {
file.mkdirs();
}
log.info("存储路径realfilepath:" + realfilepath + "下载路径:" + fileurl);
boolean bb = retepasvfile(realfilepath,fileurl,"00001.mp4");
return Result.ok();
}
/**
* 本地保存
*
* @param localurl 本地文件物理路径
* @param hosturl 远程服务器下载地址
* @param filename 文件名称
*
* @return
*/
public synchronized boolean retepasvfile(String localurl, String hosturl, String filename) {
FTPClient ftp = new FTPClient();
boolean re = false;
try {
log.info("本地存储路径==:" + localurl);
File file = FileUtil.getFile(localurl);
ftp.setConnectTimeout(10000);
ftp.setDataTimeout(10000);
ftp.connect("192.168.1.8", 21);
if (!org.springframework.util.StringUtils.isEmpty("admin") && !org.springframework.util.StringUtils.isEmpty("superAdmin123") && "superAdmin" != "null" && "superAdmin123" != "null") {
//这里输入账号密码
ftp.login("superAdmin", "superAdmin123");
} else {
log.info("开始链接ftp:");
ftp.login("anonymous", "anonymous");
}
//ftp.bin();
// String str=ftp.pwd();
ftp.enterLocalPassiveMode(); //被动模式
ftp.setControlKeepAliveTimeout(60);
ftp.setFileType(FTP.BINARY_FILE_TYPE);
boolean b = ftp.changeWorkingDirectory(hosturl.substring(0, hosturl.lastIndexOf("/")));
log.info("pwd:" + b + "---切换目录:" + hosturl.substring(0, hosturl.lastIndexOf("/")));
long locationsize = file.length();//服务器上文件的大小
if (b) {
boolean res = false;
res = ftp.retrieveFile(filename, new FileOutputStream(file));
if (res) {
//ftp.disconnect();
log.info("文件下载完成");
re = true;
} else {
log.info("文件下载失败");
//ftp.disconnect();
}
}
} catch (Exception e) {
e.getStackTrace();
log.error("retepasvfileSamba:e=3" + e);
} finally {
try {
ftp.disconnect();
} catch (Exception e) {
}
}
return re;
}