zoukankan      html  css  js  c++  java
  • ftpUtil写法,记录一下

    这个写法我试过可以用,项目中用到的,记录一下。

    代码:

      1 import org.apache.commons.net.ftp.FTPClient;
      2 import org.apache.commons.net.ftp.FTPFile;
      3 import org.apache.commons.net.ftp.FTPReply;
      4 
      5 import java.io.*;
      6 import java.net.MalformedURLException;
      7 
      8 public class FtpUtils {
      9         //ftp服务器地址
     10         public String hostname = "192.168.1.249";
     11         //ftp服务器端口号默认为21
     12         public Integer port = 21 ;
     13         //ftp登录账号
     14         public String username = "root";
     15         //ftp登录密码
     16         public String password = "123";
     17         
     18         public FTPClient ftpClient = null;
     19         
     20         /**
     21          * 初始化ftp服务器
     22          */
     23         public void initFtpClient() {
     24             ftpClient = new FTPClient();
     25             ftpClient.setControlEncoding("utf-8");
     26             try {
     27                 System.out.println("connecting...ftp服务器:"+this.hostname+":"+this.port); 
     28                 ftpClient.connect(hostname, port); //连接ftp服务器
     29                 ftpClient.login(username, password); //登录ftp服务器
     30                 int replyCode = ftpClient.getReplyCode(); //是否成功登录服务器
     31                 if(!FTPReply.isPositiveCompletion(replyCode)){
     32                     System.out.println("connect failed...ftp服务器:"+this.hostname+":"+this.port); 
     33                 }
     34                 System.out.println("connect successfu...ftp服务器:"+this.hostname+":"+this.port); 
     35             }catch (MalformedURLException e) {
     36                e.printStackTrace(); 
     37             }catch (IOException e) {
     38                e.printStackTrace(); 
     39             } 
     40         }
     41 
     42         /**
     43         * 上传文件
     44         * @param pathname ftp服务保存地址
     45         * @param fileName 上传到ftp的文件名
     46         *  @param originfilename 待上传文件的名称(绝对地址) * 
     47         * @return
     48         */
     49         public boolean uploadFile( String pathname, String fileName,String originfilename){
     50             boolean flag = false;
     51             InputStream inputStream = null;
     52             try{
     53                 System.out.println("开始上传文件");
     54                 inputStream = new FileInputStream(new File(originfilename));
     55                 initFtpClient();
     56                 ftpClient.setFileType(ftpClient.BINARY_FILE_TYPE);
     57                 CreateDirecroty(pathname);
     58                 ftpClient.makeDirectory(pathname);
     59                 ftpClient.changeWorkingDirectory(pathname);
     60                 ftpClient.storeFile(fileName, inputStream);
     61                 inputStream.close();
     62                 ftpClient.logout();
     63                 flag = true;
     64                 System.out.println("上传文件成功");
     65             }catch (Exception e) {
     66                 System.out.println("上传文件失败");
     67                 e.printStackTrace();
     68             }finally{
     69                 if(ftpClient.isConnected()){ 
     70                     try{
     71                         ftpClient.disconnect();
     72                     }catch(IOException e){
     73                         e.printStackTrace();
     74                     }
     75                 } 
     76                 if(null != inputStream){
     77                     try {
     78                         inputStream.close();
     79                     } catch (IOException e) {
     80                         e.printStackTrace();
     81                     } 
     82                 } 
     83             }
     84             return true;
     85         }
     86         /**
     87          * 上传文件
     88          * @param pathname ftp服务保存地址
     89          * @param fileName 上传到ftp的文件名
     90          * @param inputStream 输入文件流 
     91          * @return
     92          */
     93         public boolean uploadFile( String pathname, String fileName,InputStream inputStream){
     94             boolean flag = false;
     95             try{
     96                 System.out.println("开始上传文件");
     97                 initFtpClient();
     98                 ftpClient.setFileType(ftpClient.BINARY_FILE_TYPE);
     99                 CreateDirecroty(pathname);
    100                 ftpClient.makeDirectory(pathname);
    101                 ftpClient.changeWorkingDirectory(pathname);
    102                 ftpClient.storeFile(fileName, inputStream);
    103                 inputStream.close();
    104                 ftpClient.logout();
    105                 flag = true;
    106                 System.out.println("上传文件成功");
    107             }catch (Exception e) {
    108                 System.out.println("上传文件失败");
    109                 e.printStackTrace();
    110             }finally{
    111                 if(ftpClient.isConnected()){ 
    112                     try{
    113                         ftpClient.disconnect();
    114                     }catch(IOException e){
    115                         e.printStackTrace();
    116                     }
    117                 } 
    118                 if(null != inputStream){
    119                     try {
    120                         inputStream.close();
    121                     } catch (IOException e) {
    122                         e.printStackTrace();
    123                     } 
    124                 } 
    125             }
    126             return true;
    127         }
    128         //改变目录路径
    129          public boolean changeWorkingDirectory(String directory) {
    130                 boolean flag = true;
    131                 try {
    132                     flag = ftpClient.changeWorkingDirectory(directory);
    133                     if (flag) {
    134                       System.out.println("进入文件夹" + directory + " 成功!");
    135 
    136                     } else {
    137                         System.out.println("进入文件夹" + directory + " 失败!开始创建文件夹");
    138                     }
    139                 } catch (IOException ioe) {
    140                     ioe.printStackTrace();
    141                 }
    142                 return flag;
    143             }
    144 
    145         //创建多层目录文件,如果有ftp服务器已存在该文件,则不创建,如果无,则创建
    146         public boolean CreateDirecroty(String remote) throws IOException {
    147             boolean success = true;
    148             String directory = remote + "/";
    149             // 如果远程目录不存在,则递归创建远程服务器目录
    150             if (!directory.equalsIgnoreCase("/") && !changeWorkingDirectory(new String(directory))) {
    151                 int start = 0;
    152                 int end = 0;
    153                 if (directory.startsWith("/")) {
    154                     start = 1;
    155                 } else {
    156                     start = 0;
    157                 }
    158                 end = directory.indexOf("/", start);
    159                 String path = "";
    160                 String paths = "";
    161                 while (true) {
    162                     String subDirectory = new String(remote.substring(start, end).getBytes("GBK"), "iso-8859-1");
    163                     path = path + "/" + subDirectory;
    164                     if (!existFile(path)) {
    165                         if (makeDirectory(subDirectory)) {
    166                             changeWorkingDirectory(subDirectory);
    167                         } else {
    168                             System.out.println("创建目录[" + subDirectory + "]失败");
    169                             changeWorkingDirectory(subDirectory);
    170                         }
    171                     } else {
    172                         changeWorkingDirectory(subDirectory);
    173                     }
    174 
    175                     paths = paths + "/" + subDirectory;
    176                     start = end + 1;
    177                     end = directory.indexOf("/", start);
    178                     // 检查所有目录是否创建完毕
    179                     if (end <= start) {
    180                         break;
    181                     }
    182                 }
    183             }
    184             return success;
    185         }
    186 
    187       //判断ftp服务器文件是否存在    
    188         public boolean existFile(String path) throws IOException {
    189                 boolean flag = false;
    190                 FTPFile[] ftpFileArr = ftpClient.listFiles(path);
    191                 if (ftpFileArr.length > 0) {
    192                     flag = true;
    193                 }
    194                 return flag;
    195             }
    196         //创建目录
    197         public boolean makeDirectory(String dir) {
    198             boolean flag = true;
    199             try {
    200                 flag = ftpClient.makeDirectory(dir);
    201                 if (flag) {
    202                     System.out.println("创建文件夹" + dir + " 成功!");
    203 
    204                 } else {
    205                     System.out.println("创建文件夹" + dir + " 失败!");
    206                 }
    207             } catch (Exception e) {
    208                 e.printStackTrace();
    209             }
    210             return flag;
    211         }
    212         
    213         /** * 下载文件 * 
    214         * @param pathname FTP服务器文件目录 * 
    215         * @param filename 文件名称 * 
    216         * @param localpath 下载后的文件路径 * 
    217         * @return */
    218         public  boolean downloadFile(String pathname, String filename, String localpath){ 
    219             boolean flag = false; 
    220             OutputStream os=null;
    221             try { 
    222                 System.out.println("开始下载文件");
    223                 initFtpClient();
    224                 //切换FTP目录 
    225                 ftpClient.changeWorkingDirectory(pathname); 
    226                 FTPFile[] ftpFiles = ftpClient.listFiles(); 
    227                 for(FTPFile file : ftpFiles){ 
    228                     if(filename.equalsIgnoreCase(file.getName())){ 
    229                         File localFile = new File(localpath + "/" + file.getName()); 
    230                         os = new FileOutputStream(localFile);
    231                         ftpClient.retrieveFile(file.getName(), os); 
    232                         os.close(); 
    233                     } 
    234                 } 
    235                 ftpClient.logout(); 
    236                 flag = true; 
    237                 System.out.println("下载文件成功");
    238             } catch (Exception e) { 
    239                 System.out.println("下载文件失败");
    240                 e.printStackTrace(); 
    241             } finally{ 
    242                 if(ftpClient.isConnected()){ 
    243                     try{
    244                         ftpClient.disconnect();
    245                     }catch(IOException e){
    246                         e.printStackTrace();
    247                     }
    248                 } 
    249                 if(null != os){
    250                     try {
    251                         os.close();
    252                     } catch (IOException e) {
    253                         e.printStackTrace();
    254                     } 
    255                 } 
    256             } 
    257             return flag; 
    258         }
    259         
    260         /** * 删除文件 * 
    261         * @param pathname FTP服务器保存目录 * 
    262         * @param filename 要删除的文件名称 * 
    263         * @return */ 
    264         public boolean deleteFile(String pathname, String filename){ 
    265             boolean flag = false; 
    266             try { 
    267                 System.out.println("开始删除文件");
    268                 initFtpClient();
    269                 //切换FTP目录 
    270                 ftpClient.changeWorkingDirectory(pathname); 
    271                 ftpClient.dele(filename); 
    272                 ftpClient.logout();
    273                 flag = true; 
    274                 System.out.println("删除文件成功");
    275             } catch (Exception e) { 
    276                 System.out.println("删除文件失败");
    277                 e.printStackTrace(); 
    278             } finally {
    279                 if(ftpClient.isConnected()){ 
    280                     try{
    281                         ftpClient.disconnect();
    282                     }catch(IOException e){
    283                         e.printStackTrace();
    284                     }
    285                 } 
    286             }
    287             return flag; 
    288         }
    289         
    290         public static void main(String[] args) {
    291             FtpUtils ftp =new FtpUtils(); 
    292             //ftp.uploadFile("ftpFile/data", "123.docx", "E://123.docx");
    293             //ftp.downloadFile("ftpFile/data", "123.docx", "F://");
    294             ftp.deleteFile("ftpFile/data", "123.docx");
    295             System.out.println("ok");
    296         }
    297 }
  • 相关阅读:
    Spring(一)Spring的基本应用
    flask摘记
    python摘记
    String Algorithm
    leetcode -- hard part
    leetcode -- medium part
    leetcodo--Easy part
    unix网络编程
    SQL
    计算机网络知识
  • 原文地址:https://www.cnblogs.com/wangquanyi/p/11328983.html
Copyright © 2011-2022 走看看