FTP附件上傳與下載

import java.io.FileInputStream;
import java.io.IOException;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;
import cn.com.ultrapower.ultraprocess.share.*;
public class FtpTool {

 private FtpClient ftp;
 private String romateDir="";
 private String userName="";
 private String password="";
 private String host="";
 private String port="21";
  public FtpTool(String url)throws IOException
  {

   int len=url.indexOf("//");
   String strTemp=url.substring(len+2);
   len=strTemp.indexOf(":");
   userName=strTemp.substring(0,len);
   strTemp=strTemp.substring(len+1);
   
   len=strTemp.indexOf("@");
   password=strTemp.substring(0,len);
   strTemp=strTemp.substring(len+1);
   
   host="";
   len=strTemp.indexOf(":");
   if(len<0)//沒有設置端口
   {
    len=strTemp.indexOf("/");
    host=strTemp.substring(0,len);
    strTemp=strTemp.substring(len+1);
   }else
   {
    host=strTemp.substring(0,len);
    strTemp=strTemp.substring(len+1);
    len=strTemp.indexOf("/");
    port=strTemp.substring(0,len);
    strTemp=strTemp.substring(len+1);
   }
   romateDir=strTemp;
   ftp = new FtpClient();
   ftp.openServer(host, FormatInt.FormatStringToInt(port));
   
  }
  /**
   * 構造方法,新建一個FtpClient對象,並打開FTP服務器
   * @param host FTP服務器地址;port FTP服務端口
   *
   */
  public FtpTool(String host,int port)throws IOException{
   ftp = new FtpClient();
   ftp.openServer(host, port);
  }
 
  /**
   * 登陸方法,通過用戶名密碼登陸到指定的FTP服務器上去,並返回歡迎信息
   * @param username FTP用戶名;password 密碼
   *
   */
  public String login(String username,String password)throws IOException{
   this.ftp.login(username, password);
   if(!romateDir.equals(""))
       ftp.cd(romateDir);  
   return this.ftp.welcomeMsg;
  }
 
  public String login()throws IOException{
    this.ftp.login(userName, password);
    if(!romateDir.equals(""))
        ftp.cd(romateDir);
    return this.ftp.welcomeMsg;
   } 
 
  /**
   * 此方法用來上傳文件。
   * @param pathname 本地路徑;filename 要上傳的文件名稱
   */
  public void upload(String pathname,String filename)throws IOException{
  
   filename=FormatString.CheckNullString(filename);
   if(filename.equals(""))
    return;
     if(!this.ftp.serverIsOpen()){
      System.out.println("服務器連接不可用!");
     }
     this.ftp.binary();
     TelnetOutputStream os = null;
     FileInputStream is = null;
     try {
      //用ftp上傳後的文件名與原文件名相同,同爲filename變量內容
      os = this.ftp.put(filename);
      java.io.File file_in = new java.io.File(pathname+"//"+filename);
      if (file_in.length()==0) {
       System.out.println("上傳文件爲空!");
      }
      is = new FileInputStream(file_in);
      byte[] bytes = new byte[1024];
      int c;
      while ((c = is.read(bytes)) != -1) {
       os.write(bytes, 0, c);
      }
     } finally {
      if (is != null) {
       is.close();
      }
      if (os != null) {
       os.close();
      }
     }
     System.out.println("上傳文件成功!");
     this.ftp.ascii();
  }
 
  public void download()
  {
  
  }

  public void close()
  {
   try
   {
    if(ftp!=null)
     ftp.closeServer();
   }catch(Exception ex)
   {
   
   }
  }

發佈了27 篇原創文章 · 獲贊 2 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章