FTP 文件上传

    public static boolean uploadFile(FtpConfigVo ftpConfig, String baseDir,String filename, InputStream input) {
        boolean result = false;
        FTPClient ftpClient = new FTPClient();
        String encoding = System.getProperty("file.encoding");
        String ftpBasePath = ftpConfig.getPath() + baseDir;
        try {
            ftpClient.connect(ftpConfig.getIp(), ftpConfig.getPort().intValue());

            ftpClient.login(ftpConfig.getUsername(), ftpConfig.getPwd());
            ftpClient.setControlEncoding(encoding);

            int reply = ftpClient.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftpClient.disconnect();
                return result;
            }

            boolean change = ftpClient.changeWorkingDirectory(new String(
                    ftpBasePath.getBytes(encoding), "iso-8859-1"));
            if (!change) {
                change = createDirPath(ftpClient, ftpBasePath);
            }
            ftpClient.enterLocalPassiveMode();
            ftpClient.setFileType(2);
            if (change) {
                //filename 指文件名,不包含路径
                result = ftpClient.storeFile(new String(filename.getBytes(encoding), "iso-8859-1"),input);
            }
            ftpClient.logout();
            return result;
        } catch (IOException e) {
            
        } finally {
            IOUtils.closeQuietly(input);
            if (ftpClient.isConnected()) {
                try {
                    ftpClient.disconnect();
                } catch (IOException ioe) {
                }
            }
        }
    }

 

posted @ 2017-09-22 14:43  不像程序员  阅读(151)  评论(0编辑  收藏  举报