分享

Apache FTP 实践(实现了附件归档的功能)

 citycanyon 2012-03-30

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.SocketException;

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

public class SagaFTP {
 
 FTPClient ftpClient ;
    public boolean connectServer(String server, int port, String user,String password) throws SocketException, IOException {  
        ftpClient = new FTPClient();  
        ftpClient.setControlEncoding("GBK");
       
        ftpClient.connect(server, port == 0 ? 21: port);  
        System.out.println("Ftp Connected to " + server + "."); 
        boolean  login =  ftpClient.login(user, password);
        return login ;     
        
    }
   
    public boolean uploadFile(String fileName, String newName, String path) throws IOException {
     if (path.length() != 0) {
         ftpClient.makeDirectory(path);
            ftpClient.changeWorkingDirectory(path);
            ftpClient.setFileType(ftpClient.BINARY_FILE_TYPE);   // 这个很重要,否则会出现附件上传后文件损毁打不开的问题!
        } 
     
     boolean flag = false;  
     InputStream iStream = null;  
  try {  
      iStream = new FileInputStream(fileName);  
      flag = ftpClient.storeFile(newName, iStream);
      System.out.println("File upload is successful!");
  } catch (IOException e) {  
      flag = false;  
      return flag;  
  } finally {
   try {
     if (iStream != null) {  
                iStream.close();  
         }
   } catch (IOException e) {
    e.printStackTrace();
   }finally{
    ftpClient.logout();  
                ftpClient.disconnect(); 
            }
  }   
  return flag;  
    }
   
    public void downLoadFile(String ftpPath, String fileName , OutputStream os){
     
        try {
         ftpClient.setControlEncoding("GBK"); 
   ftpClient.changeWorkingDirectory(ftpPath);
   ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); 
         ftpClient.enterLocalPassiveMode(); 
   ftpClient.retrieveFile(fileName, os) ;
  } catch (IOException e) {
   e.printStackTrace();
  }finally{
   try {
    os.close() ;
   } catch (IOException e) {
    e.printStackTrace();
   }finally{
    try {
     ftpClient.logout();  
                 ftpClient.disconnect(); 
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
   
  }
    }
   
    public String readFile(String ftpPath, String fileName) { 
        StringBuffer resultBuffer = new StringBuffer(); 
        FileInputStream inFile = null; 
        InputStream in = null; 
        try {            
            ftpClient.setControlEncoding("UTF-8"); 
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); 
            ftpClient.enterLocalPassiveMode(); 
            ftpClient.changeWorkingDirectory(ftpPath); 
            in = ftpClient.retrieveFileStream(fileName); 
        } catch (FileNotFoundException e) {         
            e.printStackTrace(); 
            return "下载配置文件失败,请联系管理员."; 
        } catch (SocketException e) {       
            e.printStackTrace(); 
        } catch (IOException e) { 
            e.printStackTrace(); 
            e.printStackTrace(); 
            return "配置文件读取失败,请联系管理员."; 
        } 
        if (in != null) { 
            BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
            String data = null; 
            try { 
                while ((data = br.readLine()) != null) { 
                    resultBuffer.append(data + "\n"); 
                }
                br.close() ;
            } catch (IOException e) { 
                e.printStackTrace(); 
                return "配置文件读取失败,请联系管理员."; 
            }finally{ 
                try { 
                    ftpClient.disconnect(); 
                } catch (IOException e) { 
                    e.printStackTrace(); 
                } 
            } try {
    in.close() ;
   } catch (IOException e) {
    e.printStackTrace();
   }
        }else{ 
            return "配置文件读取失败,请联系管理员."; 
        } 

        return resultBuffer.toString(); 
    } 

}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多