javaの業務で、ファイルを他サーバーに転送しなくちゃなのですが、できなくて困っています。
転送先のサーバがFTPはFTPSのみ受け付けている、という状態です。
大変困っています。ご回答お待ちしています。よろしくお願い致します。
以下のようなコードを書きましたが、
ハンドシェイクに失敗します。
code
1 Socket ctrlSocket; 2 PrintWriter ctrlOutput; 3 BufferedReader ctrlInput; 4 byte[] localHostAddress; 5 char[] inbuffer; 6 int i; 7 String sendCommand; 8 try { 9 // FTPサーバーへ接続します 10 ctrlSocket = new Socket(host, port); 11 localHostAddress = ctrlSocket.getLocalAddress().getAddress(); 12 ctrlOutput = new PrintWriter(ctrlSocket.getOutputStream()); 13 ctrlInput = new BufferedReader(new InputStreamReader(ctrlSocket.getInputStream())); 14 inbuffer = new char[1000]; 15 i = ctrlInput.read(inbuffer); 16 17 // 暗号化する場合(FTPS) 18 if (isEncrypt) { 19 sendCommand = "AUTH TLS"; 20 ctrlOutput.println(sendCommand); 21 ctrlOutput.flush(); 22 inbuffer = new char[1000]; 23 i = ctrlInput.read(inbuffer); 24 25★ SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault(); 26 ctrlSocket = (SSLSocket) factory.createSocket(ctrlSocket, host, port, true); 27 28☆ ((SSLSocket)ctrlSocket).startHandshake(); 29 30 sendCommand = "PBSZ 0"; 31 ctrlOutput.println(sendCommand); 32 ctrlOutput.flush(); 33 inbuffer = new char[1000]; 34 i = ctrlInput.read(inbuffer); 35 36 sendCommand = "PROT P"; 37 ctrlOutput.println(sendCommand); 38 ctrlOutput.flush(); 39 inbuffer = new char[1000]; 40 i = ctrlInput.read(inbuffer); 41 } 42 43 if (isPasv) { 44 sendCommand = "PASV"; 45 ctrlOutput.println(sendCommand); 46 ctrlOutput.flush(); 47 } 48 49 // ユーザー認証します 50 sendCommand = "USER " + userName; 51 ctrlOutput.println(sendCommand); 52 ctrlOutput.flush(); 53 inbuffer = new char[1000]; 54 i = ctrlInput.read(inbuffer); 55 sendCommand = "PASS " + password; 56 ctrlOutput.println(sendCommand); 57 ctrlOutput.flush(); 58 inbuffer = new char[1000]; 59 i = ctrlInput.read(inbuffer); 60 61 // 指定したディレクトリに移動します 62 sendCommand = "CWD /"; 63 ctrlOutput.println(sendCommand); 64 ctrlOutput.flush(); 65 inbuffer = new char[1000]; 66 i = ctrlInput.read(inbuffer); 67 68 // バイナリモードに変更します 69 sendCommand = "TYPE I"; 70 ctrlOutput.println(sendCommand); 71 ctrlOutput.flush(); 72 inbuffer = new char[1000]; 73 i = ctrlInput.read(inbuffer); 74 75 // アップロードします 76 Socket dataSocket = dataConnection("STOR " + uploadPath); 77 OutputStream outstr = dataSocket.getOutputStream(); 78 79 byte[] buf = readFileToByte(inputPath); 80 outstr.write(buf); 81 outstr.flush(); 82 dataSocket.close(); 83 inbuffer = new char[1000]; 84 i = ctrlInput.read(inbuffer); 85 86 // 終了 87 sendCommand = "QUIT"; 88 ctrlOutput.println(sendCommand); 89 ctrlOutput.flush(); 90 inbuffer = new char[1000]; 91 i = ctrlInput.read(inbuffer); 92 93 以下略
【状況】
★factoryの中身
c53d99[SSL_NULL_WITH_NULL_NULL: Socket[addr=ホスト/IPアドレス,port=21,localport=58782]]
☆を実行したあとExceptionが発生します
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
※FTPSではない、暗号化してないFTP通信ではファイル転送に成功しています。
通信相手はショッピングモールの「ポンパレモール」の店舗向けFTPサーバになります。
また、FTPソフトの「winSCP」や「FFFTP」で接続したところ問題なくファイル転送ができました。
WireSharkで上記プログラム実行中FTPのパケットモニタリングしてみたところ、
RES 220 FTP
REQ AUTH TLS
RES 234 AUTH TLS successful
REQ \200e\001\003 …
RES \026\003\001 …
RES \275\036C\207K …
REQ \025\003\001 …
RES 550 TLS handshake failed
という結果になりました。
【質問】
☆の位置でハンドシェイクに失敗していまいます。
FTPSでファイル転送をする場合
・コマンド送信の順番
・SSLSocketの作成手順
に間違いがあるのかもしれませんが、わからなくて困っています。
また、ハンドシェイクに成功したあとは「PBSZ 0」「PROT P」というコマンドを送信し、
FTPSではない、FTPでのファイル転送と同じ手順で、ファイル転送ができるのでしょうか?
具体的なコードを交えて教えていただけると大変助かります。
よろしくお願い致します。m(_ _)m

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/12/10 04:26