需求:在Linux环境上安装FTP服务端Serv-U
解决方案:
1.下载Serv-U的linux 64bit的安装文件(如果要安装ftp的机器外网不通,则需要本地下载后上传到该机器)
wget http://www.rhinosoft.com.cn/download/14.0.1.0/SU-MFTS-Linux-64bit.zip
2.解压安装文件
unzip SU-MFTS-Linux-64bit.zip
3.对安装文件赋予最高权限(r=4,w=2,x=1) ,键入y继续执行
chmod 777 Serv-U-Linux-x86_64-Install
4.继续默认安装并启动服务,默认安装在/usr/local/Serv-U目录下,安装完成,默认管理界面服务的端口是8080
5.现在可以在浏览器中输入:http://ip:8080/ ,如果访问不了,原因可能是:
1)8080端口已被占用,可在Serv-U-StartupLog.txt中查看日志确认
2)防火墙没有关闭,需要手动关闭:service iptables stop
6.使用管理员默认帐号/密码:admin/admin登录,即可新增域、添加账户、配置数据目录等
7.这时候就可以测试了,使用客户端FlashFXP上传文件。浏览器可浏览ftp://ip查看下载(IE浏览器如果打开不了,需要在工具->Internet选项->高级中去除勾选下图中的选项)
8.java代码测试FTP服务的上传下载功能
-
package com.besttone.zookeepergroup;
-
-
import java.io.File;
-
import java.io.FileInputStream;
-
import java.io.FileOutputStream;
-
import java.io.InputStream;
-
import java.io.OutputStream;
-
-
import org.apache.commons.net.ftp.FTPClient;
-
import org.apache.commons.net.ftp.FTPFile;
-
import org.apache.commons.net.ftp.FTPReply;
-
-
/**
-
* @author zhenzhen
-
* @title ItemFtp
-
* @Description : FTP 上传下载工具类
-
*/
-
public class ItemFtp {
-
-
private FTPClient ftp;
-
-
/**
-
*
-
* @param path
-
* 上传到ftp服务器哪个路径下
-
* @param addr
-
* 地址
-
* @param port
-
* 端口号
-
* @param username
-
* 用户名
-
* @param password
-
* 密码
-
* @return
-
* @throws Exception
-
*/
-
private boolean connect(String path, String addr, int port,
-
String username, String password) throws Exception {
-
boolean result = false;
-
ftp = new FTPClient();
-
int reply;
-
ftp.connect(addr, port);
-
ftp.login(username, password);
-
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
-
reply = ftp.getReplyCode();
-
if (!FTPReply.isPositiveCompletion(reply)) {
-
ftp.disconnect();
-
return result;
-
}
-
ftp.changeWorkingDirectory(path);
-
result = true;
-
return result;
-
}
-
-
/**
-
* @author
-
* @class ItemFtp
-
* @title upload
-
* @Description :
-
* @time 2013 2013-11-27
-
* @return void
-
* @exception :(Error note)
-
* @param file
-
* 上传的文件或文件夹
-
* @param path
-
* 上传的文件的路径
-
* @throws Exception
-
*/
-
private void upload(File file, String path) throws Exception {
-
-
System.out.println(" file.isDirectory() : " + file.isDirectory());
-
-
if (file.isDirectory()) {
-
ftp.makeDirectory(file.getName());
-
ftp.changeWorkingDirectory(file.getName());
-
String[] files = file.list();
-
for (int i = 0; i < files.length; i++) {
-
File file1 = new File(file.getPath() + "\\" + files[i]);
-
if (file1.isDirectory()) {
-
upload(file1, path);
-
ftp.changeToParentDirectory();
-
} else {
-
File file2 = new File(file.getPath() + "\\" + files[i]);
-
FileInputStream input = new FileInputStream(file2);
-
ftp.storeFile(file2.getName(), input);
-
input.close();
-
}
-
}
-
} else {
-
File file2 = new File(file.getPath());
-
-
System.out.println(" file.getPath() : " + file.getPath()
-
+ " | file2.getName() : " + file2.getName());
-
-
InputStream input = new FileInputStream(file2);
-
-
ftp.changeWorkingDirectory(path);
-
ftp.storeFile(file2.getName(), input);
-
-
input.close(); // 关闭输入流
-
ftp.logout(); // 退出连接
-
}
-
}
-
-
/**
-
* @author
-
* @class ItemFtp
-
* @title download
-
* @Description : FPT 下载文件方法
-
* @time 2013 2013-11-27
-
* @return void
-
* @exception :(Error note)
-
* @param reomvepath
-
* 下载的文件的路径
-
* @param fileName
-
* 下载的文件名
-
* @param localPath
-
* 下载的文件本地路径
-
* @throws Exception
-
*/
-
@SuppressWarnings("unused")
-
private void download(String reomvepath, String fileName, String localPath)
-
throws Exception {
-
-
ftp.changeWorkingDirectory(reomvepath);
-
-
// 列出该目录下所有文件
-
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);
-
System.out.println("下载成功!");
-
is.close();
-
}
-
}
-
-
ftp.logout(); // 退出连接
-
-
}
-
-
public static void main(String[] args) throws Exception {
-
-
ItemFtp t = new ItemFtp();
-
-
boolean lianjie = t.connect("/zhengzhenzhen", "180.153.*.*", 21,
-
"*", "*");
-
System.out.println("连接 :" + lianjie);
-
-
// 上传
-
File file = new File("d:\\test\\test.txt");
-
t.upload(file, "/zhengzhenzhen/");
-
-
// 下载
-
// t.download("/zhengzhenzhen", "22.png", "D:\\test");
-
System.out.println("test");
-
}
-
}
完毕!