日期:2014-05-17  浏览次数:20816 次

我用jsp实现ftp上传功能,客户端不能上传文件
我用jsp实现ftp上传功能,但是客户端不能往服务器端传上传文件,服务器端自己可以上传文件,代码如下:
  private String ftpServer;
  private String ftpPort;
  private String ftpUserName;
  private String ftpPassword;
  private FTPClient ftpClient;
  private boolean isLogin = false;

  public void connServer(String pFtpServer, String pFtpPort,
  String pFtpUserName, String pFtpPassword) throws
  Exception {
  this.ftpServer = pFtpServer;
  if (pFtpPort.trim().equals("")) {
  this.ftpPort = "21";
  } else {
  this.ftpPort = pFtpPort;
  }
  if (pFtpUserName.trim().equals("")) {
  this.ftpUserName = "Anonymous";
  } else {
  this.ftpUserName = pFtpUserName;
  }
  this.ftpPassword = pFtpPassword;
  try {
  ftpClient = new FTPClient(); //ftpServer, Integer.parseInt(ftpPort)
  ftpClient.setRemoteHost(ftpServer);
  ftpClient.setRemotePort(Integer.parseInt(ftpPort));
  ftpClient.setControlEncoding("gb2312"); //加上这一句后在 edtftpj 2.0.1 下就要可以传中文文件名了
  System.out.println("开始登录");
  ftpClient.connect();
  ftpClient.login(ftpUserName, ftpPassword);
  System.out.println("登录成功");
  ftpClient.chdir("\\"); //在有的ftp服务器运行会出错,用ftpClient.chdir("/")又可以了
  System.out.println("已转入根目录");
  isLogin = true;
  } catch (Exception e) {
  throw new Exception(e.getMessage());
  }
  }

  //上传指定文件夹到ftp服务器上
  //@param folderName 本地要上传的文件夹全路径
  //@param ftpPath FTP上对于根目录的路径
  public String uploadFolder(String folderName, String ftpPath) throws
  Exception {
  if (isLogin) {
  String strMsg = "";
  try {
  File file = new File(folderName);

  if (file.isDirectory()) {
  ftpClient.chdir("\\");
  ftpClient.setType(FTPTransferType.BINARY);

  if (checkFolderIsExist(ftpPath)) {
  ftpClient.chdir(ftpPath);
  } else {
  createFolder(ftpPath);
  }

  if (!checkFolderIsExist(file.getName())) {
  ftpClient.mkdir(file.getName());
  }

  ftpClient.chdir(file.getName());

  ftpPath = ftpPath + "\\" + file.getName();
  File[] files = file.listFiles();

  for (int i = 0; i < files.length; i++) {
// System.out.println("==============================");
// System.out.println("ftpPath:"+ftpPath);
// System.out.println("files["+i+"]:"+files[i].getPath());
// System.out.println("==============================");

  if (files[i].isDirectory()) {
  uploadFolder(files[i].getPath(), ftpPath);
  } else {