public class FtpTool {
public static void main(String[] args) throws FileNotFoundException {
boolean flag = false;
//FTP推送的方法 ,向FTP文件系统推送文件 (问题:不支持创建多级目录,只把文件推到了FTP主目录)
// InputStream input = new FileInputStream(new File(tempFilePath));
// //ftp服务器的参数应该从公共参数配置表中取得
// pushFlag = FtpTool.uploadFile("127.0.0.1", "5314800263", "jXpemT", "/space/ftpdata/S2015031215314800263", fileName, input);
InputStream input = new FileInputStream(new File("C:\\1.txt"));
flag = uploadFile("127.0.0.1", "5314800263", "jXpemT", "/space/ftpdata/S2015031215314800263", "123.jpg", input);
System.out.println("ftp文件上传成功"+flag);
}
/**
* Description: 向FTP服务器上传文件
*
* @param url
* FTP服务器hostname
* @param username
* FTP登录账号
* @param password
* FTP登录密码
* @param path
* FTP服务器保存目录
* @param filename
* 上传到FTP服务器上的文件名
* @param input
* 输入流
* @return 成功返回true,否则返回false
*/
public static boolean uploadFile(String url, String username, String password,
String path, String filename, InputStream input) {
//设置FTP服务器的配置信息
FTPClientConfig ftpConfig = new FTPClientConfig(FTPClientConfig.SYST_UNIX);
ftpConfig.setServerLanguageCode(FTP.DEFAULT_CONTROL_ENCODING);
boolean success = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(url);
ftp.login(username, password);
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return success;
}
// 转到指定上传目录
ftp.changeWorkingDirectory(path);
ftp.setFileType(FTP.BINARY_FILE_TYPE); // 设置为2进制上传
// 将上传文件存储到指定目录
ftp.storeFile(filename, input);
ftp.logout();
input.close();
success = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return success;
}
/**
* Description: 从FTP服务器下载文件
*
* @param url
* FTP服务器hostname
* @param username
* FTP登录账号
* @param password
* FTP登录密码
* @param remotePath
* FTP服务器上的相对路径
* @param fileName
* 下载时的默认文件名
* @param localPath
* 下载后保存到本地的路径
* @return
*/
public boolean downFile(String url, String username, String password,
String remotePath, String fileName, HttpServletResponse response) {
// 初始表示下载失败
boolean success = false;
// 创建FTPClient对象
FTPClient ftp = new FTPClient();
try {
int reply;
// 连接FTP服务器
// 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
ftp.connect(url);
// 登录ftp
ftp.login(username, password);
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return success;
}
String realName = remotePath
.substring(remotePath.lastIndexOf("/") + 1);
// 转到指定下载目录
//ftp.changeWorkingDirectory(SysConstants.FTP_PATH);
ftp.setFileType(FTP.BINARY_FILE_TYPE);
// 列出该目录下所有文件
// 设置文件下载头部
response.setContentType("application/x-msdownload");// 设置编码
response.setHeader("Content-Disposition", "attachement;filename="
+ new String(fileName.getBytes(), "ISO-8859-1"));
FTPFile[] fs = ftp.listFiles();
// 遍历所有文件,找到指定的文件
for (FTPFile ff : fs) {
if (ff.getName().equals(realName)) {
OutputStream out = response.getOutputStream();
InputStream bis = ftp.retrieveFileStream(realName);
// 根据绝对路径初始化文件
// 输出流
int len = 0;
byte[] buf = new byte[1024];
while ((len = bis.read(buf)) > 0) {
out.write(buf, 0, len);
out.flush();
}
out.close();
bis.close();
}
}
ftp.logout();
// 下载成功
success = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return success;
}
}
转载于:https://my.oschina.net/bigdataer/blog/424759