FTPClient 创建远程文件并向文件里写内容!!!
核心代码:
for (int i = 0; i < list.size(); i++) {
RemoteDirBean dirbean = (RemoteDirBean) list.get(i);
String dirs = dirbean.getFirstdir() + "/"
+ dirbean.getSeconddir() + "/" + dirbean.getThirddir();
/**
* 向远程服务器写文件
*/
if(ftpClient.changeWorkingDirectory(dirs)){
System.out.println(ftpClient.printWorkingDirectory());
OutputStreamWriter out = new OutputStreamWriter(ftpClient
.appendFileStream("java" + i + ".txt"));
BufferedWriter pw = new BufferedWriter(out);
pw.write(" this is a test ");
pw.flush();
pw.close();
}else{
logger.info("更改目录失败!");
}
}
写第一个文件时没问题,但是循环第二次的时候,就打出“更改目录失败”,ftpClient.changeWorkingDirectory(dirs)成false了,这是怎么回事?
------解决方案--------------------ftpClient.changeWorkingDirectory(dirs)的返回BOOLEAN是不是表示是否成功切换到dirs目录?
如果是的话,那dirs可能不正确.
------解决方案--------------------菜鸟来帮您看看哇 随便帮您顶顶 我没有用过的 我去查查看哇
随便学习额
------解决方案--------------------
是不是当指向了当前的目录的时候 你再用 changeWorkingDirectory();的时候 他就成false了
我感觉如果LZ的其他代码写得对的话 应该只有这个问题哇
Change the current working directory of the FTP session.
Returns:
True if successfully completed, false if not.
要不LZ多帖点代码 然后一起看看额?
------解决方案--------------------package nc.ui.doc.doc_007;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import
java.io.IOException;
import nc.itf.doc.DocDelegator;
import nc.vo.doc.doc_007.DirVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.SuperVO;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.commons.net.ftp.FTP;
public class FtpTool {
private FTPClient ftp;
private String romateDir = "";
private String userName = "";
private String password = "";
private String host = "";
private String port = "21";
public FtpTool(String url) throws
IOException {
//String url="ftp://user:password@ip:port/ftptest/psd";
int len = url.indexOf("//");
String strTemp = url.substring(len + 2);
len = strTemp.indexOf(":");
userName = strTemp.substring(0, len);
strTemp = strTemp.substring(len + 1);
len = strTemp.indexOf("@");
password = strTemp.substring(0, len);
strTemp = strTemp.substring(len + 1);
host = "";
len = strTemp.indexOf(":");
if (len < 0)//没有设置端口
{
port = "21";
len = strTemp.indexOf("/");
if (len > -1) {
host = strTemp.substring(0, len);
strTemp = strTemp.substring(len + 1);
} else {
strTemp = "";
}
} else {
host = strTemp.substring(0, len);
strTemp = strTemp.substring(len + 1);
len = strTemp.indexOf("/");
if (len > -1) {
port = strTemp.substring(0, len);