HOME       LIST

【JAVA】FTP UPLOAD SAMPLE

FTP Serverに接続して指定ファイルを FTP Serverに Upload する サンプルコードです。

時間が無くて出来ないのではない。やる気がないからそんな言い訳を言うのだ。 - 2016/07/11(WEB)

Her handwriting is hard to read.(彼女の字は読みづらいです)


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 = "FTP SERVER ADDRESS or DOMAIN Name";	// ホスト名
			String userName = "user";	// ユーザ名
			String password = "password";	// パスワード
			String dirName = "/";	// ファイル転送先ディレクトリ
			String fileName = "send filename";	// 転送ファイル
			String storFileName = "save filename";	// 転送後のファイル名

			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;
	}
}

以前コンテンツ:【JAVA】MSSQLServer 接続サンプル
次のコンテンツ:【JAVA】MDBへのODBC接続サンプル01(データソース(ODBC)設定無し)



Copyright(c) 2007-2020 dojeun.com All Rights Reserved.

Valid XHTML 1.0 Transitional