国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務器之家 - 編程語言 - JAVA教程 - JDK1.7以上javaFTP上傳刪除文件的實現方法

JDK1.7以上javaFTP上傳刪除文件的實現方法

2021-02-22 11:04OkidoGreen JAVA教程

下面小編就為大家分享一篇JDK1.7以上javaFTP上傳刪除文件的實現方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

實例如下:

?
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
packagecom.itv.launcher.util;
 
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.IOException;
importjava.net.InetSocketAddress;
importjava.util.Properties;
importjava.util.StringTokenizer;
 
importsun.net.TelnetOutputStream;
importsun.net.ftp.FtpClient;
importsun.net.ftp.FtpProtocolException;
 
/**
 *
 FTP上傳工具類
 *
 *
 @author yanzhou
 *
 @version v1.0
 */
publicclass
FTPUtil {
 
  privatestatic
FtpClient ftpClient = null;
  privatestatic
final
String url;
  privatestatic
final
int
port;
  privatestatic
final
String user;
  privatestatic
final
String password;
  privatestatic
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
   */
  privatestatic
void
connectFTP() throwsFtpProtocolException
 {
    try{
      ftpClient
 = FtpClient.create();
      ftpClient.connect(newInetSocketAddress(url,
 port));
      ftpClient.login(user,
 password.toCharArray());
      ftpClient.setBinaryType();
      if(!"".equals(remoteFilePath)
 && remoteFilePath != null)
 {
        ftpClient.changeDirectory(remoteFilePath);
      }
    }catch(IOException
 e) {
      e.printStackTrace();
    }
  }
 
  /**
   *
 關閉FTP鏈接
   */
  publicstatic
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
   */
  publicstatic
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
 = newFileInputStream(file);
        to
 = (TelnetOutputStream) ftpClient.putFileStream(fileName, true);
        byte[]
 bytes = newbyte[1024];
        inti
 = fi.read(bytes);
        while(i
 != -1)
 {
          to.write(bytes);
          i
 = fi.read(bytes);
        }
      }
      returntrue;
    }catch(FileNotFoundException
 e1) {
      returnfalse;
    }catch(IOException
 e2) {
      returnfalse;
    }catch(Exception
 e) {
      returnfalse;
    }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 是否刪除成功
   */
  publicstatic
boolean
deleteFileFtp(String filePath, String fileName){
    try{
      connectFTP();
      filePath
 = remoteFilePath + Constants.FILE_SEPARATOR + filePath + Constants.FILE_SEPARATOR;
      if(!isDirExist(filePath.replace("\\","/")))
 {
        returnfalse;
      }
      ftpClient.changeDirectory(filePath.replace("\\","/"));
      ftpClient.deleteFile(fileName);
      returntrue;
    }catch(Exception
 e) {
      e.printStackTrace();
      returnfalse;
    }finally{
      closeFTP();
    }
  }
  /**
   *
 檢查文件夾是否存在
   *
   *
 @param dir
   *
 @param ftpClient
   *
 @return
   */
  privatestatic
Boolean isDirExist(String dir) {
    try{
      ftpClient.changeDirectory(dir);
    }catch(Exception
 e) {
      returnfalse;
    }
    returntrue;
  }
 
  /**
   *
 創建文件夾
   *
   *
 @param dir
   *
 @param ftpClient
   *
 @throws Exception
   */
  privatestatic
void
createDir(String dir) throwsException
 {
    ftpClient.setAsciiType();
    StringTokenizer
 s = newStringTokenizer(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();
 
  }
 
}

2. 常量類,系統的路徑分隔符

?
1
2
3
4
5
6
7
8
9
packagecom.itv.launcher.util;
 
publicinterface
Constants {
   
  //路徑分隔符
  publicstatic
String FILE_SEPARATOR = System.getProperty("file.separator");
}

3. FTP鏈接的配置properties文件,包括用戶名密碼一些信息    

?
1
2
3
4
5
6
7
8
9
10
#FTP的IP地址
FTP_URL=127.0.0.1
#FTP端口號
FTP_PORT=1234
#用戶名
FTP_USER=yanzhou
#密碼
FTP_PASSWORD=abcdefg12345
#FTP賬號目錄
FTP_REMOTE_FILEPATH=

以上這篇JDK1.7以上javaFTP上傳刪除文件的實現方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。

原文鏈接:http://blog.csdn.net/z69183787/article/details/50481205

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久黄网 | 国产成人精品一区二区三区网站观看 | 欧美一区二区三区视频在线观看 | 在线三级电影 | 午夜成人免费视频 | 日韩国产一区二区 | 免费成人高清在线视频 | 中文字幕一区二区三区乱码图片 | 欧美另类国产 | 欧美日韩中文 | 亚洲天堂中文 | 久久精品色欧美aⅴ一区二区 | 香蕉夜色| 免费污视频在线 | 久久国产亚洲精品 | 日韩精品一二三区 | 成人高清av | 久久99精品国产自在现线 | 精品三级在线观看 | 日本不卡一区二区 | 免费日韩 | 精品久久久久久久久久久下田 | 午夜激情视频网站 | 久久精品无码一区二区三区 | 日日摸夜夜添夜夜添高潮视频 | 黄色片视频免费观看 | 精品久久久久久久久久久久久久 | 夜夜av | 青草精品 | 一区二区久久 | 日本在线免费观看 | 高清久久 | 久久久午夜爽爽一区二区三区三州 | 日韩一级精品视频在线观看 | 国产精品成人一区二区三区 | 91在线免费播放 | 一区亚洲 | 日韩资源 | 最近最新mv字幕免费观看 | 欧美 日韩 中文 | 久草久|