日期:2014-05-16  浏览次数:20387 次

jsp如何下载FTP上的文件 .
一共两段程序,可对比一下找出区别

①---ftp协议方式 

一、创建java类:

package ftp;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.StringTokenizer;

import sun.net.TelnetInputStream;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;

public class FtpFile {

 String serverIp = "192.168.12.49";

 String serverUser = "admin";

 String serverPwd = "admin";


 FtpClient aftp;

 DataOutputStream outputs;

 TelnetInputStream ins;

 TelnetOutputStream outs;

 int ch;

 public String a;

 String hostname = "";

 private String path = "/";


 public FtpClient connect(String RWFileDir, String hostname, int port,
   String uid, String pwd) {
  /*
   * this.hostname = hostname;
   * System.out.println("正在连接"+hostname+",请等待....."); try{ aftp = new
   * FtpClient(hostname,port); aftp.login(uid,pwd); aftp.binary();
   * //aftp.openPortDataConnection(); a = "连接主机:"+hostname+"成功!";
   * System.out.println(a); } catch(FtpLoginException e){
   * a="登陆主机:"+hostname+"失败!请检查用户名或密码是否正确:"+e; System.out.println(a);
   * //return false; } catch (IOException e){
   * a="连接主机:"+hostname+"失败!请检查端口是否正确:"+e; System.out.println(a); //return
   * false; } catch(SecurityException e) {
   * a="无权限与主机:"+hostname+"连接!请检查是否有访问权限:"+e; System.out.println(a);
   * //return false; }
   */

  // log(RWFileDir,a);
  FtpClient ftpClient = null;
  try {
   ftpClient = new FtpClient();
   // 创建FtpClient对象
   ftpClient.openServer(hostname);
   // 连接FTP服务器
   ftpClient.login(uid, pwd);
   // 登录FTP服务器
   aftp = ftpClient;
   aftp.cd(RWFileDir);
  } catch (IOException ex) {
   ;
  }
  return ftpClient;
 }

 public void stop(String RWFileDir) {
  String message = "";
  try {
   if (aftp != null) {
    aftp.closeServer();
    message = "与主机" + hostname + "连接已断开!";
    System.out.println(message);
    //log(RWFileDir, message);
   }
  } catch (IOException e) {
   message = "与主机" + hostname + "断开连接失败!" + e;
   System.out.println(message);
   log(RWFileDir, message);
  }
 }

 

 
 

 public void showFileContents(String strdir) {
  StringBuffer buf = new StringBuffer();
  try {
   aftp.cd(strdir);
   ins = aftp.list();
   while ((ch = ins.read()) >= 0) {
    buf.append((char) ch);
   }

   System.out.println("buf-------------" + buf.toString());
   ins.close();
  } catch (IOException e) {
  }
 }

 // 返回当前目录的所有文件及文件夹
 public ArrayList getFileList() throws IOException {

  BufferedReader dr = new BufferedReader(new InputStreamReader(aftp
    .list()));
  ArrayList a1 = new ArrayList(); // 存放文件
  // ArrayList a2= new ArrayList(); //存放文件夹
  ArrayList a3 = new ArrayList(); // 存放文件夹详细资料
  // ArrayList a4= new ArrayList(); //存放文件夹下其它文件夹下的文件
  ArrayList a5 = new ArrayList(); // 存放文件夹下其它文件夹下的文件
  String s = "";
  // Hashtable ha=new Hashtable();
  // ha=null;

  while ((s = dr.readLine()) != null) {

   if (isFile(s)) {
    // a4.clear();
    // a4.remove(0);
    // a4=null;
    ArrayList a4 = new ArrayList(); // 存放文件夹下其它文件夹下的文件
    a4.add(parseLine(s).get(8).toString());

    a4.add("文件");
    a4.add(parseLine(s).get(4).toString() + "bytes");
    a3.add(a4);

   }

   else if (isDir(s)) {
    // a2.add(parseLine(s).get(8));
    // showFileContents(s);
    // a3.add(parseLine(s));
    // System.out.println("ssssssss:"+s);
    // a4.add(getNameList(s));
    // a2.clear();
    // a2.remove(0);
    // a2=null;

    String tem = parseLine(s).get(8).toString();

    if ((tem.trim().equals(".")) || (tem.trim().equals(".."))) {
    } else {
     ArrayList a2 = new ArrayList(); // 存放文件夹
     a2.add(parseLine(s).get(8).toString());
     // a2.add("nbsp;");
     a2.add("文件夹");
     a2.add(parseLine(s).get(4).toString() + "bytes");
     a1.add(a2);

    }
   }

   //