import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;
import sun.security.action.OpenFileInputStreamAction;
/**=========================================================
* 文件: .MyFtpTest.java
* 所含类: MyFtpTest.java
* 修改记录:
* 日期 作者 内容
* =========================================================
* Jul 5, 2010 yeliangbiao 创建文件,实现基本功能
*/
public class MyFtpTest {
public static void main(String[] args) {
//upload
MyFtpTest ftpTest = new MyFtpTest();
String serverUrl = "127.0.0.1";
String userName = "h";
String passWord = "123";
String uploadFile = "C:\\Documents and Settings\\yelb\\桌面\\改后的domXml.java";
String serverPath = "d:\\";
//上传文件到Ftp服务器
ftpTest.uploadToFtpServer(serverUrl , userName, passWord,
uploadFile, serverPath);
//从FTP服务器上下载文件
ftpTest.downloadFormFtp(serverUrl , userName, passWord,
"毕业生鉴定.doc", serverPath);
//查看FTP服务器上的文件
}
public void uploadToFtpServer(String serverUrl, String userName,
String passWord, String uploadFile, String serverPath){
FtpClient ftpClient;
try {
ftpClient = new FtpClient();//打开ftp客户端
//登录到Ftp服务器
boolean isOpenSuccess = loginToFtpServer(ftpClient, serverUrl, userName, passWord);
if (isOpenSuccess) {//以下几步顺序不能错
//进入服务器的指定目录
ftpClient.cd(serverPath);
OutputStream outputStream = getFtpClientOutputStream(ftpClient, uploadFile);
InputStream inputStream = getLocalFileInputStream(uploadFile);
writeToFtpServer(outputStream, inputStream);
close(ftpClient,outputStream, inputStream);//关闭
}
} catch(Exception e) {
e.printStackTrace();
}
}
/**从Ftp服务器上下载文件*/
public void downloadFormFtp(String serverUrl, String userName,
String passWord, String downLoadFile, String serverPath) {
FtpClient ftpClient = null;
try {
ftpClient = new FtpClient();
boolean isOpenSuccess = loginToFtpServer(ftpClient, serverUrl, userName, passWord);
if (isOpenSuccess) {//以下几步顺序不能错
//进入服务器的指定目录
ftpClient.cd(serverPath);
OutputStream outputStream = new FileOutputStream(new File("E:\\FTP下载.doc"));
InputStream inputStream = getFtpClientInputStream(ftpClient, downLoadFile);
writeToLocal(inputStream, outputStream);
close(ftpClient,outputStream, inputStream);//关闭
}
} catch (Exception e) {
}
}
/** 登录到Ftp服务器 */
public boolean loginToFtpServer(FtpClient ftpClient,String serverUrl,
String userName, String passWord){
try {
ftpClient.openServer(serverUrl);
ftpClient.login(userName, passWord);
return true;
} catch (IOException e) {
System.out.println("Ftp 客户端打开失败");
return false;
}
}
/** 得到ftp客户端的输出流 */
public OutputStream getFtpClientOutputStream(FtpClient ftpClient,
String uploadFile) throws IOException{
ftpClient.binary();
//因为已经进入到了服务的目录,只要在目录下建一个文件就行,所以只要文件名,不要路径
File file = new File(uploadFile);
return ftpClient.put(file.getName());
}
/** 得到ftp客户端的输出流 */
public InputStream getFtpClientInputStream(FtpClient ftpClient,
String downLoadFile) throws IOException{
ftpClient.binary();
//因为已经进入到了服务的目录,只要在目录下建一个文件就行,所以只要文件名,不要路径
File file = new File(downLoadFile);
return ftpClient.get(file.getName());
}
/** 得到本地文件的输入流 */
public InputStream getLocalFileInputStream(String fileName)
throws FileNotFoundException{
return new FileInputStream(new File(fileName));
}
/** 向ftp服务器写入文件 */
public void writeToFtpServer(OutputStream outputStream,
InputStream inputStream) throws IOException {
byte[] bytes = new byte[1024];
while(inputStream.read(bytes) != -1) {
outputStream.write(bytes,0,bytes.length);
}
outputStream.flush();
}
/**下载文件到本地*/
public void writeToLocal(InputStream inputStream,
OutputStream outputStream) throws IOException{
byte[] bytes = new byte[1024];
while(inputStream.read(bytes) != -1) {
outputStream.write(bytes,0,bytes.length);
}
}
/** 关闭 */
public void close(FtpClient ftpClient, OutputStream outputStream,
InputStream inputStream) throws IOException {
if(inputStream != null) inputStream.close();
if(outputStream != null) outputStream.close();
if(ftpClient != null) ftpClient.closeServer();
}
}
分享到:
相关推荐
java实现的ftp文件上传
一款由java实现ftp自动上传整个磁盘所有文件,包括目录,下面的JTextArea读取Log4j及命令行的信息,采用第三方ftpClient.不过下载的部分还没有时间去做.
用JAVA实现Ftp文件上传下载,提供实现方法和测试用例
Java实现FTP文件上传和下载
JAVA实现FTP协议文件上传
java实现ftp上传jar包,里边包括edtftpj.jar、commons-net-1.4.1.jar、jakarta-oro-2.0.8.jar
java实现文件上传到fpt,里面一些很简单的操作适合初学者。
Java,FTP,用Java实现FTP批量大文件上传下载
java实现ftp上传和下载文件功能 文档里有详细的代码
java使用FTP上传文件,非常实用 包括创建ftp链接,上传
用Java实现FTP批量大文件上传下载.doc 用Java实现FTP批量大文件上传下载.doc 用Java实现FTP批量大文件上传下载.doc
Java实现FTP批量大文件上传下载.pdf
PutButton部分为向FTP服务器上传一个文件。 别忘了在程序中还要引入两个库文件(import sun.net.*,import sun.net.ftp.*)。 以下是这三部分的JAVA源程序: (1)显示FTP服务器上的文件 void ftpList_actionPerformed...
用java语言编写的ftp小工具,可以按指定时间监控ftp服务器,把服务器指定目录内新产生的文件或者文件夹下载到本地指定文件夹,下载后删除数据。 也可以监控本地文件夹,把文件夹内新产生的文件或者文件夹整体上传到...
Java实现FTP批量大文件上传下载的东西
这是我使用java实现的linux和ftp服务器文件上传下载工具,需要电脑安装jdk8, 启动命令,java -jar linuxAndFtp.jar 启动成功后,浏览器访问:http://localhost:9999 服务器的账号密码通过服务器列表页面管理,添加的...