JDK1.7之ftp上传文件

package com.xnh.atom.server;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import sun.net.ftp.FtpClient;
import sun.net.ftp.FtpProtocolException;


public class FtpUtil {
	
	private static final Logger log = LoggerFactory.getLogger(FtpUtil.class);

    private String ip = "";

    private String username = "";

    private String password = "";

    private int port = 21;

    private String path = "";

    FtpClient ftpClient = null;

    OutputStream os = null;

    FileInputStream is = null;

    public FtpUtil(String serverIP, String username, String password) {
        this.ip = serverIP;
        this.username = username;
        this.password = password;
    }

    public FtpUtil(String serverIP, int port, String username, String password) {
        this.ip = serverIP;
        this.username = username;
        this.password = password;
        this.port = port;
    }
    /**
     * 连接ftp服务器
     *
     * @throws IOException ,FtpProtocolException
     */
    public void connectServer() throws IOException, FtpProtocolException {
        ftpClient = FtpClient.create();
        ftpClient.connect(new InetSocketAddress(ip, port));
        ftpClient.login(this.username,this.password.toCharArray());
        if (this.path.length() != 0){
            ftpClient.changeDirectory(this.path);// path是ftp服务下主目录的子目录
        }
        ftpClient.setBinaryType();// 用2进制上传、下载
        System.out.println("已登录到\"" + ftpClient.getWorkingDirectory() + "\"目录");
    }

    /**
     * 断开与ftp服务器连接
     *
     * @throws IOException
     */
    public void closeServer(){
        try{
            if (is != null) {
                is.close();
            }
            if (os != null) {
                os.close();
            }
            if (ftpClient != null) {
                ftpClient.close();
            }
            System.out.println("已从服务器断开");
        }catch(IOException e){
            e.printStackTrace();
        }
    }

    /**
     * 检查文件夹在当前目录下是否存在
     * @param dir
     * @return
     */
    private boolean isDirExist(String dir){
        boolean isDir = false;
        String pwd = "";
        try {
            pwd = ftpClient.getWorkingDirectory();
            ftpClient.changeDirectory(dir);
            ftpClient.changeToParentDirectory();
            isDir = true;
        }catch(Exception e){
            log.error("获取文件出错,文件名:" + dir);
            log.error("获取文件出错,原因为:" + e.getMessage(), e);
        }finally{
            if (pwd!=null) {
                try {
                    ftpClient.changeDirectory(pwd);
                } catch (FtpProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return isDir;
    }
    /**
     * 切换目录
     *
     * @param path
     */
    public void changeDirectory(String path) {
    	try {
          ftpClient.changeDirectory(path);
    	} catch (FtpProtocolException e) {
          try {
              ftpClient.makeDirectory(path);
              System.out.println("文件目录不存在");
          } catch (FtpProtocolException ftpProtocolException) {
              ftpProtocolException.printStackTrace();
          } catch (IOException ioException) {
              ioException.printStackTrace();
          }
    	} catch (IOException e) {
          e.printStackTrace();
    	}
    }

    /**
     * 上传文件
     *
     * @param localFile
     * @param ftpFile
     */
    public void upload(String localFile, String ftpFile) {
        OutputStream os = null;
        FileInputStream fis = null;
        try {
            // 将ftp文件加入输出流中。输出到ftp上
            os = ftpClient.putFileStream(ftpFile);
            File file = new File(localFile);
            // 创建一个缓冲区
            fis = new FileInputStream(file);
            byte[] bytes = new byte[1024];
            int c;
            while ((c = fis.read(bytes)) != -1) {
                os.write(bytes, 0, c);
            }
            System.out.println("upload success!!");
        } catch (FtpProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (os != null)
                    os.close();
                if (fis != null)
                    fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 上传文件
     *
     * @param outputStream
     * @param ftpFile
     */
    public void outUpload(ByteArrayOutputStream outputStream, String ftpFile) {
        OutputStream os = null;
        FileInputStream fis = null;
        try {
            os = ftpClient.putFileStream(ftpFile);
            os.write(outputStream.toByteArray());
            System.out.println("upload success!!");
        } catch (FtpProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (os != null)
                    os.close();
                if (fis != null)
                    fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 上传文件
     *
     * @param data
     * @param ftpFile
     */
    public void outUploadTxt(String data, String ftpFile) {
        FileInputStream fis = null;
        try {
            os = ftpClient.putFileStream(ftpFile);
            os.write(data.getBytes());
            System.out.println("upload success!!");
        } catch (FtpProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (os != null)
                    os.close();
                if (fis != null)
                    fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 文件下载
     *
     * @param localFile
     * @param ftpFile
     */
    public void download(String localFile, String ftpFile) {
        InputStream is = null;
        FileOutputStream fos = null;
        try {
            // 获取ftp上的文件
            is = ftpClient.getFileStream(ftpFile);
            File file = new File(localFile);
            byte[] bytes = new byte[1024];
            int i;
            fos = new FileOutputStream(file);
            while ((i = is.read(bytes)) != -1) {
                fos.write(bytes, 0, i);
            }
            System.out.println("download success!!");
        } catch (FtpProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fos != null)
                    fos.close();
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值