java多线程    Java入门    vsftp    ftp    linux配置    centos    FRP教程    HBase    Html5缓存    webp    zabbix    分布式    neo4j图数据库    

java操作FTP

java操作FTP,采用sun.net.ftp.FtpClient

package com.gap.y;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

import sun.net.TelnetInputStream;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;

public class Ftp {
	private FtpClient ftpClient;

	public void open(String server, String user, String password) {
		ftpClient = new FtpClient();
		try {
			ftpClient.openServer(server);
			ftpClient.login(user, password);
		} catch (IOException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
	}

	public void close() {
		try {
			ftpClient.closeServer();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public String down(String path, String remoteFile, String localFile) {

		if (path.length() != 0) {
			try {
				ftpClient.sendServer("mkdir " + path + "");
				ftpClient.cd(path);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		try {
			ftpClient.binary();
		} catch (IOException e) {
			e.printStackTrace();
		}
		TelnetInputStream in = null;
		try {
			 in = ftpClient.get(remoteFile);	
			 OutputStream out = new FileOutputStream(localFile); 
			byte[] buffer = new byte[1024]; 
			int bytesRead = -1; 
			while ((bytesRead = in.read(buffer)) != -1) out.write(buffer, 0, bytesRead); 
			out.flush(); //将缓冲的数据写入 
			out.close();				
			in.close();
		} catch (Exception e1) {
			e1.printStackTrace();
		} finally {

		}

		return "1";
	}
	
	public void creat(String path){

		try {				
			ftpClient.sendServer("MKD " + path + " \r\n");
			ftpClient.readServerResponse();				
		} catch (IOException e1) {				
			e1.printStackTrace();
		}
	}
	public void del(String path){

		try {				
			ftpClient.sendServer("DELE " + path + " \r\n");
			ftpClient.readServerResponse();				
		} catch (IOException e1) {				
			e1.printStackTrace();
		}
	}
	
	public void cd(String path){

		if (path.length() != 0) {
			try {				
				ftpClient.cd(path);				
			} catch (IOException e) {
				
			}
		}
		
		try {
			ftpClient.binary();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public String upload(String remoteFile, String str) {
		TelnetOutputStream os = null;
		InputStream is = null;
		try {
			os = ftpClient.put(remoteFile);
			is = new java.io.ByteArrayInputStream(str.getBytes());
			byte[] bytes = new byte[1024];
			int c;
			while ((c = is.read(bytes)) != -1) {
				os.write(bytes, 0, c);
			}
			is.close();
			os.flush();
			os.close();
		} catch (Exception e1) {
			e1.printStackTrace();
		} finally {

		}

		return "1";
	}
	
	  public String getStr(InputStream in) {
	        try {
	            StringBuffer temp = null;
	            BufferedReader buffer = new BufferedReader(
	                    new InputStreamReader(in));
	            String tempstr = "";
	            String strsum = "";
	            while ((tempstr = buffer.readLine()) != null) {
	                strsum = strsum + tempstr + "\r\n";

	            }
	            buffer.close();
	            // in.close();
	            return strsum;
	        } catch (Exception e) {
	            System.out.println("shit");
	        }
	        return "";
	    }
	  
	public String getList(String dir) {
		
		InputStream is = null;
		String s = "";
		try {
			is = ftpClient.nameList(dir);
			s = this.getStr(is);			
			is.close();			
		} catch (Exception e1) {
			e1.printStackTrace();
		} finally {

		}
		return s;
	}
	public String uploadFile(String remoteFile, String str) {
		TelnetOutputStream os = null;
		InputStream is = null;		
		try {
			os = ftpClient.put(remoteFile);
			is = new FileInputStream(str);
			byte[] bytes = new byte[102400];
			int c;
			while ((c = is.read(bytes)) != -1) {
				os.write(bytes, 0, c);
				os.flush();
			}
			is.close();
			os.flush();
			os.close();
		} catch (Exception e1) {
			e1.printStackTrace();
		} finally {
			
		}

		return "1";
	}


	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Ftp f= new Ftp();
		f.open("java-er.com","web9311185","ab1232wsx");
		String rpath = "/Web9311185/wwwroot/res/0/";
		String q = f.getList(rpath);
		
		String [] m = q.split("\r\n");
		for(int i = 0 ; i < m.length;i++){
			if(i%10==0){
				f.close();
				f.open("java-er.com","web9311185","ab1232wsx");
			}
			f.del(m[i]);
			System.out.println(m[i] + "|");
		}
		f.close();
		System.out.println(m.length);
	}

}


This entry was posted in JAVA and tagged , . Bookmark the permalink.
月小升QQ 2651044202, 技术交流QQ群 178491360
首发地址:月小升博客https://java-er.com/blog/java-ftp/
无特殊说明,文章均为月小升原创,欢迎转载,转载请注明本文地址,谢谢
您的评论是我写作的动力.

Leave a Reply