Java 利用FTP上传,下载文件,遍历文件目录
Java实现FTP上传下载文件的工具包有很多,这里我采用Java自带的API,实现FTP上传下载文件。另外JDK1.7以前的版本与其之后版本的API有了较大的改变了。
例如:
JDK1.7之前 | JDK1.7 |
ftpClient = new FtpClinet() | ftpClient = FtpClient.create(ip) |
ftpclient.login(user,password) | ftpclient.login(user,null,password) |
ftpclient.binary() | ftpClient.setBinaryType(); |
一. 连接FTP服务器
1: public class FTPUtil {
2: //FTP服务器IP地址
3: public final static String FTP_HOST = "10.103.240.255";
4:
5: //FTP服务器端口
6: public final static int FTP_PORT = 21;
7:
8: //FTP服务器用户名
9: public final static String FTP_USER = "bloodHunter";
10:
11: //密码
12: public final static String FTP_PASSWORD = "wbljy";
13:
14:
15: public static FtpClient getConnect()
16: {
17: try {
18: FtpClient ftpClient = FtpClient.create(FTP_HOST);
19: ftpClient.login(FTP_USER, FTP_PASSWORD.toCharArray());
20: return ftpClient;
21: } catch (FtpProtocolException e) {
22: // TODO Auto-generated catch block
23: e.printStackTrace();
24: System.out.println("Connect to FTP Server fail!");
25: return null;
26: } catch (IOException e) {
27: // TODO Auto-generated catch block
28: e.printStackTrace();
29: System.out.println("Connect to FTP Server fail!");
30: return null;
31: }
32:
33: }
34: }
二. 上传文件
1: /*
2: * ftp file upload
3: * @param path 上传文件的路径
4: * @param fileName 上传文件名称
5: * @return 上传成功返回true,否则返回false
6: * */
7:
8: public static boolean FtpUpload(String path,String fileName)
9: {
10: TelnetOutputStream os = null;
11: FileInputStream is = null;
12: FtpClient ftpClient = getConnect();
13: try {
14: ftpClient.setBinaryType();
15: os = (TelnetOutputStream) ftpClient.putFileStream(fileName, true);
16: is = new FileInputStream(new File(path));
17: byte[] buffer = new byte[1024];
18: int c;
19: while((c = is.read(buffer)) != -1)
20: {
21: os.write(buffer,0,c);
22: }
23: System.out.println("Upload Success!");
24: return true;
25: } catch (Exception e) {
26: // TODO: handle exception
27: e.printStackTrace();
28: System.out.println("Upload fail!");
29: return false;
30: }finally{
31: try {
32: ftpClient.close();
33: is.close();
34: os.close();
35: } catch (IOException e) {
36: // TODO Auto-generated catch block
37: e.printStackTrace();
38: }
39: }
40: }
三. 下载文件
1: /*
2: * ftp file download
3: * @param path 下载文件的保存路径
4: * @param fileName 下载文件名称
5: * @return 下载成功返回true,否则返回false
6: * */
7: public static boolean FtpDownload(String path,String fileName)
8: {
9: FileInputStream is = null;
10: FileOutputStream os = null;
11: FtpClient ftpClient = getConnect();
12: try {
13: is = (FileInputStream) ftpClient.getFileStream(fileName);
14: os = new FileOutputStream(new File(path));
15: byte[] buffer = new byte[1024];
16: int c;
17: while((c = is.read(buffer)) != -1)
18: {
19: os.write(buffer,0,c);
20: }
21: System.out.println("Download Success!");
22: return true;
23: } catch (FtpProtocolException e) {
24: // TODO Auto-generated catch block
25: e.printStackTrace();
26: System.out.println("Download fail!");
27: return false;
28: } catch (IOException e) {
29: // TODO Auto-generated catch block
30: e.printStackTrace();
31: System.out.println("Download fail");
32: return false;
33: }catch (Exception e) {
34: // TODO: handle exception
35: e.printStackTrace();
36: return false;
37: }
38: finally{
39: try {
40: is.close();
41: os.close();
42: ftpClient.close();
43: } catch (IOException e) {
44: // TODO Auto-generated catch block
45: e.printStackTrace();
46: }
47: }
48: }
四. 遍历FTP目录文件
1: /*
2: * FTP getFileList
3: * @param filenames 保存遍历的文件名
4: * @param path 遍历目录的路径
5: * */
6: public static void getFtpFileList(List<String> filenames,String path){
7: //DataInputStream ds = null;
8: BufferedReader ds = null;
9: FtpClient ftpClient = getConnect();
10: try {
11: ds = new BufferedReader(new InputStreamReader(ftpClient.nameList(path),"ISO-8859-1"));
12: String line = "";
13: while((line = ds.readLine())!=null){
14: line = new String(line.getBytes("ISO-8859-1"),"GBK");
15: String name[] = line.split("/");
16: filenames.add(name[name.length - 1]);
17: }
18: } catch (FtpProtocolException e) {
19: // TODO Auto-generated catch block
20: e.printStackTrace();
21: } catch (IOException e) {
22: // TODO Auto-generated catch block
23: e.printStackTrace();
24: }finally{
25: try {
26: ds.close();
27: ftpClient.close();
28: } catch (IOException e) {
29: // TODO Auto-generated catch block
30: e.printStackTrace();
31: }
32: }
33: }
Java 利用FTP上传,下载文件,遍历文件目录的更多相关文章
- JAVA 实现FTP上传下载(sun.net.ftp.FtpClient)
package com.why.ftp; import java.io.DataInputStream; import java.io.File; import java.io.FileInputSt ...
- 2.6 利用FTP上传所有文件
利用FTP上传所有文件 import os,ftptools class UploadAll(ftptools.FtpTools): #继承上一篇写的Ftptools '''upload an ent ...
- FTP上传下载文件(函数简易版)
FTP上传下载文件(函数简易版) # 服务端 import socket import json import hashlib import struct import os user_dic = { ...
- java客户端调用ftp上传下载文件
1:java客户端上传,下载文件. package com.li.utils; import java.io.File; import java.io.FileInputStream; import ...
- java web service 上传下载文件
1.新建动态web工程youmeFileServer,新建包com,里面新建类FileProgress package com; import java.io.FileInputStream; imp ...
- 【转】Java IOUtils方式上传下载文件 on HDFS
[From]https://www.cnblogs.com/areyouready/p/9795442.html package com.css.hdfs04; import java.io.File ...
- Java实现FTP上传下载功能
Java FTP客户端工具包很多,在此我选用的Apache的FTPClient.这个包的获取可以通过http://commons.apache.org/net/来获取,我使用的是最新的commons- ...
- shell ftp上传下载文件
1. ftp自动登录批量下载文件. #####从ftp服务器上的/home/data 到 本地/home/databackup#### #!/bin/bash ftp -n<<! open ...
- 在linux命令行利用SecureCRT上传下载文件
一般来说,linux服务器大多是通过ssh客户端来进行远程的登陆和管理的,使用ssh登陆linux主机以后,如何能够快速的和本地机器进行文件的交互呢,也就是上传和下载文件到服务器和本地?与ssh有关的 ...
随机推荐
- Android中的ProgressBar的android:indeterminate
不明确(false)就是滚动条的当前值自动在最小到最大值之间来回移动,形成这样一个动画效果,这个只是告诉别人“我正在工作”,但不能提示工作进度到哪个阶段.主要是在进行一些无法确定操作时间的任务时作为提 ...
- 编程方式取得Spring上下文的Properties
在spring初始化时,可以使用Properties配置器把properties文件装载到Spring的上下文中. ... xmlns:context="http://www.springf ...
- UUIDUtils
package com.cc.hkjc.util; import java.util.UUID; /** * 字符串工具类 * * @author:匿名 * */public class UUID ...
- SPOJ:Dandiya Night and Violence(Bitset优化)
It is Dandiya Night! A certain way how dandiya is played is described: There are N pairs of people p ...
- bzoj 5072 小A的树 —— 树形DP
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=5072 由于对于一个子树,固定有 j 个黑点,连通块大小是一个连续的范围: 所以记 f[i][ ...
- 【旧文章搬运】对抗RKU的StealthCode检测
原文发表于百度空间,2009-07-02========================================================================== 快一个月没 ...
- 【旧文章搬运】Windows句柄分配算法(二)
原文发表于百度空间,2009-04-04========================================================================== 在创建句柄 ...
- ol 与ul 的区别
1 <!DOCTYPE html> <html> <body> <ul> <li>咖啡</li> <li>牛奶< ...
- Flink架构及其工作原理
目录 System Architecture Data Transfer in Flink Event Time Processing State Management Checkpoints, Sa ...
- 007--linux常用命令nginx和nfs服务
作业: 集群搭建 1.部署nginx反向代理三个web服务,调度算法使用加权轮询: 2.所有web服务使用共享存储nfs,保证所有web都对其有读写权限,保证数据一致性: 一.nginx服务 1. 先 ...