有哪位知道如何虚拟ftp本地服务器

bind_er 2008-12-16 01:22:06
先谢谢各位!
我在测试一段代码,但是报错信息是:
.E
Time: 20.547
There was 1 error:
1) testDownLoadFile(com.natec.ftp.FtpToolTest)java.lang.Exception: ftp
download file error:Connect ftp server error:Welcome message:
at com.natec.ftp.FtpTool.downloadFile(FtpTool.java:71)
at com.natec.ftp.FtpToolTest.testDownLoadFile(FtpToolTest.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke
(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke
(DelegatingMethodAccessorImpl.java:25)
at com.natec.ftp.FtpToolTest.main(FtpToolTest.java:22)

FAILURES!!!
Tests run: 1, Failures: 0, Errors: 1

代码如下:
import sun.net.ftp.*;
import sun.net.*;
import java.io.*;

/**
* 使用sun.net.ftp工具包进行ftp上传下载
* @author maochangming
* @date 2008-6-20 13:09:29
* @description:
*/
public class FtpTool {
String ip;
int port;
String user;
String pwd;
String remotePath;
String localPath;
FtpClient ftpClient;

/**
* 连接ftp服务器
* @param ip
* @param port
* @param user
* @param pwd
* @return
* @throws Exception
*/
public boolean connectServer(String ip, int port, String user, String
pwd)
throws Exception {
boolean isSuccess = false;
try {
ftpClient = new FtpClient();
ftpClient.openServer(ip, port);
ftpClient.login(user, pwd);
isSuccess = true;
} catch (Exception ex) {
throw new Exception("Connect ftp server error:" + ex.getMessage
());
}
return isSuccess;
}

/**
* 下载文件
* @param remotePath
* @param localPath
* @param filename
* @throws Exception
*/
public void downloadFile(String remotePath,String localPath, String
filename) throws Exception {
try {
if (connectServer(getIp(), getPort(), getUser(), getPwd())) {
if (remotePath.length() != 0)
ftpClient.cd(remotePath);
ftpClient.binary();
TelnetInputStream is = ftpClient.get(filename);
File file_out = new File(localPath + File.separator +
filename);
FileOutputStream os = new FileOutputStream(file_out);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
os.write(bytes, 0, c);
}
is.close();
os.close();
ftpClient.closeServer();
}
} catch (Exception ex) {
throw new Exception("ftp download file error:" + ex.getMessage
());
}
}

/**
* 上传文件
* @param remotePath
* @param localPath
* @param filename
* @throws Exception
*/
public void uploadFile(String remotePath,String localPath, String
filename) throws Exception {
try {
if (connectServer(getIp(), getPort(), getUser(), getPwd())) {
if (remotePath.length() != 0)
ftpClient.cd(remotePath);
ftpClient.binary();
TelnetOutputStream os = ftpClient.put(filename);
File file_in = new File(localPath + File.separator +
filename);
FileInputStream is = new FileInputStream(file_in);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
os.write(bytes, 0, c);
}
is.close();
os.close();
ftpClient.closeServer();
}
} catch (Exception ex) {
throw new Exception("ftp upload file error:" + ex.getMessage
());
}
}

/**
* @return
*/
public String getIp() {
return ip;
}

/**
* @return
*/
public int getPort() {
return port;
}

/**
* @return
*/
public String getPwd() {
return pwd;
}

/**
* @return
*/
public String getUser() {
return user;
}

/**
* @param string
*/
public void setIp(String string) {
ip = string;
}

/**
* @param i
*/
public void setPort(int i) {
port = i;
}

/**
* @param string
*/
public void setPwd(String string) {
pwd = string;
}

/**
* @param string
*/
public void setUser(String string) {
user = string;
}

/**
* @return
*/
public FtpClient getFtpClient() {
return ftpClient;
}

/**
* @param client
*/
public void setFtpClient(FtpClient client) {
ftpClient = client;
}

/**
* @return
*/
public String getRemotePath() {
return remotePath;
}

/**
* @param string
*/
public void setRemotePath(String string) {
remotePath = string;
}

/**
* @return
*/
public String getLocalPath() {
return localPath;
}

/**
* @param string
*/
public void setLocalPath(String string) {
localPath = string;
}

}

...全文
88 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
sjbtwo 2008-12-16
  • 打赏
  • 举报
回复
顶一个

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