首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

FTP工具种

2012-12-18 
FTP工具类/** ** @author ruanzhiyong6496 * @version 1.0 */public class FtpUtil{private static FTPCli

FTP工具类

/** *  * @author ruanzhiyong6496 * @version 1.0 */public class FtpUtil{private static FTPClient ftpClient = new FTPClient();private static Properties prop = System.getProperties();static{try{InputStream in = FtpUtil.class.getClassLoader().getResourceAsStream("util.properties");prop.load(in);}catch (IOException e){e.printStackTrace();}}/** * 私有构造什么也不做仅仅是为了不让别人直接创建一个FtpUtil实例 */private FtpUtil(){}/** * 登录方法 *  */private static void login() throws SocketException, IOException{String server = (String) prop.get("ftp.server");// ftp服务器地址int port = Integer.parseInt(prop.get("ftp.port").toString());// ftp服务器端口String username = (String) prop.get("ftp.username");// 登录名String password = (String) prop.get("ftp.password");// 登录密码// 链接到ftp服务器ftpClient.connect(server, port);System.out.println("连接到ftp服务器:" + server + " 成功");System.out.println("开始登录...");// 登录.用户名 密码ftpClient.login(username, password);System.out.println("登录成功.");}/** * 登出方法 *  */private static void logout() throws IOException{if (ftpClient.isConnected()){ftpClient.disconnect();}System.out.println("已关闭ftp连接");}/** * 查找某目录下的文件信息 *  */public static void listDir(String dir){try{login();FTPFile[] files = ftpClient.listFiles(dir);System.out.println("目录" + dir + "下的文件:");if (files != null){for (int i = 0; i < files.length; i++){String name = files[i].getName();long length = files[i].getSize();String readableLength = FileUtils.byteCountToDisplaySize(length);System.out.println(name + ":\t\t" + readableLength);}}}catch (Exception e){e.printStackTrace();}finally{try{logout();}catch (IOException e){e.printStackTrace();}}}/** * 上传方法 *  */public static boolean upload(String filePath, String desDir){boolean flag = false;InputStream in = null;try{login();ftpClient.changeWorkingDirectory(desDir);int index = filePath.lastIndexOf("/");String fileName = filePath.substring(index + 1);in = new FileInputStream(new File(filePath));flag = ftpClient.storeFile(fileName, in);System.out.println("上传文件成功");}catch (Exception e){flag = false;System.out.println("上传文件失败");}finally{try{in.close();logout();}catch (IOException e){e.printStackTrace();}}return flag;}/** * 下载方法 *  */public static boolean download(String remoteFile, String localDir){int index = remoteFile.lastIndexOf("/");String dir = remoteFile.substring(0, index);String fileName = remoteFile.substring(index + 1);FileOutputStream fos = null;InputStream is = null;try{login();ftpClient.changeWorkingDirectory(dir);// 第一步:设置基本属性ftpClient.setBufferSize(1024);ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);// 第二步:获取远程文件的输入流is = ftpClient.retrieveFileStream(remoteFile);if (is == null){// 如果输入流为空,则表示要下载的远程文件不存在System.out.println("要下载的远程文件" + remoteFile + "不存在");return false;}else{// 如果输入流不为空,则将远程文件的输入流写到本地fos = new FileOutputStream(localDir + fileName);byte[] buffer = new byte[1024];int i = -1;while ((i = is.read(buffer)) != -1){fos.write(buffer, 0, i);}System.out.println("下载文件成功");return true;}}catch (Exception e){e.printStackTrace();System.out.println("下载文件失败");}finally{// 关闭输入输出流IOUtils.closeQuietly(is);IOUtils.closeQuietly(fos);try{logout();}catch (IOException e){e.printStackTrace();}}return true;}/** * 删除指定文件方法 *  */public static boolean delFile(String filePath){try{login();ftpClient.deleteFile(filePath);int status = ftpClient.getReplyCode();if (status == 550){System.out.println("文件" + filePath + "不存在");return false;}else if (status == 250){System.out.println("成功删除FTP服务器中文件" + filePath);return true;}}catch (IOException e){e.printStackTrace();System.out.println("删除FTP服务器中文件" + filePath + "失败");}finally{try{logout();}catch (IOException e){e.printStackTrace();}}return true;}}

?

热点排行