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有关的 ...
随机推荐
- CSS自定义文件上传按钮样式,兼容主流浏览器
解决办法:使用text文本框及a链接模拟文件上传按钮,并且把文件上传按钮放在他们上面,并且文件上传按钮显示透明.1.图片2. [代码][HTML]代码 <div class="b ...
- 计算机科学 —— 时间戳(timestamp)
时间戳的一个重要属性即是:唯一性,以起到唯一标识的作用: 1. linux 命令行 $ date +%s 1506222745 2. Python 时间戳 内置 time 库 >> tim ...
- BZOJ_3049_[Usaco2013 Jan]Island Travels _状压DP+BFS
BZOJ_3049_[Usaco2013 Jan]Island Travels _状压DP+BFS Description Farmer John has taken the cows to a va ...
- Cocos2dx+lua合适还是Cocos2dx+js合适?
问题: 开发cocos2dx手游Cocos2dx+lua合适还是Cocos2dx+js合适 百牛信息技术bainiu.ltd整理发布于博客园 回答: 作者:廖宇雷链接:https://www.zhih ...
- 爱奇艺面试Python,竟然挂在第5轮……
今天给大家分享我曾经在爱奇艺的面试,过程还是比较有意思的,可以给大家一些参考 聊骚阶段 嗲妹妹:你好,我是爱奇艺的HR,我们正在招聘运维开发岗位,请问您最近有在看工作机会吗? 我:(这声音也太酥了吧我 ...
- hdoj1394
题意还告诉我们是0-n-1之间的数,那么我们每次把一个数放到后面去,求一下比他大的,还有比他小的: 比如: 1 3 6 9 0 8 5 7 4 2 逆序数num:22 3 6 9 0 8 5 7 4 ...
- ExtWebComponents
我们很高兴地宣布Sencha ExtWebComponents的早期版本现已推出.ExtWebComponents提供了数百个预构建的UI组件,您可以轻松地将它们集成到使用任何框架构建的Web应用程序 ...
- poj3417 闇の連鎖 【树上差分】By cellur925
闇の連鎖(yam.pas/c/cpp)题目描述传说中的暗之连锁被人们称为 Dark.Dark 是人类内心的黑暗的产物,古今中外的勇者们都试图打倒它.经过研究,你发现 Dark 呈现无向图的结构,图中有 ...
- 一些CSS的备忘
text-transform 文本转换 属性值是 none表示没有 不转换 同时也是默认的 capitalize 表示首字母大写 uppercase全部转换为大写 lowercase全部转为小写 te ...
- div里面放img
div里面放img的时候 会出现包裹不住的情况,这个时候 只要将img { width:100%,height:100% },就可以解决问题了