FTP上传工具类

 

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;

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

public class FtpServiceUtils {
   public static void makeDirectory(String hostname,String username,String password,String path) throws Exception{
      FTPClient ftp = new FTPClient();
      ftp.setDefaultTimeout(10000);
      try{
         ftp.connect(hostname);
         if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
            throw new Exception("FTP服务器无响应");
            }
            if (!ftp.login(username, password)) {
               throw new Exception("FTP用户名密码错误");
            }
            ftp.makeDirectory(path);
            ftp.logout();
      }catch(Exception e){
         e.printStackTrace();
         throw e;
      }finally{
         try {
                if (ftp.isConnected()) {
                    ftp.disconnect();
                }
            } catch (Exception e) {
               throw e;
            }
      }
   }
   
   public static void getFile(String hostname,String username,String password,String remoteFilename,OutputStream out) throws Exception {
      FTPClient ftp = new FTPClient();
      ftp.setDefaultTimeout(10000);
      try{
         ftp.connect(hostname);
         if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
            throw new Exception("FTP服务器无响应");
            }
            if (!ftp.login(username, password)) {
               throw new Exception("FTP用户名密码错误");
            }
            ftp.setFileType(FTP.BINARY_FILE_TYPE);
            ftp.enterLocalPassiveMode();
            if (!ftp.retrieveFile(remoteFilename, out)) {
               throw new Exception(ftp.getReplyString()+ftp.getReply());
            }
            ftp.logout();
      }catch(Exception e){
         e.printStackTrace();
         throw e;
      }finally{
         try {
                if (ftp.isConnected()) {
                    ftp.disconnect();
                }
            } catch (Exception e) {
               throw e;
            }
      }
   }
   public static String dele(String hostname,String username,String password,String pathname) throws Exception {
      FTPClient ftp = new FTPClient();
      ftp.setDefaultTimeout(10000);
      try{
         ftp.connect(hostname);
         if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
            throw new Exception("FTP服务器无响应");
            }
            if (!ftp.login(username, password)) {
               throw new Exception("FTP用户名密码错误");
            }
            ftp.setFileType(FTP.BINARY_FILE_TYPE);
            ftp.enterLocalPassiveMode();
            ftp.removeDirectory(pathname);
            ftp.logout();
      }catch(Exception e){
         e.printStackTrace();
         throw e;
      }finally{
         try {
                if (ftp.isConnected()) {
                    ftp.disconnect();
                }
            } catch (Exception e) {
               throw e;
            }
      }
      
      return null;
   }
   
   
   public static Boolean delFile(String hostname,String username,String password,String remoteFilename) {
      Boolean flag = Boolean.TRUE;
      FTPClient ftp = new FTPClient();
      ftp.setDefaultTimeout(10000);
      try{
         ftp.connect(hostname);
         if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
            throw new Exception("FTP服务器无响应");
            }
            if (!ftp.login(username, password)) {
               throw new Exception("FTP用户名密码错误");
            }
            ftp.setFileType(FTP.BINARY_FILE_TYPE);
            ftp.enterLocalPassiveMode();
            ftp.deleteFile(remoteFilename);
            ftp.logout();
      }catch(Exception e){
         flag = Boolean.FALSE;
         e.printStackTrace();
      }finally{
         try {
                if (ftp.isConnected()) {
                    ftp.disconnect();
                }
            } catch (Exception e) {
            }
      }
      
      return flag;
   }
   /*
   public static void putFile(String hostname,String username,String password,String remoteFilename,InputStream is) throws Exception {
      FTPClient ftp = new FTPClient();
      ftp.setDefaultTimeout(10000);
      try{
         ftp.connect(hostname);
         if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
            throw new Exception("FTP服务器无响应");
            }
            if (!ftp.login(username, password)) {
               throw new Exception("FTP用户名密码错误");
            }
            ftp.setFileType(FTP.BINARY_FILE_TYPE);
            ftp.enterLocalPassiveMode();
            if (!ftp.storeFile(remoteFilename, is)) {
               throw new Exception(ftp.getReplyString()+ftp.getReply());
            }
            ftp.logout();
      }catch(Exception e){
         e.printStackTrace();
         throw e;
      }finally{
         try {
                if (ftp.isConnected()) {
                    ftp.disconnect();
                }
            } catch (Exception e) {
               throw e;
            }
            try {
                is.close();
            } catch (Exception e) {
               throw e;
            }
      }
   }
   */
   public static Boolean putFile(String hostname,String username,String password,String remoteFilename,InputStream is) throws Exception {
      Boolean flag = Boolean.TRUE;
      FTPClient ftp = new FTPClient();
      ftp.setDefaultTimeout(10000);
//    ftp.setDataTimeout(100000);
      try{
         ftp.connect(hostname,21);
         if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
            throw new Exception("FTP服务器无响应");
           }
           if (!ftp.login(username, password)) {
               throw new Exception("FTP用户名密码错误");
           }
           ftp.setFileType(FTP.BINARY_FILE_TYPE);
           ftp.enterLocalPassiveMode();
           if (!ftp.storeFile(remoteFilename, is)) {
               throw new Exception(ftp.getReplyString()+ftp.getReply());
           }
           ftp.logout();
      }catch(Exception e){
         flag = Boolean.FALSE;
         e.printStackTrace();
         throw e;
      }finally{
         try {
               if (ftp.isConnected()) {
                   ftp.disconnect();
               }
           } catch (Exception e) {
           }
           try {
               is.close();
           } catch (Exception e) {
           }
      }
      return flag;
   }
   
   public static FTPFile[]  listFiles(String hostname,String username,String password,String remotePath) throws Exception {
      FTPClient ftp = new FTPClient();
      FTPFile[] list=null;
      ftp.setDefaultTimeout(10000);
      try{
         ftp.connect(hostname);
         if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
            throw new Exception("FTP服务器无响应");
            }
            if (!ftp.login(username, password)) {
               throw new Exception("FTP用户名密码错误");
            }
            list=ftp.listFiles(remotePath);
            ftp.logout();
      }catch(Exception e){
         e.printStackTrace();
         throw e;
      }finally{
         try {
                if (ftp.isConnected()) {
                    ftp.disconnect();
                }
            } catch (Exception e) {
               throw e;
            }
      }
      return list;
   }
   
   public static FTPFile[]  listDirectories(String hostname,String username,String password,String remotePath) throws Exception {
      FTPClient ftp = new FTPClient();
      FTPFile[] list=null;
      ftp.setDefaultTimeout(10000);
      try{
         ftp.connect(hostname);
         if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
            throw new Exception("FTP服务器无响应");
            }
            if (!ftp.login(username, password)) {
               throw new Exception("FTP用户名密码错误");
            }
            list=ftp.listDirectories(remotePath);
            ftp.logout();
      }catch(Exception e){
         e.printStackTrace();
         throw e;
      }finally{
         try {
                if (ftp.isConnected()) {
                    ftp.disconnect();
                }
            } catch (Exception e) {
               throw e;
            }
      }
      return list;
   }
   
   public static Boolean isDirExist(String hostname,String username,String password,String remoteFilename) throws Exception {
      Boolean flag = Boolean.TRUE;
      FTPClient ftp = new FTPClient();
      ftp.setDefaultTimeout(10000);
//    ftp.setDataTimeout(100000);
      try{
         ftp.connect(hostname,21);
         if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
            throw new Exception("FTP服务器无响应");
           }
           if (!ftp.login(username, password)) {
               throw new Exception("FTP用户名密码错误");
           }
           ftp.setFileType(FTP.BINARY_FILE_TYPE);
           ftp.enterLocalPassiveMode();
           
           if (!ftp.changeWorkingDirectory(remoteFilename)) {
              flag = Boolean.FALSE;
           }
           ftp.logout();
      }catch(Exception e){
         flag = Boolean.FALSE;
         e.printStackTrace();
         throw e;
      }finally{
         try {
               if (ftp.isConnected()) {
                   ftp.disconnect();
               }
           } catch (Exception e) {
           }
      }
      return flag;
   }
   
   
   public static Boolean renameFile(String hostname,String username,String password,String pathName,String srcFname,String targetFname) throws Exception {
      Boolean flag = Boolean.TRUE;
      FTPClient ftp = new FTPClient();
      ftp.setDefaultTimeout(10000);
//    ftp.setDataTimeout(100000);
      try{
         ftp.connect(hostname,21);
         if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
            throw new Exception("FTP服务器无响应");
           }
           if (!ftp.login(username, password)) {
               throw new Exception("FTP用户名密码错误");
           }
           ftp.changeWorkingDirectory(pathName);
           ftp.rename(srcFname, targetFname);
           ftp.logout();
      }catch(Exception e){
         flag = Boolean.FALSE;
         e.printStackTrace();
         throw e;
      }finally{
         try {
               if (ftp.isConnected()) {
                   ftp.disconnect();
               }
           } catch (Exception e) {
           }
      }
      return flag;
   }
   
   public static void main(String[] args) throws Exception {  
         //InputStream is = new FileInputStream(new File("D:/11.png"));
         //putFile("106.14.160.98", "ftpuser", "dicc123456", "/home/ftp/ssish/product/test5.jpg", is);
         
         /* FtpUtil f = new FtpUtil("106.14.160.98", 21, "ftpuser", "dicc123456");
          //本地文件目录和文件名 
          String localDirectoryAndFileName=""; 
         //上传后的文件名 
          String ftpFileName=""; 
          //FTP目录如:/path1/pathb2/,如果目录不存在回自动创建目录 
          String ftpDirectory="../ftpadmin/policy/";
          MultipartFile file = (MultipartFile) new File("/");
          f.put(file, localDirectoryAndFileName, ftpFileName, ftpDirectory)
          
          try {  
              if (f.open()) {  
                 FTPFile[] flist=f.getFileList("/home/ftpadmin/policy/34922/fktzs");
                 for (FTPFile ftpFile : flist) {
               System.out.println(ftpFile.getName());
                 }
                 f.close();  
              }  
          } catch (Exception e) {  
    
              e.printStackTrace();  
          }*/
          
      /* try {
          OutputStream os=new FileOutputStream("D:/aaa.png");
           if (f.open()) { 
              ftpClient.retrieveFile("/home/ftpadmin/policy/34922/fktzs/fktzs.png", os);
           }
         os.flush();
      } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }*/
      
         
      }   
}

调用方法

 

public static void main(String[] args) {
   String ftphost = "114.55.39.153";
   String ftpusername ="ftpadmin";
   String ftppassword = "ssinanyan2016";
   String policyId ="12345";
   String temppath="renewalSignFile_"+MessageUtils.base64StrToInteger(policyId);
   try {
      FtpServiceUtil.makeDirectory(ftphost, ftpusername, ftppassword, temppath);
      FtpServiceUtil.putFile(ftphost, ftpusername, ftppassword, temppath + "/renewal00001.pdf", new FileInputStream(new File("/datavg/apache-tomcat-6.0.44/webapps/examples/renewal/renewal00001.pdf")));
   } catch (Exception e) {
      e.printStackTrace();
   }

}

 

 

// 加入pom.xml 依赖

<dependency>
    <groupId>commons-net</groupId>
    <artifactId>commons-net</artifactId>
    <version>3.3</version>
</dependency>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值