我目前正在開發一個基於commons.net包的ftp客戶端,以便運行測試來查看連接的速度。 基本上我的FTP測試包括在連接到服務器,登錄到它,然後開始下載的週期/根據需要上傳長,直到用戶決定通過一個按鈕來阻止它,那麼當前的週期將結束,因此將考試。如何用超時機制中斷正在進行的任務?
但是,在運行這些測試時,出現了需要定時機制的情況。服務器正在傳輸文件,並在確實完成之前發送返回碼226(傳輸完成)。
所以我的線程仍然卡住,試圖清空inputStream,當它不可能了。
我的想法是先從下載過程的螺紋計時器,這將在每次傳輸一個字節到我的客戶進行復位。 發生超時時,會產生一個異常,我的客戶會對它做出反應,並記錄下載。
我已經閱讀並嘗試了很多解決方案,其中包括: - 從線程引發異常 - >線程捕獲異常而不是客戶端; - 從線程中斷客戶端,所以客戶端引發一箇中斷異常 - >似乎不工作; - 使用具有超時執行器 - >因爲我無法知道下載的「正常」持續時間,所以當我啓動任務時,我無法將其交給執行器,而且,當我接收數據。
我看了很多論壇上很多關於它,並沒有發現,似乎能夠適應任何的解決方案和工作在這種情況下。如果有人有另一種方法來做到這一點的想法?
這是我的表演動作的代碼:
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.");
}
}
謝謝您提前如果任何人都可以幫助,如果您需要關於我的實際代碼的更多信息,我可以在這裏放更多信息。
猜猜你沒有直接使用InputStream API,否則你可以做類似in.available()的事情?然後讀取,否則等待一段時間,如果仍然沒有數據可用,假設連接丟失。 – 2012-04-20 09:50:32
Thread.interrupt()應該中斷你的線程,並且應該向上拋出異常,除非坐在中間的一些貪婪代碼正在吃它。 – 2012-04-20 09:51:30