[1].[代码] ftp上传和删除 跳至 [1] [2] [3]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
package com.itv.launcher.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.Properties;
import java.util.StringTokenizer;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;
import sun.net.ftp.FtpProtocolException;
/** * FTP上传工具类
*
* @author yanzhou
* @version v1.0
*/
public class FTPUtil {
private static FtpClient ftpClient = null ;
private static final String url;
private static final int port;
private static final String user;
private static final String password;
private static final String remoteFilePath;
static {
Properties FTPPro = ReadFTPProperties.getMsgFromPro();
url = FTPPro.getProperty( "FTP_URL" );
port = Integer.parseInt(FTPPro.getProperty( "FTP_PORT" ));
user = FTPPro.getProperty( "FTP_USER" );
password = FTPPro.getProperty( "FTP_PASSWORD" );
remoteFilePath = FTPPro.getProperty( "FTP_REMOTE_FILEPATH" );
}
/**
* 链接FTP
* @throws FtpProtocolException
*/
private static void connectFTP() throws FtpProtocolException {
try {
ftpClient = FtpClient.create();
ftpClient.connect( new InetSocketAddress(url, port));
ftpClient.login(user, password.toCharArray());
ftpClient.setBinaryType();
if (! "" .equals(remoteFilePath) && remoteFilePath != null ) {
ftpClient.changeDirectory(remoteFilePath);
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 关闭FTP链接
*/
public static void closeFTP() {
try {
if (ftpClient != null ) {
ftpClient.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 上传文件到FTP
* @param file file文件,struts2从页面得到的File类型
* @param filePath 要保存在FTP上的路径(文件夹)
* @param fileName 文件名(test001.jpg)
* @return 文件是否上传成功
* @throws Exception
*/
public static boolean upload(File file, String filePath, String fileName) {
TelnetOutputStream to = null ;
FileInputStream fi = null ;
filePath = remoteFilePath + Constants.FILE_SEPARATOR + filePath;
try {
if (file != null ) {
connectFTP();
if (!isDirExist(filePath.replace( "\\" , "/" ))) {
createDir(filePath.replace( "\\" , "/" ));
ftpClient.changeDirectory(filePath.replace( "\\" , "/" ));
}
fi = new FileInputStream(file);
to = (TelnetOutputStream) ftpClient.putFileStream(fileName, true );
byte [] bytes = new byte [ 1024 ];
int i = fi.read(bytes);
while (i != - 1 ) {
to.write(bytes);
i = fi.read(bytes);
}
}
return true ;
} catch (FileNotFoundException e1) {
return false ;
} catch (IOException e2) {
return false ;
} catch (Exception e) {
return false ;
} finally {
if (fi != null ) {
try {
fi.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (to != null ) {
try {
to.flush();
to.close();
} catch (IOException e) {
e.printStackTrace();
}
}
closeFTP();
}
}
/**
* 删除FTP制定目录下的文件
* @param filePath 文件在FTP存储的路径
* @param fileName 要删除的文件名称
* @return 是否删除成功
*/
public static boolean deleteFileFtp(String filePath, String fileName){
try {
connectFTP();
filePath = remoteFilePath + Constants.FILE_SEPARATOR + filePath + Constants.FILE_SEPARATOR;
if (!isDirExist(filePath.replace( "\\" , "/" ))) {
return false ;
}
ftpClient.changeDirectory(filePath.replace( "\\" , "/" ));
ftpClient.deleteFile(fileName);
return true ;
} catch (Exception e) {
e.printStackTrace();
return false ;
} finally {
closeFTP();
}
}
/**
* 检查文件夹是否存在
*
* @param dir
* @param ftpClient
* @return
*/
private static Boolean isDirExist(String dir) {
try {
ftpClient.changeDirectory(dir);
} catch (Exception e) {
return false ;
}
return true ;
}
/**
* 创建文件夹
*
* @param dir
* @param ftpClient
* @throws Exception
*/
private static void createDir(String dir) throws Exception {
ftpClient.setAsciiType();
StringTokenizer s = new StringTokenizer(dir, "/" ); // sign
s.countTokens();
String pathName = "" ;
while (s.hasMoreElements()) {
pathName = pathName + "/" + (String) s.nextElement();
try {
ftpClient.makeDirectory(pathName);
} catch (Exception e) {
e = null ;
}
}
ftpClient.setBinaryType();
}
} |
相关推荐
FTP文件上传文件到服务器 FTP文件上传 vc6.o
ftp文件上传工具 破解版 使用 ! 下载后请于24小时内删除,切勿用于任何商业用途,否则,后果自负!
ftp文件上传源代码;用于上传图片、等等
C# FTP文件上传,下载,删除,目录建立
FtpWebRequest类实现FTP客户端的开发,C#开发,可实现获取服务器文件资源列表并显示出来,可进行文件的上传和下载。
java实现的ftp文件上传
很好的FTP 上传实例,用于FTP文件上传,和下载
一个FTP文件上传封装类 解决XP系统InternetConnect连接超时不可控问题
java + sybase 实行定时(时,分,秒)执行相应的SQL语句 并生产相应的文件上传至FTP上某文件目录下!
Android Studio编写的实现FTP文件上传例子,在手机真机测试通过(FTP服务器可用IIS来搭建)。
Java实现FTP文件上传和下载
Qt+libcurl实现FTP文件上传和下载Qt+libcurl实现FTP文件上传和下载
非常好用的FTP文件上传和下载等管理类,可轻松移植到你的C++程序中,目前已经应用于我们项目中。这个类适用于windows环境下
ftp文件上传jar文件,有需要的同志可以看一下
FTP是一种实现不同主机之间文件共享的软件,只要设置了用户设置了自己的共享目录 当其他用户访问该主机时就可以看到共享文件实现下载和上传的功能
ftp 文件上传 ftp上传图片
visiual C++开发典型模块大全--FTP文件上传下载模块 visiual C++开发典型模块大全--FTP文件上传下载模块 visiual C++开发典型模块大全--FTP文件上传下载模块
Java语言编写的FTP上传下载代码,自己从项目中抽出来的,jar包也在其中,需要的朋友们拿去吧。
VB FTP文件上传、下载,都是源码,供学习开发用。thanks
用JAVA实现Ftp文件上传下载,提供实现方法和测试用例