現在、テストを実行して接続速度を確認するために、パッケージ commons.net に基づいた ftp クライアントを開発しています。基本的に私のftpテストは、サーバーに接続してログオンし、必要なだけダウンロード/アップロードのサイクルを開始することで構成され、ユーザーがボタンで停止することを決定すると、現在のサイクルが終了し、テスト。
ただし、これらのテストの実行中に、タイムアウト メカニズムが必要な状況が発生しました。サーバーはファイルを送信していて、実際に完了する前にリターン コード 226 (転送完了) を送信しました。
そのため、スレッドがスタックしたままになり、それが不可能になったときに inputStream を空にしようとします。
私のアイデアは、バイトがクライアントに転送されるたびにリセットされる、ダウンロードプロセスでスレッド化されたタイマーを開始することでした。タイムアウトが発生すると、例外などが発生し、クライアントはそれに反応してダウンロードを中止します。
私はその中の多くの解決策を読んで試しました:-スレッドから例外を発生させる->スレッドは例外をキャッチし、クライアントはキャッチしません。- スレッドからクライアントに割り込むため、クライアントは自分自身に interruptedException を発生させます -> 動作していないようです。- タイムアウトのあるエグゼキューターを使用する -> ダウンロードの「通常の」期間がわからないため、タスクを開始するときにエグゼキューターに与えることができません。さらに、受信時にタイマーをリセットする必要がありますデータ。
私は多くのフォーラムでそれについて多くのことを読みましたが、この場合に適応しているように見え、機能する解決策は見つかりませんでした. 誰かがそれを行う別の方法を考えているなら?
これは私が実行しているアクションのコードです:
public double get(String fileName) {
[...]
org.apache.commons.net.io.Util.copyStream(stO,stD,client.getBufferSize(),
this.localSize,
new org.apache.commons.net.io.CopyStreamAdapter() {
public void bytesTransferred(long totalBytesTransferred,
int bytesTransferred,long streamSize) {
setProgressDL(totalBytesTransferred);
//reset the timer here
}
});
[...]
}
クライアントを起動するテストのコードの一部を次に示します。
public class TestFtp extends Thread {
[...]
public void run() {
System.out.println("Launching FTP test");
FtpClient client = new FtpClient(this.model, this, this.model.getFtpServer());
try {
//Attempting connection on the server
client.connect();
try {
// Attempting login
client.login(this.model.getUsername(), this.model.getPassword());
do {
client.changeDirectory("get");
// start timer
client.get(this.model.getDistantFileName());
// stop timer
client.changeToParentDirectory();
client.changeDirectory("put");
client.set(this.model.getDistantFileName(),
this.model.getNewFileName());
client.changeToParentDirectory();
try {
// Little pause between each test
Thread.sleep(500);
} catch (InterruptedException e) {}
// Continue test until the user stops it
} while (this.continuous);
// Once the test is over, logout
client.logout();
} catch (FTPLoginException e) {
// If login fails, the test ends
System.out.println("Unable to login to the server.");
}
} catch (FTPConnectException e) {
// If connection error, the test ends
System.out.println("Unable to connect to the server.");
}
}
誰かが助けてくれれば、事前に感謝します。私の実際のコードについてさらに情報が必要な場合は、ここにもっと多くの情報を入れることができます.