ftp文件上传

pom依赖引入

<dependency>
     <groupId>commons-net</groupId>
     <artifactId>commons-net</artifactId>
     <version>3.3</version>
</dependency>

 实现代码

@Service
public class FtpServiceImpl {
    @Value("${ftp_host}")
    private String ftpHost;   //ftp服务器地址
    @Value("${ftp_port}")
    private int ftpPort;    //ftp服务器端口
    @Value("${ftp_username}")
    private String ftpUserName;    //ftp服务器用户名
    @Value("${ftp_password}")
    private String ftpPassword;    //ftp服务器用户密码

    public FTPClient ftpClient = null;

    /**
     * 连接ftp服务器
     * @return
     * @throws SocketException
     * @throws IOException
     */
    public FTPClient getFTPClient() throws SocketException, IOException {
        ftpClient = new FTPClient();
        ftpClient.setControlEncoding("utf-8");
        ftpClient.setDataTimeout(60000);       //设置传输超时时间为60秒
        ftpClient.setConnectTimeout(60000);       //连接超时为60秒
        try {
            ftpClient.connect(ftpHost, ftpPort);// 连接FTP服务器
            ftpClient.login(ftpUserName, ftpPassword);// 登陆FTP服务器
            if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
                System.out.println("未连接到FTP,用户名或密码错误。");
                ftpClient.disconnect();
            }
        } catch (SocketException e) {
            e.printStackTrace();
            throw new SocketException("FTP的IP地址可能错误,请正确配置。");
        } catch (IOException e) {
            e.printStackTrace();
            throw new SocketException("FTP的端口错误,请正确配置。");
        }
        return ftpClient;
    }


    /**
     * 上传文件
     *
     * @param pathName       ftp服务保存地址
     * @param fileName       上传到ftp的文件名
     * @param originfilename 待上传文件的名称(绝对地址) *
     * @return
     */
    public boolean uploadFile(String pathName, String fileName, String originfilename) throws Exception {
        InputStream inputStream = new FileInputStream(new File(originfilename));
        return uploadFile(pathName, fileName, inputStream);
    }

    /**
     * 上传文件流
     *
     * @param realPathName ftp服务保存地址
     * @param uuidName     上传到ftp的文件名
     * @param inputStream  输入文件流
     * @return
     */
    public boolean uploadFile(String realPathName, String uuidName, InputStream inputStream) throws Exception {
        boolean flag = false;
        try {
            this.ftpClient = getFTPClient();
            ftpClient.setControlEncoding("UTF-8");
            ftpClient.enterLocalPassiveMode();  //切换被动模式
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            createDirecroty(realPathName);
            ftpClient.makeDirectory(realPathName);
            ftpClient.changeWorkingDirectory(realPathName);
            ftpClient.storeFile(uuidName, inputStream);
            inputStream.close();
            ftpClient.logout();
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("上传文件失败");
        } finally {
            if (ftpClient.isConnected()) {
                try {
                    ftpClient.disconnect();
                } catch (IOException e) {
                    e.printStackTrace();
                    throw new Exception("上传文件失败");
                }
            }

        }
        return flag;
    }

    /**
     * 写入字符串文本
     * @param realPathName  ftp服务保存地址
     * @param fileName    ftp文件名
     * @param content    上传文本内容
     * @throws Exception
     */
    public void writeInto(String realPathName,String fileName,String content) throws Exception {
        System.out.println("ftp文件写入");
        try {
            this.ftpClient = getFTPClient();
            ftpClient.setControlEncoding("UTF-8");
            ftpClient.enterLocalPassiveMode();
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            createDirecroty(realPathName);
            ftpClient.makeDirectory(realPathName);
            ftpClient.changeWorkingDirectory(realPathName);
            PrintWriter pw = new  PrintWriter(new OutputStreamWriter(ftpClient.appendFileStream(fileName),"utf-8"),true);
            pw.write(content);
            pw.flush();
            pw.close();
            ftpClient.logout();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("上传文件失败");
        } finally {
            if (ftpClient.isConnected()) {
                try {
                    ftpClient.disconnect();
                } catch (IOException e) {
                    e.printStackTrace();
                    throw new Exception("上传文件失败");
                }
            }

        }
    }

    //改变目录路径
    public boolean changeWorkingDirectory(String directory) throws Exception {
        boolean flag = true;
        try {
            //flag = getDefaultFtpClient().changeWorkingDirectory(directory);
            flag = ftpClient.changeWorkingDirectory(directory);
            if (flag) {
                System.out.println("进入文件夹" + directory + " 成功!");

            } else {
                System.out.println("进入文件夹" + directory + " 失败!开始创建文件夹");
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
            throw new Exception("改变目录路径失败");
        }
        return flag;
    }

    //创建多层目录文件,如果有ftp服务器已存在该文件,则不创建,如果无,则创建

    public boolean createDirecroty(String remote) throws Exception {
        boolean success = true;
        String directory = remote + "/";
        // 如果远程目录不存在,则递归创建远程服务器目录
        if (!directory.equalsIgnoreCase("/") && !changeWorkingDirectory(new String(directory))) {
            int start = 0;
            int end = 0;
            if (directory.startsWith("/")) {
                start = 1;
            } else {
                start = 0;
            }
            end = directory.indexOf("/", start);
            String path = "";
            String paths = "";
            while (true) {
                String subDirectory = new String(remote.substring(start, end).getBytes("GBK"), "iso-8859-1");
                path = path + "/" + subDirectory;
                System.out.println("subDirectory:" + subDirectory);
                System.out.println("path:" + path);
                //false表示当前文件夹下没有文件
                if (!existFile(path)) {
                    System.out.println("path:" + path + ";existFile");
                    if (makeDirectory(subDirectory)) {
                        System.out.println("path:" + path + ";makeDirectory");
                        changeWorkingDirectory(subDirectory);
                        System.out.println("path:" + path + ";changeWorkingDirectory");
                    } else {
                        System.out.println("创建目录[" + subDirectory + "]失败");
                        changeWorkingDirectory(subDirectory);
                    }
                } else {
                    System.out.println("path:" + path + ";changeWorkingDirectory1");
                    changeWorkingDirectory(subDirectory);
                    System.out.println("path:" + path + ";changeWorkingDirectory2");
                }

                paths = paths + "/" + subDirectory;
                start = end + 1;
                end = directory.indexOf("/", start);
                // 检查所有目录是否创建完毕
                if (end <= start) {
                    break;
                }
            }
        }
        return success;
    }

    //判断ftp服务器文件是否存在
    public boolean existFile(String path) throws IOException {
        boolean flag = false;
        FTPFile[] ftpFileArr = ftpClient.listFiles(path);
        //FTPFile[] ftpFileArr = getDefaultFtpClient().listFiles(path);
        if (ftpFileArr.length > 0) {
            flag = true;
        }
        return flag;
    }

    //创建目录
    public boolean makeDirectory(String dir) {
        boolean flag = true;
        try {
//            flag = getDefaultFtpClient().makeDirectory(dir);
            flag = ftpClient.makeDirectory(dir);
            if (flag) {
                System.out.println("创建文件夹" + dir + " 成功!");

            } else {
                System.out.println("创建文件夹" + dir + " 失败!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 下载文件 *
     *
     * @param pathName FTP服务器文件目录 *
     * @param fileName 文件名称 *
     *                 //* @param localpath 下载后的文件路径 *
     * @return
     */
    public boolean downloadFile(String pathName, String fileName) throws Exception {
        boolean flag;
        OutputStream os = null;
        try {
            System.out.println("开始下载文件");
            this.ftpClient = getFTPClient();
            ftpClient.setControlEncoding("UTF-8");
            ftpClient.enterLocalPassiveMode();
            //切换FTP目录
            ftpClient.changeWorkingDirectory(pathName);
            FTPFile[] ftpFiles = ftpClient.listFiles();
            for (FTPFile file : ftpFiles) {
                if (fileName.equalsIgnoreCase(file.getName())) {
                    File localFile = new File("/Users/shifangxin/Documents/" + file.getName());
                    os = new FileOutputStream(localFile);
                    ftpClient.retrieveFile(file.getName(), os);
                    os.close();
                }
            }
            ftpClient.logout();
            flag = true;
            System.out.println("下载文件成功");
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception("下载文件失败");
        } finally {
            if (ftpClient.isConnected()) {
                try {
                    ftpClient.disconnect();
                } catch (IOException e) {
                    e.printStackTrace();
                    throw new Exception("下载文件失败");
                }
            }
            if (null != os) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    throw new Exception("下载文件失败");
                }
            }
        }
        return flag;
    }

    /**
     * 删除文件 *
     *
     * @param pathName FTP服务器保存目录 *
     * @param fileName 要删除的文件名称 *
     * @return
     */
    public boolean deleteFile(String pathName, String fileName) throws Exception {
        boolean flag;

        try {
            System.out.println("开始删除文件");
            this.ftpClient = getFTPClient();
            //切换FTP目录
            ftpClient.changeWorkingDirectory(pathName);
            ftpClient.dele(fileName);
            ftpClient.logout();
            flag = true;
            System.out.println("删除文件成功");
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception("删除文件失败");
        } finally {
            if (ftpClient.isConnected()) {
                try {
                    ftpClient.disconnect();
                } catch (IOException e) {
                    e.printStackTrace();
                    throw new Exception("删除文件失败");
                }
            }
        }
        return flag;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值