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有关的 ...
随机推荐
- hdu 超级楼梯 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2041 哦--对了,这些题读者可以直接忽略,我只是想练习一下自己薄弱的地方...... 题目意思我就不说 ...
- java中wait和notify
在JAVA中,是没有类似于PV操作.进程互斥等相关的方法的.JAVA的进程同步是通过synchronized()来实现的,需要说明的是,JAVA的synchronized()方法类似于操作系统概念中的 ...
- Netty代码分析
转自:http://www.blogjava.net/BucketLi/archive/2010/12/28/332462.html Netty提供异步的.事件驱动的网络应用程序框架和工具,用以快速开 ...
- codeforces 690C2 C2. Brain Network (medium)(bfs+树的直径)
题目链接: C2. Brain Network (medium) time limit per test 2 seconds memory limit per test 256 megabytes i ...
- MyBatis学习 之 五、MyBatis配置文件
在定义sqlSessionFactory时需要指定MyBatis主配置文件: <bean id="sqlSessionFactory" class="org.myb ...
- Centos6.5 6.6 (均可) 重置密码或强行破解root密码 简单操作
centos忘记root密码怎么重置root密码? 使用Linux系统的时候root密码是十分关键的安全机制. 但是假设那天丢失了root密码的话问题就严重了. 百牛信息技术bainiu.ltd整理发 ...
- HTTP错误code大全
100 - Continue 101 - Switching Protocols Top Success Codes 200 - OK 201 - Created 202 - Accepted 203 ...
- AndroidManifest.xml文件详解(uses-feature) (转载)
转自:http://blog.csdn.net/think_soft/article/details/7596796 语法(SYNTAX): <uses-featureandroid:name= ...
- Ruby基本语法
更新: 2017/06/09 更新: 2017/06/20 cattr_accessor定义类变量,相当于@@ 更新: 2017/06/23 生成类的实例 更新: 2017/06/24 补充loop的 ...
- python 类对象和实例对象动态添加方法
class Person(): def __init__(self, name): self.name = name def print_name(self): print(self.name) p ...