必威体育Betway必威体育官网
当前位置:首页 > IT技术

FTP上传工具类

时间:2019-11-04 11:41:10来源:IT技术作者:seo实验室小编阅读:55次「手机版」
 

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>

文章最后发布于: 2019-01-25 14:29:36

相关阅读

CSS3属性选择器及伪类选择器

CSS3相比CSS2在选择器中增加了通配符的概念,赋予其强大功能。 E[attr^="val"] 定义了属性attr,并且以val开头的任意字符串 E[att

网盘搜索工具整理

平时收集资料需要使用各类网盘搜索工具,在此特作一个收集连接不定期更新失效连接。按日常使用度来排行 胖次 网盘搜索引擎 西林

适合学生站长搭建的网站类型有哪些?

又是一年开学季,对于大一新生来说,即将打开新的世界。大学,不仅仅是个学习专业知识的地方,更是培养个人多种能力的最佳场所。随着互联

小咖秀&#038;模样,短视频类APP竞品分析报告

1.短视频应用的发展趋势随着科技的不断发展,信息的传播方式一定会朝着表现力越来越强、包含的信息量越来越多的方向发展。现如今,单

Linux FTP客户端

  1.File Zilla File Zilla是一个开源的,跨平台的Linux FTP客户端。File Zilla有一个标签式的用户界面,允许用户查看正在传输的

分享到:

栏目导航

推荐阅读

热门阅读