首页>代码>Java swing实现Ftp客户端工具(MVC模式)>/FtpClient/src/com/exercise/controller/Controller.java
package com.exercise.controller;

import com.exercise.model.bean.FileBean;
import com.exercise.model.Model;
import com.exercise.controller.utils.FileUtil;
import com.exercise.controller.utils.FtpUtil;
import com.exercise.controller.utils.StringUtil;
import com.exercise.model.bean.FtpInfoBean;
import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Vector;
import javax.swing.filechooser.FileSystemView;

/*
 *  说明:程序控制器类,包含视图需要调用的方法
 */
public class Controller {

    private Model model; //程序模型实例

    //构造方法
    public Controller(Model model) {
        this.model = model;
    }

    //刷新本地文件列表
    public void refreshLocalFiles(String path) {
        FileSystemView fsv = FileSystemView.getFileSystemView();
        //如果path为空,就初始化
        if (StringUtil.isEmpty(path)) {
            path = fsv.getDefaultDirectory().getPath();  // 我的文档默认路径
        }
        Vector<FileBean> folderData = new Vector<FileBean>();//文件夹数据
        Vector<FileBean> fileData = new Vector<FileBean>();//文件数据
        File[] files = fsv.getFiles(new File(path), true);
        for (int i = 0; i < files.length; i++) {
            File file = files[i];
            FileBean fb = new FileBean();
            fb.setName(file.getName());
            fb.setPath(file.getAbsolutePath());
            fb.setSize(file.length());
            fb.setLastModified(new Date(file.lastModified()));
            if (file.isDirectory()) {
                fb.setType(FileBean.FileType.folder);
                folderData.add(fb);
            } else {
                fb.setType(FileBean.FileType.file);
                fileData.add(fb);
            }
        }
        //所有文件夹,文件数据
        Vector<FileBean> listData = new Vector<FileBean>();
        listData.addAll(folderData);
        listData.addAll(fileData);
        //设置模型中的本地文件数据   
        model.setLocalFiles(listData);
        model.setLocalPath(path);
    }
    //本地文件夹向上按钮事件处理

    public void localUpperButtonClickHandler() {
        String currentPath = model.getLocalPath();
        String separator = File.separator;
        int pos = currentPath.lastIndexOf(separator);
        if (pos > 0 && pos != currentPath.length()) {
            currentPath = currentPath.substring(0, pos);
//            System.out.println("路径:" + currentPath);
            refreshLocalFiles(currentPath);
        } else {
            Vector<FileBean> listData = new Vector<FileBean>();//所有文件夹,文件数据
            List<String> roots = FileUtil.getFileRoots();
            for (int i = 0; i < roots.size(); i++) {
                FileBean fb = new FileBean();
                String driveName = roots.get(i).replace(File.separator, "");
                fb.setName(driveName);
                fb.setType(FileBean.FileType.folder);
                fb.setPath(driveName);
                listData.add(fb);
            }
            model.setLocalFiles(listData);//设置list数据      
            model.setLocalPath("/");//更新路径 localPathText                          
        }
    }

    //上传文件,成功返回true.如果未连接或上传失败,返回false
    public String uploadLocalFile(FileBean fb) {
        if (model.getFtpInfoBean() == null) {
            return "尚未连接服务器!";
        }
        FtpInfoBean ftpInfoBean = model.getFtpInfoBean();
        String localFileName = fb.getPath();
        String remoteFileName = model.getRemotePath();
        FtpUtil.uploadFile(ftpInfoBean, fb, localFileName, fb.getName());//直接覆盖
        model.setLastLogInfo("信息: 上传文件" + fb.getPath() + "完成,大小" + fb.getSize() + "字节");
        refreshRemoteFiles();
        return "OK";
    }
    //打开文件夹

    public void openLocalFolder(FileBean fb) {
        refreshLocalFiles(fb.getPath());
    }
    //刷新本地文件

    public void refreshLocalButtonClickHandler() {
        refreshLocalFiles(model.getLocalPath());
    }
    //初始化服务器侧 JList 

    public void initRemoteFiles() {
        Vector<FileBean> listData = new Vector<FileBean>();
        FileBean fb = new FileBean();
        fb.setName("无连接...");
        fb.setType(FileBean.FileType.info);
        listData.add(fb);
        model.setRemoteFiles(listData);//设置list数据
        model.setRemotePath(".");
    }
    //登陆ftp服务器,获得服务器文件列表

