小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Apache FTP 實(shí)踐(實(shí)現(xiàn)了附件歸檔的功能)

 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);   // 這個(gè)很重要,否則會出現(xiàn)附件上傳后文件損毀打不開的問題!
        } 
     
     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 "下載配置文件失敗,請聯(lián)系管理員."; 
        } catch (SocketException e) {       
            e.printStackTrace(); 
        } catch (IOException e) { 
            e.printStackTrace(); 
            e.printStackTrace(); 
            return "配置文件讀取失敗,請聯(lián)系管理員."; 
        } 
        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 "配置文件讀取失敗,請聯(lián)系管理員."; 
            }finally{ 
                try { 
                    ftpClient.disconnect(); 
                } catch (IOException e) { 
                    e.printStackTrace(); 
                } 
            } try {
    in.close() ;
   } catch (IOException e) {
    e.printStackTrace();
   }
        }else{ 
            return "配置文件讀取失敗,請聯(lián)系管理員."; 
        } 

        return resultBuffer.toString(); 
    } 

}

    本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多