ftp 上传 下载 删除

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;


import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;


public class FtpUtils {


private static FTPClient ftp; 
private static String path;
private static String addr;
private static int port;
private static String username;
private static String password;

static{
path = "";
addr = "192.200.5.26";//服务器地址
port = 21;//端口默认21
username = "root";//用户名
password = "root123";//密码
}

/**  
     *   
     * @param path 上传到ftp服务器哪个路径下     
     * @param addr 地址  
     * @param port 端口号  
     * @param username 用户名  
     * @param password 密码  
     * @return  
     * @throws Exception  
     * @author lxy 20170518
     */    
    private static boolean connectFtpInfo(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.changeWorkingDirectory(path);      
        result = true;      
        return result;      
    }  
    
    
    /**
     * 
     * @param file 文件名称
     * @param newImageName:新的图片的名称
     * @param directoryName 上传到目录文件名称
     * @author lxy 20160928
     * @throws Exception
     */
    public static boolean uploadFileToFtpService(InputStream input,String newImageName,String directoryName,String firstdirect) throws Exception{
    path=firstdirect;
    connectFtpInfo(path, addr, port, username, password);
    //文件上传
//         FileInputStream input = new FileInputStream(file); 
    setFtpBaseInfo(directoryName);
    ftp.enterLocalPassiveMode();
         boolean isok = ftp.storeFile(newImageName, input);
         input.close();
         ftp.logout();
         return isok;
         
    }      
    
    public static void main(String[] args) throws Exception {
    //http://192.168.3.201/presentation/CT8256220161124 //829472016120916122
//     System.out.println(isImageExistsFtp("presentation/CT8256220161124","8294720161209161220.doc"));
    File file = new File("D:/home/20170523/NR_CW_1.txt");
    FileInputStream inp = new FileInputStream(file);
    uploadFileToFtpService(inp,"sss.txt","","/zgh");
}
    
    /**
     * ftp服务器参数设置
     * @param directoryName
     * @author lxy 20160929
     * @throws Exception
     */
    private static void setFtpBaseInfo(String directoryName) throws Exception{
    ftp.enterLocalPassiveMode();
         int defaultTimeoutSecond=30*60 * 1000;  
         //设置超时时间
         ftp.setDefaultTimeout(defaultTimeoutSecond);  
//         ftp.setConnectTimeout(defaultTimeoutSecond );  
         
         ftp.setDataTimeout(defaultTimeoutSecond);  
         if(directoryName!=null && !"".equals(directoryName)){
//         directoryName = directoryName.substring(directoryName.lastIndexOf("\\")+1);
        ftp.changeWorkingDirectory(directoryName);
         
         }
    }
    
    /**
     * 
     * @param remoteminfo
     * @param imageName
     * @return 如果图片存在则返回true,不存在则返回false
     * @author lxy 20160929
     * @throws Exception
     */
    public static boolean isImageExistsFtp(String remoteminfo,String imageName) throws Exception{
    connectFtpInfo(path, addr, port, username, password);
    setFtpBaseInfo(remoteminfo);
    FTPFile[] tempImageFtpImageList = ftp.listFiles();
    boolean isok = false;
    if(tempImageFtpImageList!=null && tempImageFtpImageList.length>0){
    for (int i = 0; i < tempImageFtpImageList.length; i++) {
    FTPFile fTPFileTemp = tempImageFtpImageList[i];
    String imagesuffix = fTPFileTemp.getName();
    if(imagesuffix!=null && imagesuffix.equals(imageName)){
    isok = true;
    break;
    }
}
    }
    return isok;
    }
    
    /**
     * 
     * @param remoteminfo 远程目录地址信息
     * @param imageNameList 需要验证是否存在的图片集合
     * @return 返回存在的图片集合
     * @author lxy 20160929
     * @throws Exception
     */
    public static List<String> isImageExistsFtpList(String remoteminfo,List<String> imageNameList) throws Exception{
    connectFtpInfo(path, addr, port, username, password);
    setFtpBaseInfo(remoteminfo);
    FTPFile[] tempImageFtpImageList = ftp.listFiles();
    boolean isok = false;
    List<String> returnImageInfo = new ArrayList<String>();
    if(tempImageFtpImageList!=null && tempImageFtpImageList.length>0){
    for (int j = 0; j < imageNameList.size(); j++) {
String oneImageInfo = imageNameList.get(j);
for (int i = 0; i < tempImageFtpImageList.length; i++) {
    FTPFile fTPFileTemp = tempImageFtpImageList[i];
    String imagesuffix = fTPFileTemp.getName();
    if(imagesuffix!=null && imagesuffix.equals(oneImageInfo)){
    returnImageInfo.add(imagesuffix);
    break;
    }
}
}
    }
    return returnImageInfo;
    }
    
    /**
     * 
     * @param remoteminfo  远程服务器地址信息
     * @param imageNameList 存放图片名称的集合信息
     * @return 返回包每个图片流信息
     * @author lxy 20160929
     * @throws Exception
     */
    public static List<InputStream> ftpDownLoadInfo(String remoteminfo,List<String> imageNameList) throws Exception{
    connectFtpInfo(path, addr, port, username, password);
    setFtpBaseInfo(remoteminfo);
    List<InputStream> inputStreamList = new ArrayList<InputStream>();
    for (int j = 0; j < imageNameList.size(); j++) {
String infoiamge = imageNameList.get(j);
inputStreamList.add(ftp.retrieveFileStream(infoiamge));
}
    return inputStreamList;
    } 
    
    /**
     * 
     * @param remoteminfo远程服务器地址信息
     * @param imageName 图片名称
     * @return 单个图片流信息
     * @throws Exception
     * @author lxy 20160929
     */
    public static InputStream ftpDownLoadInfo(String remoteminfo,String imageName,String changemdir) throws Exception{
    path=changemdir;
    connectFtpInfo(path, addr, port, username, password);
       setFtpBaseInfo(remoteminfo);
return ftp.retrieveFileStream(imageName);
    } 
    /**
     * 创建文件夹
     * lxy
     * @param args
     */
    public static boolean  mkdir(String dirName,String directoryName) throws Exception{
    connectFtpInfo(path, addr, port, username, password);
    setFtpBaseInfo(directoryName);
       return  ftp.makeDirectory(dirName);
    } 
    
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值