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

解决方案 »

  1.   

    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;
    }}
      

  2.   

    我就是通过FTPCLIENT实现FTP下载的,但是下载的是装有TOMCAT的应用服务器,并不是浏览器端。
      

  3.   

    先把文件用流读进来,然后直接把流写到response中去,设置response的contextType为file