求 org.apache.commons.net.ftp.FTPClient 设置主动传输模式完整代码 如题,服务器端拒绝被动模式上传。求高手给解决方案,顺便说下注意事项,谢谢。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 拒绝被动模式,就只能主动模式了。不过主动模式,只能在同一个局域网中才能使用。给段代码参考下, 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; } 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; } //设置被动模式 ftpClient.enterLocalPassiveMode(); 很久没发帖了,请教一下Runtime本身的设计问题 请教一个问题,关于读取文件,传递的是对象,附代码...在线等 关于JRE和JDK的区别,我还是不太明白 急,在线等! 上海罗恩网络有限公司招J2EE工程师 把eclipse左边的工程窗口弄没了。。。 寻找兼职程序员 JBuilder的光标和字符怎么对不上? 一个小程序都编译不过去,是我的环境有问题么? 求救:关于表格中单元格合并拆分的问题! 如何将netbeans做好的界面导入到eclipse 关于JNI的一个小问题!求解
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;
}
看看这个例子: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;
}
ftpClient.enterLocalPassiveMode();