質問編集履歴
1
ソースコード 記入
test
CHANGED
File without changes
|
test
CHANGED
@@ -48,7 +48,251 @@
|
|
48
48
|
|
49
49
|
|
50
50
|
|
51
|
-
|
51
|
+
```Java
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
import java.io.FileInputStream;
|
58
|
+
|
59
|
+
import java.io.FileOutputStream;
|
60
|
+
|
61
|
+
import java.io.IOException;
|
62
|
+
|
63
|
+
import java.io.InputStream;
|
64
|
+
|
65
|
+
import java.io.OutputStream;
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
import org.apache.commons.net.ftp.*;
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
public class FtpClientHelper {
|
76
|
+
|
77
|
+
private static final int FTP_PORT = ポート番号;
|
78
|
+
|
79
|
+
private static final String LOCALHOST = "IPアドレス";
|
80
|
+
|
81
|
+
private static final String USER = "ID" ;
|
82
|
+
|
83
|
+
private static final String PASSWORD = "パスワード" ;
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
public static void main(String args[]){
|
88
|
+
|
89
|
+
test();
|
90
|
+
|
91
|
+
}
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
public static void test() {
|
98
|
+
|
99
|
+
try {
|
100
|
+
|
101
|
+
//ファイルアップロード
|
102
|
+
|
103
|
+
FileInputStream fis = new FileInputStream("C:\\Users\\ユーザフォルダ\\Desktop\\新しいテキスト ドキュメント.txt");
|
104
|
+
|
105
|
+
FTPClass.sendFile("remoteFilename", fis);
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
//ファイルダウンロード
|
110
|
+
|
111
|
+
//FileOutputStream fos = new FileOutputStream("downloadfile.txt");
|
112
|
+
|
113
|
+
//FTPClass.retrieveFile("/data/downloadfile", fos);
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
} catch (Exception e) {
|
118
|
+
|
119
|
+
e.printStackTrace();
|
120
|
+
|
121
|
+
}
|
122
|
+
|
123
|
+
}
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
static class FTPClass {
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
//ファイルアップロード
|
136
|
+
|
137
|
+
public static void sendFile (String remoteFilename, InputStream is ) throws Exception {
|
138
|
+
|
139
|
+
FTPClient ftpclient = new FTPClient();
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
try {
|
144
|
+
|
145
|
+
//指定するホスト、ポートに接続します
|
146
|
+
|
147
|
+
ftpclient.connect(LOCALHOST, FTP_PORT);
|
148
|
+
|
149
|
+
// System.out.println("とおった");
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
// コネクト
|
154
|
+
|
155
|
+
if (!FTPReply.isPositiveCompletion(ftpclient.getReplyCode())) {
|
156
|
+
|
157
|
+
Exception ee = new Exception("Can't Connect to :" + LOCALHOST);
|
158
|
+
|
159
|
+
throw ee;
|
160
|
+
|
161
|
+
}
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
//ログイン
|
166
|
+
|
167
|
+
if (ftpclient.login(USER, PASSWORD) == false) {
|
168
|
+
|
169
|
+
Exception ee = new Exception("Invalid user/password");
|
170
|
+
|
171
|
+
throw ee;
|
172
|
+
|
173
|
+
}
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
//ファイル転送モード設定
|
178
|
+
|
179
|
+
ftpclient.setFileType(FTPClient.BINARY_FILE_TYPE);
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
//ファイル転送
|
184
|
+
|
185
|
+
ftpclient.storeFile(remoteFilename, is);
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
} catch (IOException e) {
|
190
|
+
|
191
|
+
throw e;
|
192
|
+
|
193
|
+
} finally {
|
194
|
+
|
195
|
+
try {
|
196
|
+
|
197
|
+
ftpclient.disconnect(); //接続解除
|
198
|
+
|
199
|
+
} catch (IOException e) {
|
200
|
+
|
201
|
+
}
|
202
|
+
|
203
|
+
}
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
}
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
//ファイルダウンロード
|
216
|
+
|
217
|
+
public static void retrieveFile(String remoteFilename, OutputStream os) throws Exception {
|
218
|
+
|
219
|
+
FTPClient ftpclient = new FTPClient();
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
try {
|
224
|
+
|
225
|
+
//指定するホスト、ポートに接続します
|
226
|
+
|
227
|
+
ftpclient.connect(LOCALHOST, FTP_PORT);
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
// コネクト
|
232
|
+
|
233
|
+
if (!FTPReply.isPositiveCompletion(ftpclient.getReplyCode())) {
|
234
|
+
|
235
|
+
Exception ee = new Exception("Can't Connect to :" + LOCALHOST);
|
236
|
+
|
237
|
+
throw ee;
|
238
|
+
|
239
|
+
}
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
//ログイン
|
244
|
+
|
245
|
+
if (ftpclient.login(USER, PASSWORD) == false) {
|
246
|
+
|
247
|
+
Exception ee = new Exception("Invalid user/password");
|
248
|
+
|
249
|
+
throw ee;
|
250
|
+
|
251
|
+
}
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
//ファイル転送モード設定
|
256
|
+
|
257
|
+
ftpclient.setFileType(FTPClient.BINARY_FILE_TYPE);
|
258
|
+
|
259
|
+
|
260
|
+
|
261
|
+
// ファイル受信
|
262
|
+
|
263
|
+
ftpclient.retrieveFile(remoteFilename, os);
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
} catch (IOException e) {
|
268
|
+
|
269
|
+
throw e;
|
270
|
+
|
271
|
+
} finally {
|
272
|
+
|
273
|
+
try {
|
274
|
+
|
275
|
+
ftpclient.disconnect(); //接続解除
|
276
|
+
|
277
|
+
} catch (IOException e) {
|
278
|
+
|
279
|
+
}
|
280
|
+
|
281
|
+
}
|
282
|
+
|
283
|
+
}
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
}
|
290
|
+
|
291
|
+
}
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
```
|
52
296
|
|
53
297
|
|
54
298
|
|