    public void loginRemoteServer(FtpInfoBean ftpInfoBean) {
        //如果测试连接成功
        if (FtpUtil.isValid(ftpInfoBean)) {
            model.setFtpInfoBean(ftpInfoBean);//保存ftp登录信息到模型中
            Vector<FileBean> listData = FtpUtil.getTrimedFileList(ftpInfoBean, null);//服务器文件列表
            model.setLastLogInfo("信息: 连接服务器[" + ftpInfoBean.getIp() + "]完成");
            model.setRemoteFiles(listData);//更新模型数据  
            model.setRemotePath(".");
        } //如果测试连接不成功,更新JList数据
        else {
            Vector<FileBean> listData = new Vector<FileBean>();
            FileBean fb = new FileBean();
            fb.setName("连接失败...");
            fb.setType(FileBean.FileType.info);
            listData.add(fb);
            model.setRemoteFiles(listData);
        }
    }
    //刷新服务器文件列表

    public void refreshRemoteFiles() {
        if (model.getFtpInfoBean() != null) {
            String remotePath = model.getRemotePath();
            Vector<FileBean> listData = FtpUtil.getTrimedFileList(model.getFtpInfoBean(), remotePath);
            model.setRemoteFiles(listData);
        }
    }
    //服务器 JList 鼠标双击的事件处理方法

    public void remoteFileDoubleClickedHandler(FileBean fb) {
        if (fb.getType().equals(FileBean.FileType.file)) {
            downloadRemoteFile(fb);//下载文件到本地            
        } else if (fb.getType().equals(FileBean.FileType.folder)) {
            openRemoteFolder(fb);//打开文件夹
        }
    }
    //下载文件到本地

    private void downloadRemoteFile(FileBean fb) {
        FtpInfoBean ftpInfoBean = model.getFtpInfoBean();
        String localPath = model.getLocalPath();
        FtpUtil.downloadFile(ftpInfoBean, localPath, fb);
        model.setLastLogInfo("信息: 下载文件" + fb.getName() + "完成,大小" + fb.getSize() + "字节");
        refreshLocalFiles(localPath);
    }
    //打开服务器文件夹

    private void openRemoteFolder(FileBean fb) {
        String separator = "/";
        String remotePath = fb.getPath();
        if (remotePath.equals(separator)) {
            remotePath = remotePath + fb.getName();
        } else {
            remotePath = fb.getPath() + separator + fb.getName();
        }
        Vector<FileBean> listData = FtpUtil.getTrimedFileList(model.getFtpInfoBean(), remotePath);
        model.setRemoteFiles(listData);
        model.setRemotePath(remotePath);
    }
    //服务器文件路径向上

    public void remoteUpperButtonClickHandler() {
        String remotePath = model.getRemotePath();
        String separator = "/";
        int pos = remotePath.lastIndexOf(separator);
        if (pos > 0) {
            remotePath = remotePath.substring(0, pos);
        } else {
            remotePath = remotePath.substring(0, pos + 1);
        }
        Vector<FileBean> listData = FtpUtil.getTrimedFileList(model.getFtpInfoBean(), remotePath);
        model.setRemoteFiles(listData);
        model.setRemotePath(remotePath);
    }

    //断开连接
    public void remoteDisconnectButtonClickHandler() {
        if (model.getFtpInfoBean() != null) {
            FtpUtil.disconnectFromServer();
            model.setLastLogInfo("信息: 断开服务器[" + model.getFtpInfoBean().getIp() + "]完成");
            model.setFtpInfoBean(null);
            initRemoteFiles();
        }
    }
    //删除服务器端文件

    public void deleteSeletedRemoteFile(FileBean fb) {
        if (model.getFtpInfoBean() != null) {
            FtpInfoBean ftpInfoBean = model.getFtpInfoBean();
            FtpUtil.deleteFile(ftpInfoBean, fb);
//            model.setLastLogInfo("信息: 删除服务器文件"+fb.getName()+"完成,大小"+fb.getSize()+"字节");
            refreshRemoteFiles();
        }

    }
}
最近下载更多
zengxq056  LV2 2022年10月3日
annazhang  LV29 2021年7月24日
Little already  LV3 2021年6月22日
lilinfeng  LV1 2021年5月25日
冬日  LV1 2021年5月21日
陈学文  LV1 2021年4月30日
2469095052  LV8 2020年12月14日
guaixia163  LV13 2020年11月11日
675104182  LV14 2020年9月22日
小小滑头鱼  LV26 2020年7月15日
最近浏览更多
手工咖啡  LV2 2024年6月17日
kevin_2023  LV1 2023年10月26日
yangxb2  LV10 2023年7月11日
as365049954  LV2 2022年10月16日
zengxq056  LV2 2022年10月3日
1576765285  LV1 2022年9月13日
qwqw900619  LV4 2022年9月7日
哎呀马吖  LV6 2022年8月25日
qsyqsy 2022年6月2日
暂无贡献等级
sunfanlin  LV2 2022年5月18日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友