FTP上传下载删除

来源:互联网 发布:淘宝宠物狗 编辑:程序博客网 时间:2024/06/05 17:26

导入包:commons-net-xx.jar

import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import org.apache.commons.net.ftp.FTP;import org.apache.commons.net.ftp.FTPClient;import org.apache.commons.net.ftp.FTPClientConfig;import org.apache.commons.net.ftp.FTPFile;import org.apache.commons.net.ftp.FTPReply;public class FtpUtil {    private static String CONTROL_ENCODING = "GBK";    private static int DATE_TIMEOUT = 60000;//设置传输超时时间为60秒     private static int CONNECT_TIMEOUT = 460000;//设置链接超时时间为60秒     private static FTPClient ftp;    //登陆服务     public static void loginFTPService(String host, int port, String username, String password){         ftp = new FTPClient();        try {            int reply = 0;             ftp.setDataTimeout(DATE_TIMEOUT);                   ftp.setConnectTimeout(CONNECT_TIMEOUT);                 ftp.connect(host, port);            reply = ftp.getReplyCode();             if (!FTPReply.isPositiveCompletion(reply)) {                  ftp.disconnect();              }              ftp.enterLocalPassiveMode();            FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);               conf.setServerLanguageCode("zh");             ftp.setControlEncoding(CONTROL_ENCODING);            ftp.setFileType(FTP.ASCII_FILE_TYPE);            ftp.setFileType(FTP.BINARY_FILE_TYPE);          } catch (Exception e) {            e.printStackTrace();        }     }     //上传文件     public static void uploadFile( String basePath,String filePath, String filename, InputStream input) {              try {                  //切换到上传目录                  if (!ftp.changeWorkingDirectory(basePath+filePath)) {                      //如果目录不存在创建目录                      String[] dirs = filePath.split("/");                      String tempPath = basePath;                      for (String dir : dirs) {                          if (null == dir || "".equals(dir)) continue;                          tempPath += "/" + dir;                          if (!ftp.changeWorkingDirectory(tempPath)) {                              if (!ftp.makeDirectory(tempPath)) {                              } else {                                  ftp.changeWorkingDirectory(tempPath);                              }                          }                      }                  }                  //上传文件                  ftp.storeFile(new String(filename.getBytes("GBK"),"ISO-8859-1"), input);                input.close();                  ftp.logout();              } catch (IOException e) {                  e.printStackTrace();              } finally {                  if (ftp.isConnected()) {                      try {                          ftp.disconnect();                      } catch (IOException ioe) {                      }                  }              }          }       //下载文件     public static void downloadFile(String remotePath,String fileName, String localPath) {              try {                  ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录                  FTPFile[] fs = ftp.listFiles();                  for (FTPFile ff : fs) {                      if (ff.getName().equals(fileName)) {                          File localFile = new File(localPath + "/" + ff.getName());                          OutputStream is = new FileOutputStream(localFile);                          ftp.retrieveFile(ff.getName(), is);                          is.close();                      }                  }              } catch (IOException e) {                  e.printStackTrace();              }          }       //删除文件     public static void deleteFtpFile( String remotePath,String fileName)  {        try {            // 转移到FTP服务器目录            ftp.deleteFile(remotePath + "/" + fileName);        } catch (IOException e) {            e.printStackTrace();        }    }    //删除文件目录    public static  void deleteFTPdir(String pathName) throws Exception {        ftp.enterLocalPassiveMode();        FTPFile[] files = ftp.listFiles(pathName);        if (null != files && files.length > 0) {            for (FTPFile file : files) {                if (file.isDirectory()) {                    deleteFTPdir(pathName +"/"+ file.getName());                    ftp.changeWorkingDirectory(pathName.substring(0,pathName.lastIndexOf("/")));                    ftp.removeDirectory(pathName);                }else{                    ftp.deleteFile(pathName+"/"+file.getName());                }            }        }        ftp.changeWorkingDirectory(pathName.substring(0,pathName.lastIndexOf("/")));        ftp.removeDirectory(pathName);    }    //退出ftp服务    public static void logoutFTPService() throws IOException{        if(ftp != null){            ftp.logout();        }    }}