关于使用java通过 FTP 上传文件,总结

1、需要引入包
   commons-net-ftp-2.0.jar
2、部分代码如下:
import org.apache.commons.io.IOUtils; 
import org.apache.commons.net.ftp.FTPClient; 
import sun.net.ftp.FtpClient;
import sun.net.ftp.FtpLoginException;
import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.FileOutputStream; 
import java.util.ResourceBundle;


/**
 * 
 * @author 赵永恩
 *
 */
public class FtpFile { 
	public static String FtpIp = "192.168.1.100";
	public static int port = 21;
	public static String FtpUser = "admin";
	public static String FtpPwd = "password";
	public static String Ftppath = "/FTP_file/VID/";
	
	/**
	 * 测试方法
	 * 上边的信息,可以写在配置文件中,通过读取配置文件来获取
	 * 
	 *  读取配置文件方法如下:
	 *  ResourceBundle bundle = ResourceBundle.getBundle("FilePathConfig");//FilePathConfig.properties配置文件的获取路径
	 *	String FtpIp = bundle.getString("FtpIp");
	 *  String port = bundle.getString("port");
	 * 
	 * @param args
	 */
    public static void main(String[] args) { 
    	String filename = "zykgl_yong.dmp";
    	String localpath = "D:\\zykgl.dmp";

        testUpload(localpath,Ftppath,filename); 
        testDownload(localpath,Ftppath,filename);
        fileNames();
    } 
    
    /**
     * 
     * 连接判断的方法
     * 
     * @param RWFileDir
     * @param hostname
     * @param port
     * @param uid
     * @param pwd
     * @return
     */
    public static String connect(String RWFileDir,String hostname,int port,String uid,String pwd) {
    	String a="fail";
    	FtpClient aftp;
		System.out.println("正在连接"+hostname+",请等待....");
		try{ 
		  aftp = new FtpClient(hostname,port); 
		  aftp.login(uid,pwd); 
		  aftp.binary();
		  a = "连接主机:"+hostname+"成功!";
		  a="connectSuccess";
		} catch(FtpLoginException e){ 
		  a="登陆主机:"+hostname+"失败!请检查用户名或密码是否正确:"+e; 
		} catch (IOException e){
		  a="连接主机:"+hostname+"失败!请检查端口是否正确:"+e; 
		}catch(SecurityException e){ 
		  a="无权限与主机:"+hostname+"连接!请检查是否有访问权限:"+e; 
		} 
		return a; 
	} 
    
    /** 
     * FTP上传单个文件测试 
     */ 
    public static void testUpload(String localpath,String Ftppath,String filename) { 
        FTPClient ftpClient = new FTPClient(); 
        FileInputStream fis = null; 
        try { 
            ftpClient.connect(FtpIp,port); 
            ftpClient.login(FtpUser, FtpPwd); 
            
            File srcFile = new File(localpath); 
            fis = new FileInputStream(srcFile); 
            //设置上传目录 
            ftpClient.changeWorkingDirectory(Ftppath); 
            ftpClient.setBufferSize(1024*1024); 
            ftpClient.setControlEncoding("utf-8"); 
            //设置文件类型(二进制) 
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); 
            ftpClient.storeFile(filename, fis); 
            // 关闭输入流
            fis.close();
            // 退出ftp
            ftpClient.logout();
            System.out.println("====上传成功====");
        } catch (IOException e) { 
            e.printStackTrace(); 
            throw new RuntimeException("FTP客户端出错!", e); 
        } finally { 
            IOUtils.closeQuietly(fis); 
            try { 
                ftpClient.disconnect(); 
            } catch (IOException e) { 
                e.printStackTrace(); 
                throw new RuntimeException("关闭FTP连接发生异常!", e); 
            } 
        } 
    } 


    
    /** 
     * FTP下载单个文件测试 
     */ 
    public static void testDownload(String localpath,String Ftppath,String filename) { 
        FTPClient ftpClient = new FTPClient(); 
        FileOutputStream fos = null; 

        try { 
            ftpClient.connect(FtpIp,port); 
            ftpClient.login(FtpUser, FtpPwd); 

            String remoteFileName = Ftppath+filename; 
            fos = new FileOutputStream(localpath); 
            ftpClient.setBufferSize(1024); 
            //设置文件类型(二进制) 
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); 
            ftpClient.retrieveFile(remoteFileName, fos); 
            fos.close(); 
            //退出ftp
            ftpClient.logout();
            System.out.println("====下载成功====");
        } catch (IOException e) { 
            e.printStackTrace(); 
            throw new RuntimeException("FTP客户端出错!", e); 
        } finally { 
            IOUtils.closeQuietly(fos); 
            try { 
                ftpClient.disconnect(); 
            } catch (IOException e) { 
                e.printStackTrace(); 
                throw new RuntimeException("关闭FTP连接发生异常!", e); 
            } 
        } 
    } 
    
    /**
     * 读取目录下所有文件并下载
     */
    public static void fileNames(){
    	 try { 
	    	FTPClient ftpclient = new FTPClient(); 
	    	ftpclient.connect(FtpIp); 
	    	ftpclient.login(FtpUser, FtpPwd); 
	    	String[] nameList = ftpclient.listNames();
	    	for(int i = 0 ; i < nameList.length ; i++){
	    		String filename = new String(nameList[i].getBytes("iso-8859-1"));//文件名中文转换iso-8859-1,utf-8
	    		System.out.println(filename+nameList[i]);
	    		String localpath = "F:\\"+filename;//下载后重建文件名
	    		 FileOutputStream fos = null; 
	    		 String remoteFileName = "/"+nameList[i]; //服务器上文件名
	    		if (1>0) {
	    			fos = new FileOutputStream(localpath); 
	    			ftpclient.setBufferSize(204800); 
	    			//设置文件类型(二进制) 
	    			ftpclient.setFileType(FTPClient.BINARY_FILE_TYPE); 
	    			ftpclient.retrieveFile(remoteFileName, fos); 
	    			fos.close(); 
	    			ftpclient.logout();
	    		}
	    	}
    	 }catch(Exception e){
    		 e.printStackTrace();
    	 }
   }
}


配置文件如下:
FtpIp=192.168.1.100
ftp_port=21
FtpUser=admin
FtpPwd=password
Ftppath=/FTP_file/VID/

你可能感兴趣的:(java,apache,.net,F#,sun)