package com.xiaomo.ftp.down_upload;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
一、建立一个类,用来获取ftp对应目录下的目录和文件信息。
public class ListMapFtp {
public static List<Map> showList(String hostname,int port,String username,String password,String pathname) throws IOException{
FTPClient ftpClient = new FTPClient();
List<String> list = new ArrayList<String>();
List<Map> listMap = new ArrayList<Map>();
Map<String, String> mapFile = new HashMap<String, String>();
Map<String, String> mapDirectory = new HashMap<String, String>();
try{
int reply;
ftpClient.connect(hostname, port);
ftpClient.login(username, password);
reply = ftpClient.getReplyCode();
if(!FTPReply.isPositiveCompletion(reply)){
ftpClient.disconnect();
}
ftpClient.changeWorkingDirectory(pathname+"/");
FTPFile[] ftpFiles = ftpClient.listFiles();
for(FTPFile ftpFile:ftpFiles){
if(ftpFile.getType()==1&&!ftpFile.getName().equals(".")&&!ftpFile.getName().equals("..")){
list.add(pathname+"/"+ftpFile.getName());
mapDirectory.put(ftpFile.getName(), pathname+"/"+ftpFile.getName());
}
}
for(FTPFile ftpFile:ftpFiles){
if(ftpFile.getType()==0){
list.add(ftpFile.getName());
}
}
listMap.add(mapDirectory);
listMap.add(mapFile);
ftpClient .logout();
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
return listMap;
}
}
二、建立一个servlet类,通过网页显示对应的ftp目录和文件信息
package com.xiaomo.ftp.down_upload;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class ServletFtp extends HttpServlet {
ListMapFtp listMapFtp = null;
DownloadFtp downloadFtp = null;
public void service(HttpServletRequest request,HttpServletResponse response)
throws ServletException ,IOException{
String appPath = request.getContextPath();//获得当前项目的相对路径
HttpSession session = request.getSession();
String remotePath = request.getParameter("remotePath");//获得当前路径
if(remotePath!=null){
System.out.println("remotePath:"+remotePath);
session.setAttribute("sessionPath", remotePath);//将当前路径保存到session中
}
if(remotePath==null){
remotePath = "";
}
String filename = request.getParameter("filename");//获得当前文件的名称
if(filename!=null){
System.out.println("filename: "+filename);
}
listMapFtp = new ListMapFtp();
List<Map> list = listMapFtp.showList("192.168.**.**", 21, "*****", "******",remotePath);//获得ftp对应路径下的所有目录和文件信息
Map<String,String> mapDirectory = list.get(0);//获得ftp该路径下的所有目录信息
Map<String,String> mapFile = list.get(1);//获得ftp该路径下所有的文件信息
PrintWriter out = response .getWriter();
out.print("<html><head><title>信息提示页</title></head><body>");
out.print("<a style='color:red' href='http://localhost:8080/xiaomo/ftpList?remotePath='>首页</a><br/><br/>");
if(remotePath!=null&&filename==null){//如果前台点击的是目录则显示该目录下的所有目录和文件
if(mapDirectory.size()>1){
//out.println("<h3>目录</h3>");
for(String str : mapDirectory.keySet()){
out.print("<img src='"+appPath+"/pic/directory.jpg' width='25px' height='25px'> <a style='color:blue' href='http://localhost:8080/xiaomo/ftpList?remotePath="+mapDirectory.get(str)+"'>"+str+"</a><br/>");
}
}
if(mapFile.size()>1){
//out.println("<h3>文件</h3>");
for(String str : mapFile.keySet()){
out.print("<img src='"+appPath+"/pic/file.jpg' width='20px' height='20px'> <a style='color:green' href='http://localhost:8080/xiaomo/ftpList?filename="+mapFile.get(str)+"'>"+str+"</a><br/>");
}
}
}else if(filename!=null){//如果前台点击的是文件,则下载该文件
downloadFtp = new DownloadFtp();
String sessionPath = (String) session.getAttribute("sessionPath");//获得保存在session中的当前路径信息
System.out.println("session--->sessionPath"+sessionPath);
downloadFtp.downFile("192.168.50.23", 21, "admin", "123456",sessionPath,filename, "D:/test/download/");
//调用方法下载该文件
out.println("<h3>"+filename+" 下载成功!</h3>");
out.print("<a style='color:blue' href='http://localhost:8080/xiaomo/ftpList?remotePath="+sessionPath+"'>返回</a><br/>");
}
out.print("</body></html>");
}
}
三、配置web.xml,通过网页访问http://localhost:8080/xiaomo/ftpList 效果如下:
出处:http://blog.csdn.net/cl05300629/article/details/9703539 作者:伫望碧落