FTP 文件上传下载/读取


用到的jar包: comments-net.jar

下载地址:http://download.csdn.net/detail/xuanjiewu/9838448

这里仅仅是对ftp工具类的简单使用,很多东西还不是很了解。当然学以致用,先用到这里吧。

[java]  view plain  copy
 print ?
  1. public class FtpTest {  
  2.     /** 
  3.      * 向ftp写文件(数据) 
  4.      */  
  5.     @Test  
  6.     public void uploadFile() {  
  7.    
  8.         // 要写入的文件内容  
  9.         String fileContent = "hello world,你好世界";  
  10.         // ftp登录用户名  
  11.         String userName = "admin";  
  12.         // ftp登录密码  
  13.         String userPassword = "xxxx";  
  14.         // ftp地址  
  15.         String server = "127.0.0.1";//直接ip地址  
  16.         // 创建的文件  
  17.         String fileName = "ftp.txt";  
  18.         // 指定写入的目录  
  19.         String path = "wd";  
  20.    
  21.         FTPClient ftpClient = new FTPClient();  
  22.         try {  
  23.             InputStream is = null;  
  24.             // 1.输入流  
  25.             is = new ByteArrayInputStream(fileContent.getBytes());  
  26.             // 2.连接服务器  
  27.             ftpClient.connect(server);  
  28.             // 3.登录ftp  
  29.             ftpClient.login(userName, userPassword);  
  30.             // 4.指定写入的目录  
  31.             ftpClient.changeWorkingDirectory(path);  
  32.             // 5.写操作  
  33.             ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);  
  34.             ftpClient.storeFile(new String(fileName.getBytes("utf-8"),  
  35.                     "iso-8859-1"), is);  
  36.             is.close();  
  37.         } catch (Exception e) {  
  38.             e.printStackTrace();  
  39.         } finally {  
  40.             if (ftpClient.isConnected()) {  
  41.                 try {  
  42.                     ftpClient.disconnect();  
  43.                 } catch (Exception e) {  
  44.                     e.printStackTrace();  
  45.                 }  
  46.             }  
  47.         }  
  48.     }  
  49.       
  50.     /** 
  51.      * ftp下载数据 
  52.      */  
  53.     @Test  
  54.     public void downFile() {  
  55.         // ftp登录用户名  
  56.         String userName = "admin";  
  57.         // ftp登录密码  
  58.         String userPassword = "xxxx";  
  59.         // ftp地址:直接IP地址  
  60.         String server = "xxxx";  
  61.         // 创建的文件  
  62.         String fileName = "ftp.txt";  
  63.         // 指定写入的目录  
  64.         String path = "wd";  
  65.         // 指定本地写入文件  
  66.         String localPath="D:\\";  
  67.           
  68.         FTPClient ftp = new FTPClient();  
  69.         try {  
  70.             int reply;  
  71.             //1.连接服务器  
  72.             ftp.connect(server);  
  73.             //2.登录服务器 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器  
  74.             ftp.login(userName, userPassword);  
  75.             //3.判断登陆是否成功  
  76.             reply = ftp.getReplyCode();  
  77.             if (!FTPReply.isPositiveCompletion(reply)) {  
  78.                 ftp.disconnect();  
  79.             }  
  80.             //4.指定要下载的目录  
  81.             ftp.changeWorkingDirectory(path);// 转移到FTP服务器目录  
  82.             //5.遍历下载的目录  
  83.             FTPFile[] fs = ftp.listFiles();  
  84.             for (FTPFile ff : fs) {  
  85.                 //解决中文乱码问题,两次解码  
  86.                 byte[] bytes=ff.getName().getBytes("iso-8859-1");  
  87.                 String fn=new String(bytes,"utf8");  
  88.                 if (fn.equals(fileName)) {  
  89.                     //6.写操作,将其写入到本地文件中  
  90.                     File localFile = new File(localPath + ff.getName());  
  91.                     OutputStream is = new FileOutputStream(localFile);  
  92.                     ftp.retrieveFile(ff.getName(), is);  
  93.                     is.close();  
  94.                 }  
  95.             }  
  96.             ftp.logout();  
  97.         } catch (IOException e) {  
  98.             e.printStackTrace();  
  99.         } finally {  
  100.             if (ftp.isConnected()) {  
  101.                 try {  
  102.                     ftp.disconnect();  
  103.                 } catch (IOException ioe) {  
  104.                 }  
  105.             }  
  106.         }  
  107.     }  
  108.  }  
  109.    
很多知识点是相互联系的,希望以后的例子中能够结合更多的知识点进行实例编写,这样也有助于知识的巩固。

参见:

http://blog.csdn.net/techbirds_bao/article/details/8593706

http://blog.csdn.net/kardelpeng/article/details/6588284


封装

/**
	  * FTP 连接服务
	  * @param ftpHost
	  * @param ftpPort
	  * @param ftpUserName
	  * @param ftpPassword
	  * @return
	  */
	 public static FTPClient getFTPClient(String ftpHost, int ftpPort, String ftpUserName, String ftpPassword) { 
	        FTPClient ftpClient = null;  
	        try {  
	            ftpClient = new FTPClient();  
	            ftpClient.connect(ftpHost, ftpPort);// 连接FTP服务器  
	            ftpClient.login(ftpUserName, ftpPassword);// 登陆FTP服务器  
	            if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {  
	                System.out.println("未连接到FTP,用户名或密码错误!");  
	                ftpClient.disconnect();  
	            } else {  
	            	System.out.println("FTP连接成功!");  
	            }  
	        } catch (SocketException e) {  
	            e.printStackTrace();  
	            System.out.println("FTP的IP地址可能错误,请正确配置!");  
	        } catch (IOException e) {  
	            e.printStackTrace();  
	            System.out.println("FTP的端口错误,请正确配置!");  
	        }  
	        return ftpClient;  
	    }   


	 /**
	  * FTP文件下载/读取
	  * @param ftpServer
	  * @param ftpPort
	  * @param ftpUser
	  * @param ftpPwd
	  * @param fileName
	  * @param filePath
	  * @param localPath
	  */
		public static String ftpDownload(String ftpServer, int ftpPort, String ftpUser,String ftpPwd,String filePath,String fileName,String localPath){
			String message = "";
			try {
				FTPClient ftp = getFTPClient(ftpServer, ftpPort, ftpUser, ftpPwd); 
		        ftp.changeWorkingDirectory(filePath);  //转移到FTP服务器目录 
		       
		        File localFile = new File(localPath + fileName);  
                        OutputStream os = new FileOutputStream(localFile);  
                        ftp.retrieveFile(fileName, os);  
                        os.close();  
                        System.out.println("FTP文件:"+fileName+" 已成功下载到 "+localPath);
                         message = fileName+" 已成功下载到 "+localPath;
		        
		        ftp.logout(); 
			} catch (IOException e) {
				message = "FTP文件:"+fileName+"下载失败!";
				e.printStackTrace();
			}
			return message;
		}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值