基于 apache 的 commons-net-3.3.jar 的 ftp java代码上传下载文件
2013-11-28 17:14
1166 查看
package com.genomics.ib.item.control; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; import org.apache.commons.net.ftp.FTPReply; /** * @author * package com.genomics.ib.item.control * @title ItemFtp * @Description : FTP 上传下载工具类 * @time 2013-11-27 */ public class ItemFtp { private FTPClient ftp; /** * * @param path 上传到ftp服务器哪个路径下 * @param addr 地址 * @param port 端口号 * @param username 用户名 * @param password 密码 * @return * @throws Exception */ private boolean connect(String path,String addr,int port,String username,String password) throws Exception { boolean result = false; ftp = new FTPClient(); int reply; ftp.connect(addr,port); ftp.login(username,password); ftp.setFileType(FTPClient.BINARY_FILE_TYPE); reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return result; } ftp.changeWorkingDirectory(path); result = true; return result; } /** * @author * @class ItemFtp * @title upload * @Description : * @time 2013 2013-11-27 * @return void * @exception :(Error note) * @param file 上传的文件或文件夹 * @param path 上传的文件的路径 * @throws Exception */ private void upload(File file , String path) throws Exception{ System.out.println( " file.isDirectory() : " + file.isDirectory() ); if(file.isDirectory()){ ftp.makeDirectory(file.getName()); ftp.changeWorkingDirectory(file.getName()); String[] files = file.list(); for (int i = 0; i < files.length; i++) { File file1 = new File(file.getPath()+"\\"+files[i] ); if(file1.isDirectory()){ upload(file1 , path ); ftp.changeToParentDirectory(); }else{ File file2 = new File(file.getPath()+"\\"+files[i]); FileInputStream input = new FileInputStream(file2); ftp.storeFile(file2.getName(), input); input.close(); } } }else{ File file2 = new File(file.getPath()); System.out.println( " file.getPath() : " + file.getPath() + " | file2.getName() : " + file2.getName() ); InputStream input = new FileInputStream(file2); ftp.changeWorkingDirectory(path); ftp.storeFile(file2.getName(), input); input.close(); //关闭输入流 ftp.logout(); //退出连接 } } /** * @author * @class ItemFtp * @title download * @Description : FPT 下载文件方法 * @time 2013 2013-11-27 * @return void * @exception :(Error note) * @param reomvepath 下载的文件的路径 * @param fileName 下载的文件名 * @param localPath 下载的文件本地路径 * @throws Exception */ @SuppressWarnings("unused") private void download(String reomvepath , String fileName , String localPath ) throws Exception{ ftp.changeWorkingDirectory(reomvepath); // 列出该目录下所有文件 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); System.out.println("下载成功!"); is.close(); } } ftp.logout(); //退出连接 } public static void main(String[] args) throws Exception{ ItemFtp t = new ItemFtp(); boolean lianjie = t.connect("D:\\", "127.0.0.1", 21, "jiang", "jiang"); System.out.println( "连接 :" + lianjie ); //上传 // File file = new File("d:\\test.txt"); // t.upload(file , "E:\\ftptest\\mulu"); //下载 t.download("E:\\ftptest\\mulu", "test.txt", "D:\\db"); } }借助jar包 apache 的 commons-net-3.3.jar
相关文章推荐
- 基于apache的commons-net-3.3.jar的 ftp java代码上传下载文件
- apache 的 commons-net-3.3.jar 的 ftp java代码上传下载文件
- Java实现Ftp上传和下载文件(依赖commons-net-3.3.jar)--FTPUtil.java
- apache-commons-net Ftp 进行文件、文件夹的上传下载及日志的输出
- apache-commons-net Ftp 进行文件、文件夹的上传下载及日志的输出
- org.apache.commons.net.ftp.FTPClient----根据文件名,路径检索文件 FTP上传 下载 删除文件
- java基于Apache FTP实现文件上传、下载、修改文件名、删除
- apache-commons-net Ftp 进行文件、文件夹的上传下载及日志的输出
- apache-commons-net Ftp 进行文件、文件夹的上传下载及日志的输出
- 利用Apache的commons-net实现FTP的文件读取、上传、下载、删除和移动功能
- java基于Apache FTP点断续传的文件上传和下载
- FTP文件的上传和下载---org.apache.commons.net.ftp.FTPClient
- org.apache.commons.net.ftp.FTPClient文件上传下载,中文乱码
- apache-commons-net Ftp 进行文件、文件夹的上传下载及日志的输出
- org.apache.commons.net.ftp.FTPClient上传、下载、修改文件等功能
- FTP文件的上传和下载---org.apache.commons.net.ftp.FTPClient
- apache-commons-net Ftp 进行文件、文件夹的上传下载及日志的输出
- java FTP 工具类 同步文件夹 下载文件..等操作(FtpHelper 需要commons-net-3.5.jar)
- Java ftp实现文件的上传和下载ftp,sftp sun.net.ftp.FtpProtocolException:Welcome message: SSH-2.0-OpenSSH_5.1
- FTP方式的文件上传和下载的Java代码