Java Ftp上传文件,Ftp工具类

这里已经忘了参考的哪位博友的文章,如果有哪位小伙伴看到,请留言
Ftp工具类
package com.ldl.test.util;

import java.io.BufferedInputStream;  
import java.io.BufferedOutputStream;  
import java.io.ByteArrayOutputStream;  
import java.io.File;  
import java.io.FileInputStream;  
import java.io.FileNotFoundException;  
import java.io.FileOutputStream;  
import java.io.IOException;  
import java.io.InputStream;  
import java.io.OutputStream;  
import java.net.SocketException;  
import java.net.UnknownHostException;  
import java.util.ArrayList;  
import java.util.List;  
  
import org.apache.commons.net.ftp.FTP;  
import org.apache.commons.net.ftp.FTPClient;  
import org.apache.commons.net.ftp.FTPFile;  
import org.apache.commons.net.ftp.FTPReply;  
  
/** 
 * FTPClient工具类 
 * @author ldl 
 */  
public class FtpUtil {  
      
    //private static Logger log = Logger.getLogger(FtpUtil.class);  
    private FTPClient ftp;  
  
    public FtpUtil() {  
        ftp = new FTPClient();  
        ftp.setControlEncoding("UTF-8"); //解决上传文件时文件名乱码  
    }  
      
    public FtpUtil(String controlEncoding) {  
        ftp = new FTPClient();  
        ftp.setControlEncoding(controlEncoding); //解决上传文件时文件名乱码  
    }  
      
    public void setTimeOut(int defaultTimeoutSecond, int connectTimeoutSecond, int dataTimeoutSecond){  
        try {  
            ftp.setDefaultTimeout(defaultTimeoutSecond * 1000);  
            //ftp.setConnectTimeout(connectTimeoutSecond * 1000); //commons-net-3.5.jar  
            ftp.setSoTimeout(connectTimeoutSecond * 1000); //commons-net-1.4.1.jar 连接后才能设置  
            ftp.setDataTimeout(dataTimeoutSecond * 1000);  
        } catch (SocketException e) { 
        	System.out.println("setTimeout Exception:"+e);
        }  
    }  
      
    public FTPClient getFTPClient(){  
        return ftp;  
    }  
      
    public void setControlEncoding(String charset){  
        ftp.setControlEncoding(charset);  
    }  
      
    public void setFileType(int fileType) throws IOException {  
        ftp.setFileType(fileType);  
    }  
  
    /** 
     * Connect to FTP server. 
     *  
     * @param host 
     *            FTP server address or name 
     * @param port 
     *            FTP server port 
     * @param user 
     *            user name 
     * @param password 
     *            user password 
     * @throws IOException 
     *             on I/O errors 
     */  
    public FTPClient connect(String host, int port, String user, String password) throws IOException {  
        // Connect to server.  
        try {  
            ftp.connect(host, port);  
        } catch (UnknownHostException ex) {  
            throw new IOException("Can't find FTP server '" + host + "'");  
        }  
  
        // Check rsponse after connection attempt.  
        int reply = ftp.getReplyCode();  
        if (!FTPReply.isPositiveCompletion(reply)) {  
            disconnect();  
            throw new IOException("Can't connect to server '" + host + "'");  
        }  
  
        if ("".equals(user)) {  
            user = "qiangdiao_ftp";  
        }  
  
        // Login.  
        if (!ftp.login(user, password)) {  
            disconnect();  
            throw new IOException("Can't login to server '" + host + "'");  
        }  
  
        // Set data transfer mode.  
        ftp.setFileType(FTP.BINARY_FILE_TYPE);  
        //ftp.setFileType(FTP.ASCII_FILE_TYPE);  
          
        /*  注: ftp上传到服务器分主动模式和被动模式,网ip上传主动模式:ftp.enterLocalActiveMode();(默认)
        	内网ip上传被动模式:ftp.enterLocalPassiveMode();*/
        // Use passive mode to pass firewalls.  
        //ftp.enterLocalPassiveMode();  
          
        return ftp;  
    }  
      
    /** 
     * Test connection to ftp server 
     *  
     * @return true, if connected 
     */  
    public boolean isConnected() {  
        return ftp.isConnected();  
    }  
      
    /** 
     * Disconnect from the FTP server 
     *  
     * @throws IOException 
     *             on I/O errors 
     */  
    public void disconnect() throws IOException {  
  
        if (ftp.isConnected()) {  
            try {  
                ftp.logout();  
                ftp.disconnect();  
            } catch (IOException ex) {  
            }  
        }  
    }  
      
    /** 
     * Get file from ftp server into given output stream 
     *  
     * @param ftpFileName 
     *            file name on ftp server 
     * @param out 
     *            OutputStream 
     * @throws IOException 
     */  
    public void retrieveFile(String ftpFileName, OutputStream out) throws IOException {  
        try {  
            // Get file info.  
            FTPFile[] fileInfoArray = ftp.listFiles(ftpFileName);  
            if (fileInfoArray == null || fileInfoArray.length == 0) {  
                throw new FileNotFoundException("File '" + ftpFileName + "' was not found on FTP server.");  
            }  
  
            // Check file size.  
            FTPFile fileInfo = fileInfoArray[0];  
            long size = fileInfo.getSize();  
            if (size > Integer.MAX_VALUE) {  
              
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值