javascript学习指南_Java FTP上传下载删除功能实例代码

更新时间:2021-07-19    来源:Action    手机版     字体:

【www.bbyears.com--Action】

FTP上传下载,容易出现乱码,记得转换

 

 代码如下

packagecom.yinhai.team.action;

importjava.io.File;

importjava.io.FileInputStream;

importjava.io.FileOutputStream;

importjava.io.IOException;

importjava.io.InputStream;

importjava.io.OutputStream;

importorg.apache.commons.net.ftp.FTPClient;

importorg.apache.commons.net.ftp.FTPFile;

importorg.apache.commons.net.ftp.FTPReply;

publicclassFavFTPUtil {

   /**

    * 上传文件(可供Action/Controller层使用)未测试

    * @param hostname FTP服务器地址

    * @param port FTP服务器端口号

    * @param username FTP登录帐号

    * @param password FTP登录密码

    * @param pathname FTP服务器保存目录

    * @param fileName 上传到FTP服务器后的文件名称

    * @param inputStream 输入文件流

    * @return

    */

   publicstaticbooleanuploadFile(String  hostname,intport, String username, String password, String pathname,  String fileName, InputStream inputStream){

    booleanflag =false;

    FTPClient ftpClient =newFTPClient();

    ftpClient.setControlEncoding("UTF-8");

    try{

     //连接FTP服务器

     ftpClient.connect(hostname, port);

     //登录FTP服务器

     ftpClient.login(username, password);

     //是否成功登录FTP服务器

     intreplyCode = ftpClient.getReplyCode();

     if(!FTPReply.isPositiveCompletion(replyCode)){

      returnflag;

     }

     ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);

     ftpClient.makeDirectory(pathname);

     ftpClient.changeWorkingDirectory(pathname);

     ftpClient.storeFile(fileName, inputStream);

     inputStream.close();

     ftpClient.logout();

     flag =true;

    }catch(Exception e) {

     e.printStackTrace();

    }finally{

     if(ftpClient.isConnected()){

      try{

       ftpClient.disconnect();

      }catch(IOException e) {

       e.printStackTrace();

      }

     }

    }

    returnflag;

   }

   /**

    * 上传文件(可对文件进行重命名)未测试

    * @param hostname FTP服务器地址

    * @param port FTP服务器端口号

    * @param username FTP登录帐号

    * @param password FTP登录密码

    * @param pathname FTP服务器保存目录

    * @param filename 上传到FTP服务器后的文件名称

    * @param originfilename 待上传文件的名称(绝对地址)

    * @return

    */

   publicstaticbooleanuploadFileFromProduction(String  hostname,intport, String username, String password, String pathname,  String filename, String originfilename){

    booleanflag =false;

    try{

     InputStream inputStream =newFileInputStream(newFile(originfilename));

     flag = uploadFile(hostname, port, username, password, pathname, filename, inputStream);

    }catch(Exception e) {

     e.printStackTrace();

    }

    returnflag;

   }

   /**

    * 上传文件(不可以进行文件的重命名操作) 未测试

    * @param hostname FTP服务器地址

    * @param port FTP服务器端口号

    * @param username FTP登录帐号

    * @param password FTP登录密码

    * @param pathname FTP服务器保存目录

    * @param originfilename 待上传文件的名称(绝对地址)

    * @return

    */

   publicstaticbooleanuploadFileFromProduction(String  hostname,intport, String username, String password, String pathname,  String originfilename){

    booleanflag =false;

    try{

     String fileName =newFile(originfilename).getName();

     InputStream inputStream =newFileInputStream(newFile(originfilename));

     flag = uploadFile(hostname, port, username, password, pathname, fileName, inputStream);

    }catch(Exception e) {

     e.printStackTrace();

    }

    returnflag;

   }

   /**

    * 删除文件 未测试

    * @param hostname FTP服务器地址

    * @param port FTP服务器端口号

    * @param username FTP登录帐号

    * @param password FTP登录密码

    * @param pathname FTP服务器保存目录

    * @param filename 要删除的文件名称

    * @return

    */

   publicstaticbooleandeleteFile(String hostname,intport, String username, String password, String pathname, String filename){

    booleanflag =false;

    FTPClient ftpClient =newFTPClient();

    try{

     //连接FTP服务器

     ftpClient.connect(hostname, port);

     //登录FTP服务器

     ftpClient.login(username, password);

     //验证FTP服务器是否登录成功

     intreplyCode = ftpClient.getReplyCode();

     if(!FTPReply.isPositiveCompletion(replyCode)){

      returnflag;

     }

     //切换FTP目录

     ftpClient.changeWorkingDirectory(pathname);

     ftpClient.dele(filename);

     ftpClient.logout();

     flag =true;

    }catch(Exception e) {

     e.printStackTrace();

    }finally{

     if(ftpClient.isConnected()){

      try{

       ftpClient.logout();

      }catch(IOException e) {

      }

     }

    }

    returnflag;

   }

   /**

    * 下载文件

    * @param hostname FTP服务器地址

    * @param port FTP服务器端口号

    * @param username FTP登录帐号

    * @param password FTP登录密码

    * @param pathname FTP服务器文件目录

    * @param filename 文件名称

    * @param localpath 下载后的文件路径

    * @return

    */

  publicstaticbooleandownloadFile(String  hostname,intport, String username, String password, String pathname,  String filename, String localpath){

    booleanflag =false;

    FTPClient ftpClient =newFTPClient();

    try{

     //连接FTP服务器

     ftpClient.connect(hostname, port);

     //登录FTP服务器

     ftpClient.login(username, password);

     //验证FTP服务器是否登录成功

     intreplyCode = ftpClient.getReplyCode();

     if(!FTPReply.isPositiveCompletion(replyCode)){

      returnflag;

     }

     //切换FTP目录

     booleanb = ftpClient.changeWorkingDirectory(newString(pathname.getBytes(),"ISO-8859-1"));

     System.out.println(b);

     FTPFile[] ftpFiles = ftpClient.listFiles();

     for(FTPFile file : ftpFiles){

       String fName =newString(file.getName().getBytes("iso-8859-1"),"UTF-8");

       System.out.println(fName);

      if(filename.equalsIgnoreCase(fName)){

       File localFile =newFile(localpath +"/"+ fName);

       OutputStream os =newFileOutputStream(localFile);

       ftpClient.retrieveFile(file.getName(), os);

       os.close();

      }

     }

     ftpClient.logout();

     flag =true;

    }catch(Exception e) {

     e.printStackTrace();

    }finally{

     if(ftpClient.isConnected()){

      try{

       ftpClient.logout();

      }catch(IOException e) {

      }

     }

    }

    returnflag;

   }

}

 

本文来源:http://www.bbyears.com/flash/130667.html

热门标签

更多>>

本类排行