ftp上传

类别:Java 点击:0 评论:0 推荐:

     在一般的web应用中,文件上传可以分为两类(ftp上传和http上传)。其中http上传是在页面的form表单中加入<input type=“file“ name=“file“>类型的接口,当表单被提交时,file中所指的文件就会被写入request对象中。那么在服务器端,用户只要根据rfc1867协议解析request中的数据流就可以得到文件,但是这样上传的速度很慢了,并且对文件的大小又要求。相比之下,ftp上传就很优越了。他是ftp客户端直接与ftp服务器进行交互,将文件创传到ftp服务器。当然,ftp客户端已经被实现。下面我们就应用第三方ftp客户端实现ftp上传。

     首先,从www.enterprisedt.com下载ftp客户端。整个应用公有五个类组成Upload.java,FileUpload.java,DirectoryUpload.java,Directory.java,UploadException.java.希望大家改进。

/*
 * @(#)Upload.java 1.0 04/11/17
 * 实现上传前的初始化
 */
package com.head.upload;

//调用第三方ftp客户端,可在www.enterprisedt.com下载
import com.enterprisedt.net.ftp.*;
import java.io.*;


public class Upload {
  
   static FTPClient ftpClient;
   static String path;

    //ftp服务器的ip地址,登陆用户和密码
   String remoteAddress="127.0.0.1",userName="xie",password="xie";
  
  
   Upload(){}
  
    Upload(String path){
      this.path = path;
    }
    
    public boolean config(String fileName) throws UploadException {
      //从配置文件中读取远端地址,用户名和密码(没实现)
      return false;   
    }
  
    public  void init() throws UploadException {
       try{    
        ftpClient = new FTPClient(this.remoteAddress);
         ftpClient.login(this.userName,this.password);
         ftpClient.setConnectMode(FTPConnectMode.PASV);
         ftpClient.setType(FTPTransferType.BINARY);
       }catch(Exception e)
       {
        throw new UploadException(e.toString(),"Upload.init()");
       }
    
    }
   

    public boolean upload() throws UploadException {
     File file = null;
     boolean flag = false;
     try{
       init();
      
       file = new File(path);
       if(file.isFile() && new FileUpload().uploadFile())       
          flag = true;
       else if(file.isDirectory() && new DirectoryUpload().uploadFile(path))
          flag = true;
        }catch(Exception e)
     {
           throw new UploadException(e.toString(),"Upload.upload()");     
     }
     finally{
      clear(ftpClient);
     }
     return flag;
    }
   
   
    boolean clear(FTPClient ftpClient) throws UploadException {
       try{
         ftpClient.quit();
         return true;
       }catch(Exception e)
       {
        throw new UploadException(e.toString(),"Upload.clear(ftpClient)");
       }
    }
   
         }  
}


/*
 * @(#)FileUpload.java 1.0 04/11/17
 *
 * 上传单个文件
 */
package com.head.upload;

import com.enterprisedt.net.ftp.*;
import java.io.*;


public  class FileUpload extends Upload {
 

 
 FileUpload(){}
            
   
    public boolean uploadFile() throws UploadException{
       //the name of file will be changed to avoid iteration
       String fileName = path.substring(path.lastIndexOf("\\")+1);
       try{    
         ftpClient.put(path,fileName);
         return true;   
       }catch(Exception e){
         throw new UploadException(e.toString(),"FileUpload.uploadFile()");      
       }
    }
   
    public boolean uploadFile(String name) throws UploadException{
       //the name of file will be changed to avoid iteration
       String fileName = name.substring(name.lastIndexOf("\\")+1);
       try{ 
         ftpClient.put(name,fileName);
         return true;   
       }catch(Exception e){
         throw new UploadException(e.toString(),"FileUpload.uploadFile(name)");      
       }
    }
   
  
 }


*
 * @(#)DirectoryUpload.java 1.0 04/11/17
 *
 * 上传文件夹
 */
package com.head.upload;

import com.enterprisedt.net.ftp.*;
import java.io.*;


public  class DirectoryUpload extends Upload {
 

 
 
 DirectoryUpload(){}
 
   
              
    public boolean uploadFile(String dir) throws UploadException{
       //the name of directory will be changed to avoid iteration    
       String dirName = dir.substring(dir.lastIndexOf("\\")+1);
       ftpMkDir(dirName);
      
       try{     
         String currentDir = ftpClient.pwd();
         ftpClient.chdir(dirName);
         String[] subFiles = new Directory(dir).fileNames();
         for(int i=0;i<subFiles.length;i++) {
           String tmpP = dir +"\\" + subFiles[i];
           if(new File(tmpP).isFile()) {   
             System.out.println("upload file:"+tmpP);       
             new FileUpload().uploadFile(tmpP);
           }
           else{
             System.out.println("into the directory:"+tmpP);
             uploadFile(tmpP);
           }
         }   
         ftpClient.chdir(currentDir);
       }catch(Exception e){
         throw new UploadException(e.toString(),"DirecotryUpload.uploadFile()");      
       }
      
       return false;
    }
   
   
   
    private  synchronized void ftpMkDir(String dirName) throws UploadException{
      try{
     ftpClient.mkdir(dirName);
      }catch(Exception e)
      {
        throw new UploadException(e.toString(),"DirecotryUpload.ftpMkDir(dirName)"); 
      }
    }
   
  
 }


/*
 * @(#)Directory.java 1.0 04/11/17
 *
 *
 */
package com.head.upload;

import com.enterprisedt.net.ftp.*;
import java.io.*;


public  class Directory {
 
 private String path;
 
 Directory(){}
   
    Directory(String path) {
       this.path = path;
    }            
   
    //返回一个目录下所有的文件
    String[] fileNames() {
      String[] subNames = null;
      try{
       File file = new File(path);
       subNames = file.list();
      }catch(Exception e)
       {
         System.out.println(e.toString());
       }
      return subNames;
    }
   

   }

 

/*
 * @(#)UploadException.java 1.0 04/11/17
 *
 
 *
 */
package com.head.upload;

 

public class UploadException extends Exception {
  
   UploadException(String eInfo,String location) {
      String info = "The error("+eInfo+") occur in "+location;
       System.out.println(info);
    }

}


好了,就这么多了。其中要更改的地方已经注明,希望大家有时间实现

 

本文地址:http://com.8s8s.com/it/it12979.htm