Java实现ftp上传文件、文件夹
2012-04-06 09:49
477 查看
import java.io.File; import java.io.FileInputStream; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPReply; public class test { 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; } /** * * @param file 上传的文件或文件夹 * @throws Exception */ private void upload(File file) throws Exception{ 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); 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()); FileInputStream input = new FileInputStream(file2); ftp.storeFile(file2.getName(), input); input.close(); } } public static void main(String[] args) throws Exception{ test t = new test(); t.connect("", "localhost", 21, "yhh", "yhhazr"); File file = new File("e:\\uploadify"); t.upload(file); } }
相关文章推荐
- Java实现ftp上传文件、文件夹
- Java实现ftp上传下载、删除文件及在ftp服务器上传文件夹的方法
- Java实现ftp上传文件、文件夹
- Java实现ftp上传文件、文件夹
- Java实现ftp上传文件、文件夹
- Java实现FTP文件与文件夹的上传和下载
- Java实现ftp上传文件、文件夹
- Java实现ftp上传文件、文件夹
- Java实现FTP文件与文件夹的上传和下载
- Java实现FTP文件与文件夹的上传和下载
- Java实现ftp文件上传下载解决慢中文乱码多个文件下载等问题
- Java代码实现FTP服务器上传文件与下载文件
- Java FtpClient 实现文件上传服务
- Java ftp实现文件的上传和下载ftp,sftp sun.net.ftp.FtpProtocolException:Welcome message: SSH-2.0-OpenSSH_5.1
- Java连接FTP服务器并且实现对其文件的上传和下载
- java ftp实现文件上传和删除
- 请问哪位高手知道用Apache Commons Net包实现javaFTP上传整个目录(文件夹)
- python 实现统计ftp服务器指定目录下文件夹数目、文件数目及所有文件大小 本次主要为满足应用方核对上传到ftp服务器的文件是否缺漏。 主要要求:指定目录下,文件夹数目/文件数目/所有文件大小
- Java连接FTP服务器并且实现对其文件的上传和下载
- Java FTP 实现跨服务器文件上传下载