看了
http://dengli19881102.iteye.com/blog/1028957我想其中的变量path 应该是待上传的文件在客户端的路径吧。我通过java程序从数据库里查询出若干条数据,然后拼凑成xml格式的字符串。我想把这个字符串直接输出到ftp服务器中的一个文件夹下,变成xml文件,请问怎么做哦?
http://dengli19881102.iteye.com/blog/1028957我想其中的变量path 应该是待上传的文件在客户端的路径吧。我通过java程序从数据库里查询出若干条数据,然后拼凑成xml格式的字符串。我想把这个字符串直接输出到ftp服务器中的一个文件夹下,变成xml文件,请问怎么做哦?
所以你先得在本地生成文件,然后上传到ftp服务器的某个目录下
FTPFile[] fs = ftp.listFiles(); // 得到目录的相应文件列表
总是空数组,不知道怎么搞的哦?我总觉得我传的path有问题,因为这个变量是存储的客户端路径呀,而不是服务端ftp的路径呀
请求帮助
lz可以用下apache的commons-net-3.0.1.jar参考下get()方法.import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.SocketException;import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.log4j.Logger;public class FtpClientUtil{
static final private Logger logger = Logger.getLogger(FtpClientUtil.class);
private FTPClient ftp;
/**
* 构造器(端口:21)
* @param ip ftp server ip
* @param username 用户名
* @param passwd 密码
* @throws SocketException
* @throws IOException
*/
public FtpClientUtil(String ip,
String username, String passwd
) throws SocketException, IOException{
this(ip, 21, username, passwd);
}
/**
* 构造器
* @param ip ftp server ip
* @param port ftp server port
* @param username 用户名
* @param passwd 密码
* @throws SocketException
* @throws IOException
*/
public FtpClientUtil(String ip, int port,
String username, String passwd
) throws SocketException, IOException{
this.ftp = new FTPClient();
init(ip, port, username, passwd);
}
/**
* 初始化
* @param ip
* @param port
* @param username
* @param passwd
* @throws SocketException
* @throws IOException
*/
private void init(String ip, int port, String username, String passwd) throws SocketException, IOException{
// this.ftp.setSoTimeout(1000 * 30); //30秒超时
this.ftp.connect(ip, port);
if(!this.ftp.login(username, passwd)){
throw new RuntimeException("login incorrect.");
}
setBinaryType();
}
/**
* 设置传输类型:二进制<br/>
* <b>FtpUtil的默认类型</b>
* @throws IOException
*/
public void setBinaryType() throws IOException{
this.ftp.setFileType(FTP.BINARY_FILE_TYPE);
}
/**
* 设置传输类型:asc ii
* @throws IOException
*/
public void setASCIIType() throws IOException{
this.ftp.setFileType(FTP.ASCII_FILE_TYPE);
}
/**
* 切换到另一个目录
* @param remoteDir
* @return true:切换成功,false:切换失败
* @throws IOException
*/
public boolean chdir(String remoteDir) throws IOException{
return ftp.changeWorkingDirectory(remoteDir);
}
/**
* 删除远程文件
* @param remoteDir 远程目录
* @param file 文件名
* @throws IOException
*/
public void delete(String file) throws IOException{
ftp.deleteFile(file);
}
/**
* 下载一个远程文件到本地目录下,并重命名为localFile
* @param remoteFile 远程文件名
* @param lcoalFile 本地文件名
* @param localDir 本地目录
* @return true:下载完成,false:下载失败
* @exception IOException
*/
public boolean get(String remoteFile, String localFile, String localDir) throws IOException{
boolean ok = false;
OutputStream out = null;
File file = null;
long fileLen = 0;
try{
file = new File(localDir, localFile);
if(!file.exists()){
file.createNewFile();
fileLen = 0;
}else{
fileLen = file.length();
}
out = new FileOutputStream(file);
ftp.setBufferSize(512);
//断点续传,有的ftp server不支持这个命令,但并不报错.
ftp.setRestartOffset(fileLen);
ok = ftp.retrieveFile(remoteFile, out);
}finally{
try{
if(null != out) out.close();
}catch(Exception ex){
}
}
return ok;
}
/**
* 下载一个远程文件到本地目录下
* @param downFile 要下载的文件名
* @param localDir 本地保存目录
* @return true:下载完成,false:下载失败
* @throws IOException
*/
public boolean get(String downFile, String localDir) throws IOException{
boolean ok = false;
OutputStream out = null;
File file = null;
long fileLen = 0;
try{
file = new File(localDir, downFile);
if(!file.exists()){
file.createNewFile();
fileLen = 0;
}else{
fileLen = file.length();
}
out = new FileOutputStream(file);
ftp.setBufferSize(512);
//断点续传,有的ftp server不支持这个命令,但并不报错.
ftp.setRestartOffset(fileLen);
ok = ftp.retrieveFile(downFile, out);
}finally{
try{
if(null != out) out.close();
}catch(Exception ex){
}
}
return ok;
}
/**
* 登出并关闭连接
*/
public void logout(){
try{
if(null != ftp) ftp.logout();
}catch(Exception ex){
}
try{
if(null != ftp) ftp.disconnect();
}catch(Exception ex){
}
}
}
http://blog.csdn.net/liuyuhua0066/article/details/6411197