求 org.apache.commons.net.ftp.FTPClient 设置主动传输模式完整代码

javaaihao897 2011-07-18 11:08:21
如题,服务器端拒绝被动模式上传。求高手给解决方案,顺便说下注意事项,谢谢。
...全文
1255 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
javaaihao897 2011-07-19
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 wula0010 的回复:]

//设置被动模式
ftpClient.enterLocalPassiveMode();
[/Quote]
本来就是被动模式。因为被动模式被服务器拒绝才选择主动模式的。
AlexLee8810 2011-07-18
  • 打赏
  • 举报
回复
路过,就为下点东西,容易吗?
Mybeautiful 2011-07-18
  • 打赏
  • 举报
回复
拒绝被动模式,就只能主动模式了。不过主动模式,只能在同一个局域网中才能使用。

给段代码参考下,


public boolean download(String remoteFile, String localFile) {
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
boolean flag = false;
try {
boolean flag2 = connectServer();
if (flag2) {
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.enterLocalActiveMode(); //这句重要,不行换enterRemoteActiveMode 看看
ftpClient.setFileTransferMode(FTP.STREAM_TRANSFER_MODE);

ftpClient.setBufferSize(1024);
ftpClient.setControlEncoding("UTF-8");
FTPClientConfig conf = new FTPClientConfig(
FTPClientConfig.SYST_NT);
conf.setServerLanguageCode("zh");

File file_in = new File(localFile);
FileOutputStream fos = new FileOutputStream(file_in);
flag = ftpClient.retrieveFile(directory + remoteFile, fos);
ftpClient.disconnect();
}
} catch (IOException ex) {
// flag = false;
System.out.println("not download");
System.out.println(ex);
} finally {
try {
ftpClient.disconnect();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return flag;
}

wula0010 2011-07-18
  • 打赏
  • 举报
回复
ftp.enterLocalActiveMode()这个可以设置被动模式上传。
看看这个例子:

public DownloadStatus download(String remote,String local) throws IOException{
//设置被动模式
ftpClient.enterLocalPassiveMode();
//设置以二进制方式传输
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
DownloadStatus result;

//检查远程文件是否存在
FTPFile[] files = ftpClient.listFiles(new String(remote.getBytes("GBK"),"iso-8859-1"));
if(files.length != 1){
System.out.println("远程文件不存在");
return DownloadStatus.Remote_File_Noexist;
}

long lRemoteSize = files[0].getSize();
File f = new File(local);
//本地存在文件,进行断点下载
if(f.exists()){
long localSize = f.length();
//判断本地文件大小是否大于远程文件大小
if(localSize >= lRemoteSize){
System.out.println("本地文件大于远程文件,下载中止");
return DownloadStatus.Local_Bigger_Remote;
}

//进行断点续传,并记录状态
FileOutputStream out = new FileOutputStream(f,true);
ftpClient.setRestartOffset(localSize);
InputStream in = ftpClient.retrieveFileStream(new String(remote.getBytes("GBK"),"iso-8859-1"));
byte[] bytes = new byte[1024];
long step = lRemoteSize /100;
long process=localSize /step;
int c;
while((c = in.read(bytes))!= -1){
out.write(bytes,0,c);
localSize+=c;
long nowProcess = localSize /step;
if(nowProcess > process){
process = nowProcess;
if(process % 10 == 0)
System.out.println("下载进度:"+process);
//TODO 更新文件下载进度,值存放在process变量中
}
}
in.close();
out.close();
boolean isDo = ftpClient.completePendingCommand();
if(isDo){
result = DownloadStatus.Download_From_Break_Success;
}else {
result = DownloadStatus.Download_From_Break_Failed;
}
}else {
OutputStream out = new FileOutputStream(f);
InputStream in= ftpClient.retrieveFileStream(new String(remote.getBytes("GBK"),"iso-8859-1"));
byte[] bytes = new byte[1024];
long step = lRemoteSize /100;
long process=0;
long localSize = 0L;
int c;
while((c = in.read(bytes))!= -1){
out.write(bytes, 0, c);
localSize+=c;
long nowProcess = localSize /step;
if(nowProcess > process){
process = nowProcess;
if(process % 10 == 0)
System.out.println("下载进度:"+process);
//TODO 更新文件下载进度,值存放在process变量中
}
}
in.close();
out.close();
boolean upNewStatus = ftpClient.completePendingCommand();
if(upNewStatus){
result = DownloadStatus.Download_New_Success;
}else {
result = DownloadStatus.Download_New_Failed;
}
}
return result;
}

jtchan 2011-07-18
  • 打赏
  • 举报
回复
“服务器端拒绝被动模式上传”是什么意思?
wula0010 2011-07-18
  • 打赏
  • 举报
回复
//设置被动模式
ftpClient.enterLocalPassiveMode();
javaaihao897 2011-07-18
  • 打赏
  • 举报
回复
用了二楼的方法,还是上传不了。为神马呢?

62,616

社区成员

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

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