FTP图片服务器实例

最近在做一个FTP图片服务器,以前没有接触过,在网上摸爬滚打了好几天过后终于出了一点小小成绩。

FtpServerImpl代码如下:


package org.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.service.FtpService;
import org.utils.ReadIniInfo;

/**
* <p>FTP客户端业务逻辑实现类</p>
* @author QingLong
* @version 1.0Beta
*/
public class FtpServiceImpl implements FtpService {

/**
* 登陆FTP服务器
*/
@Override
public void login(FTPClient ftpClient) throws Exception {

ReadIniInfo iniInfo = new ReadIniInfo();//读取配置文件对象

String ftpServer = iniInfo.getFtpServer();//获取FTP服务器地址

String ftpPort = iniInfo.getFtpPort();//获取FTP服务器端口

String ftpUser = iniInfo.getFtpUser();//获取FTP用户账号

String ftpPwd = iniInfo.getFtpPwd();//获取FTP用户密码

try {
// 链接到FTP服务器
ftpClient.connect(ftpServer, Integer.valueOf(ftpPort));

System.out.println("链接到FTP服务器:" + ftpServer + "成功..");

// 开始登陆服务器
boolean boo = ftpClient.login(ftpUser, ftpPwd);

if(boo){

System.out.println("登陆到FTP服务器:" + ftpServer + "成功..");

}else{

System.out.println("登陆到FTP服务器:" + ftpServer + "失败..");

logout(ftpClient);//退出/断开FTP服务器链接
}

} catch (Exception e) {

e.printStackTrace();

System.err.println("登陆到FTP服务器:" + ftpServer + "失败..");
}
}

/**
* 上传本地文件
* @param fileNewName 文件更改后的名称
* @throws Exception
*/
@Override
public void uploadFile(FTPClient ftpClient, String remotePath,
String fileNewName, String localFilePath) throws Exception{

FileInputStream is = new FileInputStream(new File(localFilePath));

try {

//设置被动模式
ftpClient.enterLocalPassiveMode();// 设置PassiveMode传输

// 设置以二进制流的方式传输
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

//设置字符集
ftpClient.setControlEncoding("GBK");

ftpClient.changeWorkingDirectory(remotePath);//改变FTP服务器目录

boolean boo = ftpClient.storeFile(fileNewName, is);

if(boo){

System.out.println("文件上传成功..");

}else{

System.out.println("文件上传失败..");
}

} catch (Exception e) {

e.printStackTrace();

}finally{

try {

if(null != is){

is.close();
}

logout(ftpClient);//退出/断开FTP服务器链接

} catch (IOException e) {

System.out.println("关闭链接失败..");

e.printStackTrace();
}
}
}

/**
* 远程文件列表
*/
@Override
public List<Map<String,String>> listFile(FTPClient ftpClient,String remotePath)
throws Exception {

//设置目录字符集
FTPFile[] remoteFiles = ftpClient.listFiles(new String(remotePath.getBytes("GBK"),"iso-8859-1"));

List<Map<String,String>> list = new ArrayList<Map<String,String>>();

if(remoteFiles != null && remoteFiles.length!= 0){

System.out.println("目录" + remotePath + "下的文件列表..");

}else{

System.out.println("目录" + remotePath + "下无文件内容..");
}
//递归目录文件
for(FTPFile ftpFile : remoteFiles){

Map<String,String> map = new HashMap<String,String>();

map.put("fileName",ftpFile.getName());//文件名称

map.put("fileSize",Long.toString(ftpFile.getSize()));//文件大小

//文件时间
map.put("fileTime",new SimpleDateFormat("yyyy/MM/dd kk:mm:ss").format(ftpFile.getTimestamp().getTime()));

list.add(map);
}

logout(ftpClient);//退出/断开FTP服务器链接

return list;
}

/**
* 下载远程文件
* @throws Exception
*/
@Override
public void downFile(FTPClient ftpClient, String remotePath,
String fileName, String localPath) throws Exception {

FTPFile[] fs;

FileOutputStream os = null;

try {

// 设置以二进制流的方式传输
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

ftpClient.setControlEncoding("GBK");

ftpClient.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录

fs = ftpClient.listFiles();//递归目标目录

for (FTPFile ff : fs) {

if (ff.getName().equals(fileName)) {//查找目标文件

File localFile = new File(localPath + File.separator+ ff.getName());

os = new FileOutputStream(localFile);

boolean blean = ftpClient.retrieveFile(ff.getName(), os);

if(blean){

System.out.println("文件下载成功..");

}else{

System.out.println("文件下载失败..");
}
}
}

} catch (IOException e) {

e.printStackTrace();

}finally{

try {

logout(ftpClient);//退出/断开FTP服务器链接

if(null != os){

os.close();
}

} catch (IOException e) {

System.out.println("关闭链接失败..");

e.printStackTrace();
}
}
}

/**
* 退出/断开FTP服务器链接
*/
@Override
public void logout(FTPClient ftpClient) throws Exception{

ftpClient.logout();//退出登陆

System.out.println("已退出FTP远程服务器..");

if(ftpClient.isConnected()){

ftpClient.disconnect();//断开链接

System.out.println("已断开FTP服务器链接..");
}
}
}


其他代码就不贴出来了,如有需要的直接下载就是了!(注:架包太大分批压缩的,可能会出错!)

apache ftp server所放位置为:E:\Server\apache-ftpserver-1.0.4

点击E:\Server\apache-ftpserver-1.0.4\bin\RunFtp.bat开启FTP服务器

远程存放文件地址为:D:\FtpTest\Upload\
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值