java 中应用FTP的一些知识点及注意

主要有ftp的上传、下载、取得ftp文件列表功能。其中上传引用的是jdk自带的sun.net.flt.FtpClient(路径不知时候正确),文件列表和下载应用的是apache 的common -net .jar  jar包

具体代码如下:

	/**
		 * 连接ftp服务器
		 * @param server  服务器地址
		 * @param user 用户名
		 * @param password  密码
		 * @param path 用户权限根路径下面的路径
		 */
		public static FtpClient connectServer(String server, String user, String password,String path){
			// server:FTP服务器的IP地址;user:登录FTP服务器的用户名
			// password:登录FTP服务器的用户名的口令;path:FTP服务器上的路径
			FtpClient ftpClient = new FtpClient();
			try {
				ftpClient.openServer(server);
				ftpClient.login(user, password);
				//path是ftp服务下主目录的子目录
				if (path.length() != 0)
				ftpClient.cd(path);
				//用2进制上传
				ftpClient.binary();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return ftpClient;
		}
	/**
	 * 上传文件到ftp服务器
	 * 	@param 服务名称 ,后面没有端口参数默认为21
	 *  @param user 用户名
	 *  @param password 密码
	 *  @param path 路径
	 *  @file 要上传的文件
	 */
	public static boolean uploadToFtp(String server, String user, String password,String path,File file){
		// server:FTP服务器的IP地址;user:登录FTP服务器的用户名
		// password:登录FTP服务器的用户名的口令;path:FTP服务器上的路径
		boolean flag = true;
		FtpClient ftpClient = new FtpClient();
		try {
			ftpClient.openServer(server);
			ftpClient.login(user, password);
			//path是ftp服务下主目录的子目录
			if (path.length() != 0)
			ftpClient.cd(path);
			//用2进制上传
			ftpClient.binary();
			if (ftpClient != null) {
			   TelnetOutputStream os = null;
				FileInputStream is = null;
				//"upftpfile"用ftp上传后的文件名
				os = ftpClient.put(file.getName());				
				is = new FileInputStream(file);
				byte[] bytes = new byte[1024];
				int c;
				while ((c = is.read(bytes)) != -1) {
				  os.write(bytes, 0, c);
				}
				if (is != null) {
				  is.close();
				}
				if (os != null) {
				  os.close();
				}
			} 
			ftpClient.closeServer();						
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			flag = false;
		}
		return flag;
	}
	
	/**
	 *得到FTP中的文件目录 
	 */
	public static List getFtpList(String server, String user, String password,String path){
	   org.apache.commons.net.ftp.FTPClient ftpClient = new FTPClient();
	   List<FTPFile> listFtpFiles  = new ArrayList<FTPFile>();
	try {
	   ftpClient.connect(server);
	   ftpClient.login(user, password);
	   FTPFile[] names = ftpClient.listFiles(path);
	  // ftpClient.setKeepAlive(true);
	   for(FTPFile ftpFile :names){
		   //System.out.println("文件名称:"+new String(ftpFile.getName().getBytes("iso-8895-1"),"gbk"));
		   ftpFile.setName(new String(ftpFile.getName().getBytes("iso-8859-1"),"gbk"));
		   listFtpFiles.add(ftpFile);
	   }
	   ftpClient.disconnect();
	} catch (SocketException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	} catch (IOException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	}
	   return listFtpFiles;
	}
	
	 /**
  * ftp下载
  * @param fileName 要下载的文件的名称
  * @param outDir 文件的输出路径
  */
 public static boolean  downLoadFtpFile(String server, String user, String password,String path,String fileName,String outDir){
  boolean flag = true;
  org.apache.commons.net.ftp.FTPClient ftpClient = new FTPClient();  
  try {
     ftpClient.connect(server);
     ftpClient.login(user, password);
     FTPFile[] names = ftpClient.listFiles(path);
    // ftpClient.setKeepAlive(true);
     for(FTPFile ftpFile :names){
      //System.out.println("文件名称:"+new String(ftpFile.getName().getBytes("iso-8895-1"),"gbk"));
      ftpClient.setControlEncoding("gbk");
      String name = new String(ftpFile.getName().getBytes("iso-8859-1"),"gbk");
     // String name = ftpFile.getName();
     if(name.equalsIgnoreCase(fileName)){
       OutputStream os = new FileOutputStream(new File(outDir+File.separatorChar+name));
       System.out.println("文件名:"+name);
       ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
       //如果对名称不进行转换的话会将中文的文件下下来后大小变为0kb
       ftpClient.retrieveFile(new String((path+File.separatorChar+name).getBytes("gbk"),"iso-8859-1"), os);
       os.close();
     }    
     }
     ftpClient.disconnect();
  } catch (Exception e1) {
   // TODO Auto-generated catch block
   e1.printStackTrace();
   flag = false;
  }
     return flag;
 }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值