Java.ftp上传下载

1:jar的maven的引用:

  1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2 	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3 
  4 	<!-- jar包依赖 -->
  5 	<dependencies>
  6 
  7 		<!-- Apache工具组件 -->
  8 	 		<dependency>
  9 			<groupId>commons-net</groupId>
 10 			<artifactId>commons-net</artifactId>
 11 		</dependency>
 12 
 13 	</dependencies>
 14 
 15 
 16 </project>
 
 
 
 
2:javaCode:
  1 package com.taotao.common.utils;
  2 
  3 import java.io.File;
  4 import java.io.FileInputStream;
  5 import java.io.FileNotFoundException;
  6 import java.io.FileOutputStream;
  7 import java.io.IOException;
  8 import java.io.InputStream;
  9 import java.io.OutputStream;
 10 
 11 import org.apache.commons.net.ftp.FTP;
 12 import org.apache.commons.net.ftp.FTPClient;
 13 import org.apache.commons.net.ftp.FTPFile;
 14 import org.apache.commons.net.ftp.FTPReply;
 15 
 16 /**
 17  *
 18  * @ClassName:  FtpUtil
 19  * @Description: ftp上传下载工具类
     *  来源:传智播客
 20  * @author:  刘军/shall_liu(1136808529@qq.com)
 21  * @date:   2017年8月26日 下午1:22:06
 22  *
 23  * @Copyright: 2017
 24  */
 25 public class FtpUtil {
 26 
 27 	/**
 28 	 * Description: 向FTP服务器上传文件
 29 	 * @param host FTP服务器hostname
 30 	 * @param port FTP服务器端口
 31 	 * @param username FTP登录账号
 32 	 * @param password FTP登录密码
 33 	 * @param basePath FTP服务器基础目录
 34 	 * @param filePath FTP服务器文件存放路径。例如分日期存放:/2015/01/01。文件的路径为basePath+filePath
 35 	 * @param filename 上传到FTP服务器上的文件名
 36 	 * @param input 输入流
 37 	 * @return 成功返回true,否则返回false
 38 	 */
 39 	public static boolean uploadFile(String host, int port, String username, String password, String basePath,
 40 			String filePath, String filename, InputStream input) {
 41 		boolean result = false;
 42 		FTPClient ftp = new FTPClient();
 43 		try {
 44 			int reply;
 45 			ftp.connect(host, port);// 连接FTP服务器
 46 			// 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器
 47 			ftp.login(username, password);// 登录
 48 			reply = ftp.getReplyCode();
 49 			if (!FTPReply.isPositiveCompletion(reply)) {
 50 				ftp.disconnect();
 51 				return result;
 52 			}
 53 			//切换到上传目录
 54 			if (!ftp.changeWorkingDirectory(basePath+filePath)) {
 55 				//如果目录不存在创建目录
 56 				String[] dirs = filePath.split("/");
 57 				String tempPath = basePath;
 58 				for (String dir : dirs) {
 59 					if (null == dir || "".equals(dir)) continue;
 60 					tempPath += "/" + dir;
 61 					if (!ftp.changeWorkingDirectory(tempPath)) {
 62 						if (!ftp.makeDirectory(tempPath)) {
 63 							return result;
 64 						} else {
 65 							ftp.changeWorkingDirectory(tempPath);
 66 						}
 67 					}
 68 				}
 69 			}
 70 			//设置上传文件的类型为二进制类型
 71 			ftp.setFileType(FTP.BINARY_FILE_TYPE);
 72 			//上传文件
 73 			if (!ftp.storeFile(filename, input)) {
 74 				return result;
 75 			}
 76 			input.close();
 77 			ftp.logout();
 78 			result = true;
 79 		} catch (IOException e) {
 80 			e.printStackTrace();
 81 		} finally {
 82 			if (ftp.isConnected()) {
 83 				try {
 84 					ftp.disconnect();
 85 				} catch (IOException ioe) {
 86 				}
 87 			}
 88 		}
 89 		return result;
 90 	}
 91 
 92 	/**
 93 	 * Description: 从FTP服务器下载文件
 94 	 * @param host FTP服务器hostname
 95 	 * @param port FTP服务器端口
 96 	 * @param username FTP登录账号
 97 	 * @param password FTP登录密码
 98 	 * @param remotePath FTP服务器上的相对路径
 99 	 * @param fileName 要下载的文件名
100 	 * @param localPath 下载后保存到本地的路径
101 	 * @return
102 	 */
103 	public static boolean downloadFile(String host, int port, String username, String password, String remotePath,
104 			String fileName, String localPath) {
105 		boolean result = false;
106 		FTPClient ftp = new FTPClient();
107 		try {
108 			int reply;
109 			ftp.connect(host, port);
110 			// 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器
111 			ftp.login(username, password);// 登录
112 			reply = ftp.getReplyCode();
113 			if (!FTPReply.isPositiveCompletion(reply)) {
114 				ftp.disconnect();
115 				return result;
116 			}
117 			ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录
118 			FTPFile[] fs = ftp.listFiles();
119 			for (FTPFile ff : fs) {
120 				if (ff.getName().equals(fileName)) {
121 					File localFile = new File(localPath + "/" + ff.getName());
122 
123 					OutputStream is = new FileOutputStream(localFile);
124 					ftp.retrieveFile(ff.getName(), is);
125 					is.close();
126 				}
127 			}
128 
129 			ftp.logout();
130 			result = true;
131 		} catch (IOException e) {
132 			e.printStackTrace();
133 		} finally {
134 			if (ftp.isConnected()) {
135 				try {
136 					ftp.disconnect();
137 				} catch (IOException ioe) {
138 				}
139 			}
140 		}
141 		return result;
142 	}
143 
144 	public static void main(String[] args) {
145 		try {
146 	        FileInputStream in=new FileInputStream(new File("D:\temp\image\gaigeming.jpg"));
147 	        boolean flag = uploadFile("192.168.25.133", 21, "ftpuser", "ftpuser", "/home/ftpuser/www/images","/2015/01/21", "gaigeming.jpg", in);
148 	        System.out.println(flag);
149 	    } catch (FileNotFoundException e) {
150 	        e.printStackTrace();
151 	    }
152 	}
153 }
154 


原文地址:https://www.cnblogs.com/ios9/p/JavaCode_Ftp_UpDownload.html