java 链接ftp,相关操作

package com.galaxy.helper;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.springframework.stereotype.Component;



/**
 * @author DELL
 *
 */
@Component
public class FtpHelper {
	
	private String username = "ccjy";
	private String password = "11";
	private String host = "192.168.2.54";
	private String port = "21";
	
	public boolean uploadFile2Ftp(String path,String fileName,InputStream inputStream)throws Exception{
		this.connect(path, host, Integer.parseInt(port), username,password);
	    return this.upload(fileName,inputStream);  
	}
	
	 private  FTPClient ftp;    
	    /** 
	     *  
	     * @param path 上传到ftp服务器哪个路径下    
	     * @param addr 地址 
	     * @param port 端口号 
	     * @param username 用户名 
	     * @param password 密码 
	     * @return 
	     * @throws Exception 
	     */  
	    private  boolean connect(String path,String addr,int port,String username,String password) throws Exception {    
	        boolean result = false;    
	        ftp = new FTPClient();    
	        int reply;    
	        ftp.connect(addr,port);    
	        ftp.login(username,password);    
	        ftp.setFileType(FTPClient.BINARY_FILE_TYPE);  
	        reply = ftp.getReplyCode();    
	        if (!FTPReply.isPositiveCompletion(reply)) {  
	            ftp.disconnect();    
	            return result;    
	        }    
	        ftp.makeDirectory(path);  
	        ftp.changeWorkingDirectory(path);    
	        result = true;    
	        return result;    
	    }    
	    
	    private boolean upload(String fileName,InputStream inputStream)throws Exception{
	    	boolean storeFile = ftp.storeFile(fileName, inputStream); 
	    	if(inputStream!=null){
	    		try {
					inputStream.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
	    	}
	    	ftp.disconnect();  
	    	return storeFile;
	    }
	    
	    
	    //查询指定目录下的文件 返回JSONArray
	    public  JSONArray filelist(String path) throws Exception{
	    	this.connect(path, host, Integer.parseInt(port), username,password);
	    	FTPFile[] filelist = ftp.listFiles();
	    	JSONArray jarray=new JSONArray();
	    	for(int i=0;i<filelist.length;i++){
	    		if(filelist[i].isFile()){
	    			String remoteAbsoluteFile = filelist[i].getName();
		    		int tag=remoteAbsoluteFile.indexOf("-success");
		    		if(tag<=0){
		    			//System.out.println(ftp.retrieveFileStream(remoteAbsoluteFile)+"======");
		    			InputStream in=ftp.retrieveFileStream(remoteAbsoluteFile);
			    		BufferedReader reader=new BufferedReader(new InputStreamReader(in));
			    		String tempString = null;
			    		String laststr = "";
			    	     //一次读入一行,直到读入null为文件结束
			    	     while ((tempString = reader.readLine()) != null) {
			    	      laststr = laststr+tempString;
			    	     }
			    		JSONObject json = new JSONObject().fromObject(laststr);
			    		jarray.add(json);
			    		reader.close();
			    		in.close();
			    		ftp.completePendingCommand();
		    		}
	    		}
	    	}
	    	ftp.disconnect();    
	    	return jarray;
	    }
	    
	    //重命名ftp上的文件
	    public Boolean renamefile(String path,String tablename,String  id) throws Exception{
	    	this.connect(path, host, Integer.parseInt(port), username,password);
	    	Boolean tag= ftp.rename(tablename+id+".json", tablename+id+"-success.json");
	    	ftp.disconnect();  
	    	return tag;
	    }
	    
	    
	   
}



猜你喜欢

转载自blog.csdn.net/u011293970/article/details/53516888