熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

Java做的比較完善的FTP連接上傳下載文件

2013-11-23 19:13:14  來源: Java核心技術 

  這是偶第一次寫java程序實現公司要求的FTP數據同步所有原代碼如下以供各位參考並提出寶貴意見!另外俺還有兩個功能沒有實現有請高手指點一二如何刪除FTP服務器上已經下載過的文件?如何將上傳的文件改名?上傳時在文件名前加temp_上傳完畢後需要把文件名改回來如何使用openPassiveDataConnection()方法將ftp連接更改為主動連接因為要從unix系統配置的ftp服務器下載文件的話就需要主動連接

  /*

  *******************************************************************************************************

  Filename: ftpjava

  Author: leetsing(elove)

  Create date:

  Use: connect to FTP serverthen upload and download file

  Modify date: add to upload file

   add to download file

  Copy right: Magisky Media Technology CoLtd

  *******************************************************************************************************

  */

  //import czdhlio*;

  //import czdhlftp*;

  import ftp*;

  import *;

  import javaapplet*;

  import javaio*;

  import javaioIOException;

  import javautilStringTokenizer;

  import ftpFtpClient;

  import javautilArrayList;

  public class ftp extends Applet

  {

  FtpClient aftp;

  DataOutputStream outputs ;

  TelnetInputStream ins;

  TelnetOutputStream outs;

  int ch;

  public String a;

  String hostname=;

  private String path = /;

  public static void main(String[] args)

  {

  String hostname = ;

  int port = ;

  String uid = lee;

  String pwd = lee;

  String RWFileDir = D:\\smsftp\\;//文件目錄

  //連接ftp服務器

  ftp ft = new ftp();

  nnect(RWFileDirhostnameportuidpwd);

  //下載文件

  if (ftaftp != null){

  try {

  ftgetNameList(RWFileDir);

  }catch(IOException e)

  {

  Systemoutprintln(下載文件出錯+e);

  }

  }

  //上傳文件

  if (ftaftp != null){

  String sdir = RWFileDir + subunsubfromsp\\;

  File fdir = new File(sdir);

  String FileName = ;

  for(int i=;i FileName = sdir + (fdirlist());

  ftuploadFile(RWFileDirFileName);

  }

  //Systemoutprintln(成功上傳的文件);

  //ftshowFileContents(subunsubfromsp\\);

  }

  //刪除subunsubfromsp目錄下已經上傳的文件文件

  // ftdeleFile(RWFileDir);

  //斷開服務器連接

  ftstop(RWFileDir);

  }

  public FtpClient connect(String RWFileDirString hostnameint portString uidString pwd)

  {

  thishostname = hostname;

  Systemoutprintln(正在連接+hostname+請等待);

  try{

  aftp = new FtpClient(hostnameport);

  aftplogin(uidpwd);

  aftpbinary();

  //aftpopenPortDataConnection();

  a = 連接主機:+hostname+成功!;

  Systemoutprintln(a);

  }

  catch(FtpLoginException e){

  a=登陸主機:+hostname+失敗!請檢查用戶名或密碼是否正確+e;

  Systemoutprintln(a);

  //return false;

  }

  catch (IOException e){

  a=連接主機:+hostname+失敗!請檢查端口是否正確+e;

  Systemoutprintln(a);

  //return false;

  }

  catch(SecurityException e)

  {

  a=無權限與主機:+hostname+連接!請檢查是否有訪問權限+e;

  Systemoutprintln(a);

  //return false;

  }

  log(RWFileDira);

  return aftp;

  }

  public void stop(String RWFileDir)

  {

  String message = ;

  try {

  if(aftp!=null){

  aftpcloseServer();

  message = 與主機+hostname+連接已斷開!;

  Systemoutprintln(message);

  log(RWFileDirmessage);

  }

  }

  catch(IOException e)

  {

  message = 與主機+hostname+斷開連接失敗!+e;

  Systemoutprintln(message);

  log(RWFileDirmessage);

  }

  }

  public boolean downloadFile(String RWFileDirString filepathname){

  boolean result=true;

  String message = ;

  if (aftp != null)

  {

  Systemoutprintln(正在下載文件+filepathname+請等待);

  String badfile = filepathnamesubstring(filepathnamelength()filepathnamelength());

  String badlog = filepathnamesubstring(filepathnamelength()filepathnamelength());

  String baddir = ;

  if ((pareTo(bad) != ) && (pareTo(badlog) != )){

  baddir = subunsubtosp\\;

  }

  else{

  baddir = bad\\;

  }

  String strdir = subunsubtosp\\;

  //Systemoutprintln(RWFileDir + baddir + filepathname);

  try{

  //FtpClient fc=new FtpClient();

  //fclogin(leelee);

  int ch;

  File fi = new File(RWFileDir + baddir + filepathname);

  //aftpcd(strdir);

  RandomAccessFile getFile = new RandomAccessFile(firw);

  getFileseek();

  TelnetInputStream fget=aftpget(strdir+filepathname);

  DataInputStream puts = new DataInputStream(fget);

  while ((ch = putsread()) >= ) {

  getFilewrite(ch);

  }

  //sdelete();

  fgetclose();

  getFileclose();

  //fccloseServer();

  message = 下載+filepathname+文件到+baddir +目錄成功!;

  Systemoutprintln(message);

  log(RWFileDirmessage);

  }

  catch(IOException e){

  message = 下載+filepathname+文件到+baddir +目錄失敗!+e;

  Systemoutprintln(message);

  log(RWFileDirmessage);

  result = false ;

  }

  }

  else{

  result = false;

  }

  return result;

  }

  public boolean uploadFile(String RWFileDirString filepathname){

  boolean result=true;

  String message = ;

  if (aftp != null)

  {

  Systemoutprintln(正在上傳文件+filepathname+請等待);

  try{

  String fg =new String(\\subunsubfromsp\\);

  int index = filepathnamelastIndexOf(fg);

  String filename = filepathnamesubstring(index+);

  File localFile = new File(filepathname) ;

  RandomAccessFile sendFile = new RandomAccessFile(filepathnamer);

  //

  sendFileseek();

  //改名上傳temp_

  filename = filenamesubstring()+temp_+filenamesubstring(filenamelength());

  outs = aftpput(filename);

  outputs = new DataOutputStream(outs);

  while (sendFilegetFilePointer() < sendFilelength() )

  {

  ch = sendFileread();

  outputswrite(ch);

  }

  rename(filenamesubstring(filenamelength())filenamesubstring(filenamelength()));

  outsclose();

  sendFileclose();

  message = 上傳+filepathname+文件成功!;

  Systemoutprintln(message);

  log(RWFileDirmessage);

  }

  catch(IOException e){

  message = 上傳+filepathname+文件失敗!+e;

  Systemoutprintln(message);

  log(RWFileDirmessage);

  result = false ;

  }

  }

  else{

  result = false;

  }

  return result;

  }

  public void rename(String oldNameString newName){

  //aftprenameTo(oldNamenewName);

  File Old = new File(oldName); //oldName

  File New = new File(newName); //newName

  //aftprenameTo(New);

  //boolean OldrenameTo(File newName);

  //Systemoutprintln(Old);

  //Systemoutprintln(New);

  }

  public static void deleFile(String RWFileDir) {

  //try {

  //取得ReadFile目錄下的txt文件

  String sdir = RWFileDir + subunsubfromsp\\;

  File fdir = new File(sdir);

  String FileName = ;

  int j = fdirlist()length;

  Systemoutprintln(sdir+目錄下要刪除的文件數+fdirlist()length);

  File file;

  for(int i=;i {

  //刪除subunsubfromsp中的txt文件

  FileName = RWFileDir + subunsubfromsp\\ + (fdirlist())[];

  file = new File(FileName);

  filedelete();

  Systemoutprintln(已經成功刪除+FileName+文件!);

  }

  //}

  //catch (IOException e) {

  // Systemoutprintln(刪除txt文件錯誤!);

  // eprintStackTrace();

  //}

  }

  public void showFileContents(String strdir)

  {

  StringBuffer buf = new StringBuffer();

  try {

  aftpcd(strdir);

  ins= aftplist();

  while ((ch=insread())>=){

  bufappend((char)ch);

  }

  Systemoutprintln(buftoString());

  insclose();

  }

  catch(IOException e)

  {

  }

  }

  // 返回當前目錄的所有文件及文件夾

  public ArrayList getFileList() throws IOException {

  BufferedReader dr = new BufferedReader(new InputStreamReader(aftplist()));

  ArrayList al = new ArrayList();

  String s = ;

  while ( (s = drreadLine()) != null) {

  aladd(s);

  }

  return al;

  }

  public void setPath(String path) throws IOException {

  if (aftp == null)

  thispath = path;

  else {

  aftpcd(path);

  }

  }

  // 返回當前目錄的文件名稱

  public ArrayList getNameList(String RWFileDir) throws IOException {

  BufferedReader dr = new BufferedReader(new InputStreamReader(aftpnameList(subunsubtosp\\)));

  ArrayList al = new ArrayList();

  String s = ;

  while ( (s = drreadLine()) != null) {

  aladd(s);

  s = ssubstring(slength());

  isFile(s);

  downloadFile(RWFileDirs);

  //String strFileDelF = aftpnameList(subunsubtosp\\);

  File fileDelF=new File(s);

  fileDelFdelete();

  }

  return al;

  //Systemoutprintln(aladd(s));

  }

  // 判斷一行文件信息是否為目錄

  public boolean isDir(String line) {

  return ( (String) parseLine(line)get())indexOf(d) != ;

  }

  public boolean isFile(String line) {

  return!isDir(line);

  }

  // 處理getFileList取得的行信息

  private ArrayList parseLine(String line) {

  ArrayList s = new ArrayList();

  StringTokenizer st = new StringTokenizer(line );

  while (sthasMoreTokens()) {

  sadd(stnextToken());

  }

  return s;

  }

  //寫消息日志

  public static void log(String RWFileDirString msg)

  {

  String message = ;

  try {

  javatextDateFormat df = new javatextSimpleDateFormat(yyyyMMdd HH:mm:ss);

  javatextDateFormat dflog = new javatextSimpleDateFormat(yyyyMMdd);

  javautilDate date = new javautilDate() ;

  String datestr = dfformat(new javautilDate()) ;

  String datelog = dflogformat(new javautilDate()) ;

  //String datelog = datestrsubstring();

  //datelog = datelogreplace( );

  //按日期每天生成一個日志文件

  FileWriter fwl = new FileWriter(RWFileDir + CMSSftp+datelog+logtrue);

  PrintWriter outl = new PrintWriter(fwl);

  outlprintln(datestr + + msg);

  outlclose();

  fwlclose();

  }catch (IOException e) {

  message = 寫log文件錯誤!+e;

  eprintStackTrace();

  log(RWFileDirmessage);

  Systemoutprintln(message);

  }

  }

  }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26458.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.