stroll [名] [C] ぶらぶら歩き,散歩.

  スポンサーサイト

一定期間更新がないため広告を表示しています

- | permalink | - | - | ↑top
Clip!! | Ranking |

  FTPファイル送信サンプル

FTPファイル送信サンプル。
PASV(パッシブ)モードでの接続を試み、PASVモードが使用できない場合は、ACTIVE(アクティブ)モードでファイルを送信。
FtpSample.java
import java.io.*;
import java.net.*;
import java.util.*;

/**
* FTPファイル送信サンプル
* PASVモードでの接続を試み、PASVモードが使用できない場合は、
* ACTIVEモードでファイルを送信
*
* @version 1.0
*/
public class FtpSample {
private static final int CTRLPORT = 21; // ftpの制御用のポート
private static Socket ctrlSocket; // 制御用ソケット
private static PrintWriter ctrlOutput; // 制御出力用ストリーム
private static BufferedReader ctrlInput; // 制御入力用ストリーム
private static byte[] localHostAddress; // ローカルホストのアドレス

public static void main(String[] args) {
try {
String hostName = "hostName"; // ホスト名
String userName = "userName"; // ユーザ名
String password = "password"; // パスワード
String dirName = "dirName"; // ファイル転送先ディレクトリ
String fileName = "temp"; // 転送ファイル
String storFileName = "ftptest.txt"; // 転送後のファイル名

System.out.println("hostName=" + hostName);
System.out.println("loginName=" + userName);
System.out.println("password=" + password);
System.out.println("dirName=" + dirName);
System.out.println("fileName=" + fileName);
System.out.println("storFileName=" + storFileName);

// 接続
ctrlSocket = new Socket(hostName, CTRLPORT);
localHostAddress = ctrlSocket.getLocalAddress().getAddress();
ctrlOutput =
new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
ctrlSocket.getOutputStream(), "JIS")),
true);
ctrlInput =
new BufferedReader(
new InputStreamReader(ctrlSocket.getInputStream()));

String line = ctrlInput.readLine();
while (line.charAt(3) == '-') {
line = ctrlInput.readLine();
}

// ユーザー認証
ctrlOutput.println("USER " + userName);
ctrlOutput.flush();
line = ctrlInput.readLine();
while (line.charAt(3) == '-') {
System.out.println("USER line=" + line);
line = ctrlInput.readLine();
}

ctrlOutput.println("PASS " + password);
ctrlOutput.flush();
line = ctrlInput.readLine();
while (line.charAt(3) == '-') {
System.out.println("PASS line=" + line);
line = ctrlInput.readLine();
}

// 指定したディレクトリに移動
ctrlOutput.println("CWD " + dirName);
ctrlOutput.flush();
line = ctrlInput.readLine();
while (line.charAt(3) == '-') {
System.out.println("CWD line=" + line);
line = ctrlInput.readLine();
}

// バイナリモードを設定(アスキーモードの場合は'TYPE A')
ctrlOutput.println("TYPE I");
ctrlOutput.flush();
line = ctrlInput.readLine();
while (line.charAt(3) == '-') {
System.out.println("TYPE line=" + line);
line = ctrlInput.readLine();
}

// PASVモードテスト
ctrlOutput.println("PASV");
ctrlOutput.flush();
line = ctrlInput.readLine();
while (line.charAt(3) == '-') {
System.out.println("PASV line=" + line);
line = ctrlInput.readLine();
}

// PASVモード可の場合
if (Integer.parseInt(line.substring(0, 3)) == 227) {
System.out.println("PASVモード");
int port;
String ip;
int[] numbers = new int[6];
String temp;
StringTokenizer tempToken;
tempToken = new StringTokenizer(line, "(");
temp = tempToken.nextToken();
temp = tempToken.nextToken();
tempToken = new StringTokenizer(temp, ")");
temp = tempToken.nextToken();
tempToken = new StringTokenizer(temp, ",");
for (int i = 0; i < 6; i++) {
numbers[i] = Integer.parseInt(tempToken.nextToken());
}

port = (numbers[4] * 256) + numbers[5];
ip = numbers[0] + "." + numbers[1] + "."
+ numbers[2] + "." + numbers[3];

InetAddress hostAddress = InetAddress.getByName(ip);
byte[] address = hostAddress.getAddress();
Socket dataSocket = new Socket(hostAddress, port);
dataSocket.setSoTimeout(30000);
ctrlOutput.println("STOR " + storFileName);
ctrlOutput.flush();
line = ctrlInput.readLine();
while (line.charAt(3) == '-') {
System.out.println("STOR line=" + line);
line = ctrlInput.readLine();
}
System.out.println("STOR line=" + line);

int n;
int transferred = 0;
byte[] buff = new byte[1024];
DataOutputStream dataOutput =
new DataOutputStream(dataSocket.getOutputStream());
RandomAccessFile file = new RandomAccessFile(fileName, "r");
while ((n = file.read(buff)) > 0) {
dataOutput.write(buff, 0, n);
transferred += n;
}

file.close();
dataOutput.close();
dataSocket.close();
} else {
System.out.println("ACTIVEモード");
FileInputStream fis = new FileInputStream(fileName);
Socket dataSocket = dataConnection("STOR " + storFileName);
OutputStream outstr = dataSocket.getOutputStream();
int n;
byte[] buff = new byte[1024];
while ((n = fis.read(buff)) > 0) {
outstr.write(buff, 0, n);
}
dataSocket.close();
fis.close();
}

line = ctrlInput.readLine();
while (line.charAt(3) == '-') {
System.out.println("STOR line=" + line);
line = ctrlInput.readLine();
}
System.out.println("STOR line=" + line);

ctrlOutput.println("QUIT");
ctrlOutput.flush();
line = ctrlInput.readLine();
while (line.charAt(3) == '-') {
System.out.println("QUIT line=" + line);
line = ctrlInput.readLine();
}
System.out.println("QUIT line=" + line);

// 接続を閉じます
ctrlOutput.close();
ctrlInput.close();
ctrlSocket.close();
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* データ送受信用ソケットを取得
*
* @param ctrlcmd 
*/
private static Socket dataConnection(String ctrlcmd)
throws IOException, UnknownHostException {
String cmd = "PORT ";
ServerSocket serverDataSocket = new ServerSocket(0, 1);
for (int i = 0; i < 4; i++) {
cmd = cmd + (localHostAddress[i] & 0xff) + ",";
}
cmd += (((serverDataSocket.getLocalPort()) / 256) & 0xff)
+ "," + (serverDataSocket.getLocalPort() & 0xff);

ctrlOutput.println(cmd);
ctrlOutput.flush();
String line = ctrlInput.readLine();
while (line.charAt(3) == '-') {
System.out.println("PORT line=" + line);
line = ctrlInput.readLine();
}
System.out.println("PORT line=" + line);

ctrlOutput.println(ctrlcmd);
ctrlOutput.flush();
line = ctrlInput.readLine();
while (line.charAt(3) == '-') {
System.out.println("STOR line=" + line);
line = ctrlInput.readLine();
}

Socket dataSocket = serverDataSocket.accept();
serverDataSocket.close();
return dataSocket;
}
}
memo(Java) | permalink | comments(0) | trackbacks(0) | ↑top
Clip!! | Ranking |

  スポンサーサイト

- | permalink | - | - | ↑top
Clip!! | Ranking |

  Comment List

  Comment









  Trackback URL
 

  Trackback
Home  |  Selected Entries  |  Recent Comments  |  Recent Trackback  |  Categories  |  Archives  |  bbs