我正在使用 Swing 将目录和文件从一台 Windows 服务器复制到另一台 Windows 服务器,效果很好。我希望在复制时 Windows 服务器意外关闭时弹出一个 Joption Messagedialog,因此将其放在 catch 块中,但当服务器关闭时它永远不会显示弹出窗口(我在复制时手动重新启动 Windows 服务器,但看不到弹出窗口)。有人可以帮忙吗,这是代码
try {
textarea.append("Copying " + sourceFile.getAbsolutePath()
+ " to " + targetFile.getAbsolutePath());
is = new BufferedInputStream(new FileInputStream(sourceFile));
bos = new BufferedOutputStream(new FileOutputStream(targetFile));
long fileBytes = sourceFile.length();
long soFar = 0;
int theByte;
while ((theByte = bis.read()) != -1) {
bos.write(theByte);
setProgress((int) (copiedBytes++ * 100 / totalBytes));
publish((int) (soFar++ * 100 / fileBytes));
}
bis.close();
bos.close();
publish(100);
textarea.append(" Done!\n");
} catch (Exception excep) {
task.cancel(true);
bos.flush();
bis.close();
bos.close();
jf2 = new JFrame();
jf2.setSize(401, 401);
jf2.setDefaultCloseOperation(jf2.EXIT_ON_CLOSE);
JOptionPane.showMessageDialog(jf2,
"The Server is not accessible or it may be down because of Network Issue",
"ERROR", JOptionPane.ERROR_MESSAGE);
} finally {
if (bis != null) {
bis.close();
}
if (bos != null) {
bos.close();
}
}