我用jsp实现ftp上传功能,客户端不能上传文件

csuluolei 2010-09-17 03:26:24
我用jsp实现ftp上传功能,但是客户端不能往服务器端传上传文件,服务器端自己可以上传文件,代码如下:
private String ftpServer;
private String ftpPort;
private String ftpUserName;
private String ftpPassword;
private FTPClient ftpClient;
private boolean isLogin = false;

public void connServer(String pFtpServer, String pFtpPort,
String pFtpUserName, String pFtpPassword) throws
Exception {
this.ftpServer = pFtpServer;
if (pFtpPort.trim().equals("")) {
this.ftpPort = "21";
} else {
this.ftpPort = pFtpPort;
}
if (pFtpUserName.trim().equals("")) {
this.ftpUserName = "Anonymous";
} else {
this.ftpUserName = pFtpUserName;
}
this.ftpPassword = pFtpPassword;
try {
ftpClient = new FTPClient(); //ftpServer, Integer.parseInt(ftpPort)
ftpClient.setRemoteHost(ftpServer);
ftpClient.setRemotePort(Integer.parseInt(ftpPort));
ftpClient.setControlEncoding("gb2312"); //加上这一句后在 edtftpj 2.0.1 下就要可以传中文文件名了
System.out.println("开始登录");
ftpClient.connect();
ftpClient.login(ftpUserName, ftpPassword);
System.out.println("登录成功");
ftpClient.chdir("\\"); //在有的ftp服务器运行会出错,用ftpClient.chdir("/")又可以了
System.out.println("已转入根目录");
isLogin = true;
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}

//上传指定文件夹到ftp服务器上
//@param folderName 本地要上传的文件夹全路径
//@param ftpPath FTP上对于根目录的路径
public String uploadFolder(String folderName, String ftpPath) throws
Exception {
if (isLogin) {
String strMsg = "";
try {
File file = new File(folderName);

if (file.isDirectory()) {
ftpClient.chdir("\\");
ftpClient.setType(FTPTransferType.BINARY);

if (checkFolderIsExist(ftpPath)) {
ftpClient.chdir(ftpPath);
} else {
createFolder(ftpPath);
}

if (!checkFolderIsExist(file.getName())) {
ftpClient.mkdir(file.getName());
}

ftpClient.chdir(file.getName());

ftpPath = ftpPath + "\\" + file.getName();
File[] files = file.listFiles();

for (int i = 0; i < files.length; i++) {
// System.out.println("==============================");
// System.out.println("ftpPath:"+ftpPath);
// System.out.println("files["+i+"]:"+files[i].getPath());
// System.out.println("==============================");

if (files[i].isDirectory()) {
uploadFolder(files[i].getPath(), ftpPath);
} else {
if (files[i].isFile()) {
try {
//===========================方法一开始================================
//ftpClient.chdir("\\");
//ftpClient.chdir(ftpPath);
//ftpClient.put(files[i].getPath(),files[i].getName());
//===========================方法一结束=================================
//
//===========================方法二开始================================
uploadFile(files[i].getPath(), ftpPath);
//===========================方法二结束================================
} catch (Exception ee) {
strMsg += "upload file<<:" +
files[i].getPath() +
">> error!Message:" + ee.getMessage() +
"\r\n";
}
}
}
}
if (!strMsg.equals("")) {
throw new Exception(strMsg);
}
} else {
System.out.println("file:" + file.getName());
throw new Exception(folderName + " is not a folder'name!");
}
} catch (Exception e) {
strMsg += e.getMessage() + "\r\n";
}
return strMsg;
} else {
throw new Exception("you didnot login remote ftp server!");
}
}
哪位大侠帮忙解决下?
...全文
276 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
suixindong 2011-11-12
  • 打赏
  • 举报
回复
同样遇到此问题 。
看见有人说用applet 或者 ocx ,还能在详细介绍下吗?
shapan1989 2010-11-01
  • 打赏
  • 举报
回复
我也遇到这个问题没解决 你要是解决了给小弟说一下哦
csuluolei 2010-09-17
  • 打赏
  • 举报
回复
在jsp中使用smartupload上传单个文件都没有问题,应该不是crazylaa你说的问题吧!
csuluolei 2010-09-17
  • 打赏
  • 举报
回复
applet,我试试
csuluolei 2010-09-17
  • 打赏
  • 举报
回复
zn85600301,我的说的意思就是:代码功能是好的 客服端不能成功,在服务器上可以
crazylaa 2010-09-17
  • 打赏
  • 举报
回复
jsp运行在服务器,当然不能操作客户端文件。
你用applet或ocx组件。
zn85600301 2010-09-17
  • 打赏
  • 举报
回复
没明白 你意思代码功能是好的 客服端不能成功,在服务器上可以?
YangWenChaoX 2010-09-17
  • 打赏
  • 举报
回复
观望 ...

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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