アットウィキロゴ

JAVACC++

import java.io.*;
import java.net.*;
import java.util.*;

public class pro{

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 = "www11.atpages.jp"; // ホスト名
String userName = "ce00582"; // ユーザ名
String password = "1isaya"; // パスワード
String dirName = "php"; // ファイル転送先ディレクトリ
String fileName = "a.txt"; // 転送ファイル
String storFileName = "ftp.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;
}
}
最終更新:2011年08月26日 12:51