用Java去操作FTP服务器去做下载,本文章里面分为单个下载和批量下载,批量下载只不过多了一层循环,为了方便参考,我代码都贴出来了。
不管单个下载还是多个,一定要记得,远程服务器的直接写文件夹路径,别加什么ftp://ip了
单个csv下载:
-
- import org.apache.commons.io.FileUtils;
- import org.apache.commons.net.ftp.FTP;
- import org.apache.commons.net.ftp.FTPClient;
-
- import java.io.File;
- import java.io.FileOutputStream;
-
- /**
- * Created by Administrator on 2018/8/29 0029.
- * FTP 工具类
- */
-
- public class FTPUtil {
-
- /************************** 方案一 ****************************/
-
-
- public static void main(String[] args) {
- FTPUtil f = new FTPUtil();
- f.download();
- }
-
-
- public void download(){
- //进行下载文件---------------------------------开始
- //远程服务器下载地址
- //远程路径路径直接主要写,不用在前面加ip什么的,会报路径找不到的
- String fileurl = "/pub/himawari/L2/xxxx.csv";
- //本地文件物理路径
- String realfilepath = "E:/xxxx.csv";
- //本地中新建文件夹目录
- System.out.println("存储路径realfilepath:" + realfilepath + "下载路径:" + fileurl);
- boolean bb = retepasvfile(realfilepath,fileurl,"xxxx.csv");
-
- }
-
-
- /**
- * 本地保存
- *
- * @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 {
- System.out.println("本地存储路径==:" + localurl);
- File file = FileUtils.getFile(localurl);
- ftp.setConnectTimeout(10000);
- ftp.setDataTimeout(10000);
- ftp.connect("192.168.1.12", 21);
- ftp.login("username", "pwd");
- //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("/")));
- System.out.println("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();
- System.out.println("文件下载完成");
- re = true;
- } else {
- System.out.println("文件下载失败");
- //ftp.disconnect();
- }
- }
- } catch (Exception e) {
- e.getStackTrace();
-
- System.out.println("retepasvfileSamba:e=3" + e);
- } finally {
- try {
- ftp.disconnect();
- } catch (Exception e) {
- }
- }
- return re;
- }
-
-
- /************************** 方案二 ****************************/
-
- public static void main(String[] args) {
- String server = "192.168.1.12";
- int port = 21;
-
- String username = "name";
- String password = "pwd";
-
- org.apache.commons.net.ftp.FTPClient ftpClient = new FTPClient();
- try {
- // 连接到FTP服务器
- ftpClient.connect(server, port);
- ftpClient.login(username, password);
-
- // 设置文件传输模式为二进制
- ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
-
-
-
- // 下载文件从FTP服务器
- File fileToDownload = new File("ceshi/1111.xlsx");
- FileOutputStream outputStream = new FileOutputStream(fileToDownload,true);
- ftpClient.retrieveFile("E:\\1111.xlsx", outputStream);
- outputStream.close();
-
- // 断开连接
- ftpClient.logout();
- ftpClient.disconnect();
-
- System.out.println("文件上传和下载成功");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
-
- }
批量下载是基于第一个方案去做的:
- import org.apache.commons.io.FileUtils;
- import org.apache.commons.net.ftp.FTP;
- import org.apache.commons.net.ftp.FTPClient;
- import org.apache.commons.net.ftp.FTPFile;
-
- import java.io.*;
- import java.util.ArrayList;
- import java.util.List;
-
- public class Text {
-
- /*public static void main(String[] args) {
- Text f = new Text();
- f.download();
- }*/
-
-
- public void download(){
- //进行下载文件---------------------------------开始
- //远程服务器下载地址
- String fileurl = "/pub/19/"; //远程目录
- //本地文件物理路径
- String realfilepath = "E:/excel采集"; //本地目录
- //本地中新建文件夹目录
- System.out.println("存储路径realfilepath:" + realfilepath + "下载路径:" + fileurl);
- boolean bb = retepasvfile(realfilepath,fileurl);
-
- }
-
-
- /**
- * 本地保存
- *
- * @param localurl 本地文件物理路径
- * @param hosturl 远程服务器下载地址
- *
- * @return
- */
- public synchronized boolean retepasvfile(String localurl, String hosturl) {
- FTPClient ftp = new FTPClient();
- boolean re = false;
- try {
- System.out.println("本地存储路径==:" + localurl);
- File file = FileUtils.getFile(localurl);
- ftp.setConnectTimeout(10000);
- ftp.setDataTimeout(10000);
- ftp.connect("ftp.ptree.jaxa.jp", 21);
- ftp.login("fsr1812317746_gmail.com", "SP+wari8");
- //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("/")));
- System.out.println("pwd:" + b + "---切换目录:" + hosturl.substring(0, hosturl.lastIndexOf("/")));
- FTPFile[] ftpFiles = ftp.listFiles();
- System.out.println(ftpFiles.length);
-
- if (b) {
- boolean res = false;
- //唯一和单个下载改动的就是这里,去循环获取,然后单个下载,如果数量多的情况下,可以使用线程
- for (FTPFile ftpFile : ftpFiles){
- res = ftp.retrieveFile(ftpFile.getName(), new FileOutputStream(file+"/"+ftpFile.getName()));
- if (res) {
- //ftp.disconnect();
- System.out.println(">>>>>> "+ftpFile.getName()+" 文件下载完成");
- re = true;
- } else {
- System.out.println("文件下载失败");
- //ftp.disconnect();
- }
- }
-
- }
- } catch (Exception e) {
- e.getStackTrace();
-
- System.out.println("retepasvfileSamba:e=3" + e);
- } finally {
- try {
- ftp.disconnect();
-
- } catch (Exception e) {
- }
- }
- return re;
- }
-
-
- }