if(result==JFileChooser.APPROVE_OPTION) {
ftp.downloadFile(targetFile, fDialog.getSelectedFile().toString()); update();
status.setText(\下载文件\+targetFile+\成功\);
JOptionPane.showMessageDialog(null, \下载文件\+targetFile+\成功\);
time = new Date();
out.println(time+\下载文件:\+targetFile); } } else {
JOptionPane.showMessageDialog(null, \请选择所要下载的文件\); } }
//上一级目录
public void upDir() {
try {
ftp.up(); update(); }
catch(Exception e) {
e.printStackTrace(); } }
//下一级目录
public void downDir() {
if(!fileList.isSelectionEmpty()) {
String foldername=fileList.getSelectedValue().toString(); if(foldername.indexOf(\)==-1) {
ftp.setPath(foldername); update(); } else
{
JOptionPane.showMessageDialog(null, \请选择一个文件夹\); } } else {
JOptionPane.showMessageDialog(null, \请选择一个文件夹\); } }
//关闭连接
public void close(){ String s=ftp.close(); status.setText(s); m.removeAllElements();
time = new Date();
out.println(time+\离开服务器:\+url.getText()); out.close();
connectButton.setEnabled(true); closeButton.setEnabled(false); }
//显示日志信息
@SuppressWarnings(\) public void ShowLog() {
new LogDialog().show(); }
public static void main(String[] args) //throws Exception {
try {
UIManager.setLookAndFeel(\el\);//设置windows系统外观 }catch(Exception e) {
e.printStackTrace();//打印异常信息在程序中出错的位置及原因 }
new Ftp(); } }
(2) FtpBean.java
public class FtpBean {
private FtpClient ftpClient = null; //获取文件名列表
public List
List
InputStreamReader isr = null;// InputStreamReader 是字节流通向字符流的桥梁
BufferedReader br = null; try {
if(ftpClient!=null) {
isr = new
InputStreamReader(this.ftpClient.nameList(this.getDir())); br = new BufferedReader(isr); String fileName = \; while (true)
{ fileName = br.readLine(); if (fileName == null) {
break; }
fileNameList.add(fileName); } } }
finally {
if (br != null) {
try {
br.close(); }
catch (IOException e) {
e.printStackTrace(); } }
if (isr != null) {
try {
isr.close(); }
catch (IOException e) {
e.printStackTrace(); } } }
return fileNameList; }
//打开连接
public String connect(String dir, String hostname, int port, String username,String passwd) { String msg = \; try {
ftpClient = new FtpClient(hostname, port); ftpClient.login(username, passwd); ftpClient.binary();
msg = \登录主机成功!\; }
catch (FtpLoginException e) {
msg = \登录主机失败,可能是用户名密码错误!\; ftpClient=null; }
catch (IOException e) {
msg = \登录主机失败,请检验端口是否正确!\; ftpClient=null; }
catch (SecurityException e) {
msg = \无权连接主机,请确认是否有权限连接主机!\; ftpClient=null; }
return msg; }
//读取文件列表