当前位置:编程学习 > JAVA >>

通过应用服务器下载FTP服务器上的文件?

   我今天尝试在tomcat上架构个应用,B/S模式,用户通过jsp 下载FTP服务器上的文件到客户端,是否可以行。

   今天下载的文件都到tomcat架构的服务器上了。 --------------------编程问答-------------------- 搞个软链接把目录挂到本地不就好了 --------------------编程问答-------------------- 可以啊 FtpClient.... --------------------编程问答--------------------
引用 1 楼 cxzucc 的回复:
搞个软链接把目录挂到本地不就好了


什么意思? --------------------编程问答-------------------- public static boolean downFile(String remotePath,String fileName,String localPath) {
boolean success = false;
FTPClient ftp = new FTPClient();
try {
int reply;
String url = "192.168.*.*";
Integer port = 21;
String username = "anonymous";
String password = "";
ftp.connect(url, port);
ftp.login(username, password);//登录
reply = ftp.getReplyCode();

//此段为了能下载带有中文名字的文件
ftp.setControlEncoding("GBK");
FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_UNIX);
conf.setServerLanguageCode("zh");

if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return success;
}
ftp.changeWorkingDirectory(remotePath);//转移到FTP服务器目录
ftp.enterLocalPassiveMode();  //不加此段系统一直在等待中
FTPFile[] fs = ftp.listFiles();
for(FTPFile ff:fs){
if(ff.getName().equals(fileName)){
File localFile = new File(localPath+"\\"+ff.getName());
    OutputStream is = new FileOutputStream(localFile); 
ftp.retrieveFile(ff.getName(), is);
is.close();
}
}
ftp.logout();
success = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return success;
}

}
--------------------编程问答-------------------- 我就是通过FTPCLIENT实现FTP下载的,但是下载的是装有TOMCAT的应用服务器,并不是浏览器端。 --------------------编程问答-------------------- 先把文件用流读进来,然后直接把流写到response中去,设置response的contextType为file --------------------编程问答-------------------- 我试试看。
引用 6 楼 zhuyibin2012 的回复:
先把文件用流读进来,然后直接把流写到response中去,设置response的contextType为file
补充:Java ,  Web 开发
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,