FTPClient:sun.net.ftp.FtpClient由于种种原因,上传和下载均有失败的可能性,并且这种情况我也遇到了。我用Timer来定时下载文件,当循环次数多了之后,连接就会出现问题: public void download1(String remoteFile, String localFile,
JTextArea textarea) {
try {
TelnetInputStream is = ftpClient.get(remoteFile);
java.io.File file_in = new java.io.File(localFile);
FileOutputStream os = new FileOutputStream(file_in);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
// System.out.println((char)is.read());
// System.out.println(file_in);
os.write(bytes, 0, c);
}
os.close();
is.close();
} catch (IOException ex) {
ex.printStackTrace();
textarea.append(Down.getDate() + " 验证更新失败,正在尝试重新运行程序... \n");
textarea.setCaretPosition(textarea.getText().length());
Upload_DownloadFTP upload_DownloadFTP = new Upload_DownloadFTP();
upload_DownloadFTP.download1(remoteFile, localFile, textarea);
Down d = new Down();
d.timer.cancel();
d.begin(textarea, Test.defaultSavepath);
}
}这个是我下载文件的方法。当出现异常时,这里能够捕获到这个异常,也能够显示“更新失败,正在尝试”。但是再往下程序就停住了。注意:“download1”方法在“Upload_DownloadFTP”这个类中。下面是我开始计时器的方法: public void begin(JTextArea textarea, String str) {
this.timer = new Timer();
this.timer.schedule(new MyTask(textarea, str), 1000, 100 * 60 * 15);
}因为在这里,我每次都要new一个timer对象出来,所以不存在Timer is already calceled这个错误,至少本人是这样认为。
那么为什么我写在catch里的语句就不会执行呢?提示到“验证更新失败,正在尝试重新运行程序...”就不执行了?
JTextArea textarea) {
try {
TelnetInputStream is = ftpClient.get(remoteFile);
java.io.File file_in = new java.io.File(localFile);
FileOutputStream os = new FileOutputStream(file_in);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
// System.out.println((char)is.read());
// System.out.println(file_in);
os.write(bytes, 0, c);
}
os.close();
is.close();
} catch (IOException ex) {
ex.printStackTrace();
textarea.append(Down.getDate() + " 验证更新失败,正在尝试重新运行程序... \n");
textarea.setCaretPosition(textarea.getText().length());
Upload_DownloadFTP upload_DownloadFTP = new Upload_DownloadFTP();
upload_DownloadFTP.download1(remoteFile, localFile, textarea);
Down d = new Down();
d.timer.cancel();
d.begin(textarea, Test.defaultSavepath);
}
}这个是我下载文件的方法。当出现异常时,这里能够捕获到这个异常,也能够显示“更新失败,正在尝试”。但是再往下程序就停住了。注意:“download1”方法在“Upload_DownloadFTP”这个类中。下面是我开始计时器的方法: public void begin(JTextArea textarea, String str) {
this.timer = new Timer();
this.timer.schedule(new MyTask(textarea, str), 1000, 100 * 60 * 15);
}因为在这里,我每次都要new一个timer对象出来,所以不存在Timer is already calceled这个错误,至少本人是这样认为。
那么为什么我写在catch里的语句就不会执行呢?提示到“验证更新失败,正在尝试重新运行程序...”就不执行了?
解决方案 »
- 如何用tokenizer抽取一个string中的数字?新手跪求问题!!
- java set小问题,在线等
- 关于Jtable行选中事件的疑问
- 立信笔试题,求助。
- Java中如何在两个类对象之间传递参数?
- 初学java,按书上所写的敲了代码, 编译也能通过!!可为什么出不来图形呢? 在线等!~~
- 如何用Java生成指定像素的BMP?
- 不用jdbc-odbc连接桥,怎样连接SQL SERVER 数据库
- ios的ZSSRichTextEditor-master。。android版本谁移植个嘛
- Java中LinkedList中的getFirst()方法和element()方法有什么区别?
- java 基础问题
- xml解析
String password, String path) {
try {
if (ftpClient != null) {
ftpClient.closeServer();
ftpClient = null;
}
ftpClient = new FtpClient();
setConnectTimeOut(5000);
ftpClient.openServer(ip, port);
ftpClient.login(user, password);
if (path.length() != 0)
ftpClient.cd(path);
ftpClient.binary();
} catch (IOException ex) {
System.out.println(ex);
}
} public void closeConnect1() {
try {
if (ftpClient != null) {
ftpClient.closeServer();
}
} catch (IOException ex) {
System.out.println("not disconnect");
System.out.println(ex);
} finally {
ftpClient = null;
}
}
愁死我了!
TelnetInputStream is = ftpClient.get(remoteFile);
java.io.File file_in = new java.io.File(localFile);
FileOutputStream os = new FileOutputStream(file_in);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
// System.out.println((char)is.read());
// System.out.println(file_in);
os.write(bytes, 0, c);
}
os.close();
is.close();
} catch (IOException ex) {
ex.printStackTrace();
textarea.append(Down.getDate() + " 验证更新失败,正在尝试重新运行程序... \n");
textarea.setCaretPosition(textarea.getText().length());
Upload_DownloadFTP upload_DownloadFTP = new Upload_DownloadFTP();
upload_DownloadFTP.download1(remoteFile, localFile, textarea);
Down d = new Down();
d.timer.cancel();
d.begin(textarea, Test.defaultSavepath);
}
========如果在os 和 is close 之前出现异常,那么 os /is 都不会close ,,
public void download1(String remoteFile, String localFile,
JTextArea textarea) {
FileOutputStream os = null;
TelnetInputStream is = null;
try {
is = ftpClient.get(remoteFile);
java.io.File file_in = new java.io.File(localFile);
os = new FileOutputStream(file_in);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
// System.out.println((char)is.read());
// System.out.println(file_in);
os.write(bytes, 0, c);
}
} catch (IOException ex) {
ex.printStackTrace();
textarea.append(Down.getDate() + " 验证更新失败,正在尝试重新运行程序... \n");
textarea.setCaretPosition(textarea.getText().length());
Upload_DownloadFTP upload_DownloadFTP = new Upload_DownloadFTP();
upload_DownloadFTP.download1(remoteFile, localFile, textarea);
Down d = new Down();
d.timer.cancel();
d.begin(textarea, Test.defaultSavepath);
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}照您说的改了就成了这样了。(我不喜欢用throws)