`
ianylb
  • 浏览: 73357 次
  • 性别: Icon_minigender_1
  • 来自: 厦门
社区版块
存档分类
最新评论

Java实现Ftp上传文件

    博客分类:
  • Java
阅读更多
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();
}
}
分享到:
评论
3 楼 lancezhcj 2010-11-22  
ianylb 写道
lancezhcj 写道
你确认你的ftpClient.cd(serverPath)没问题,我这不行,就是在dos命令下也不行


ftpClient.cd(serverPath)是没问题的,但要保证serverPath是ftp有权访问的目录。

,嗯,是的,不过ftp中cd的是虚拟目录吧
2 楼 ianylb 2010-11-11  
lancezhcj 写道
你确认你的ftpClient.cd(serverPath)没问题,我这不行,就是在dos命令下也不行


ftpClient.cd(serverPath)是没问题的,但要保证serverPath是ftp有权访问的目录。
1 楼 lancezhcj 2010-11-10  
你确认你的ftpClient.cd(serverPath)没问题,我这不行,就是在dos命令下也不行

相关推荐

Global site tag (gtag.js) - Google Analytics