import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
public class FtpUtils {
private static FTPClient ftp;
private static String path;
private static String addr;
private static int port;
private static String username;
private static String password;
static{
path = "";
addr = "192.200.5.26";//服务器地址
port = 21;//端口默认21
username = "root";//用户名
password = "root123";//密码
}
/**
*
* @param path 上传到ftp服务器哪个路径下
* @param addr 地址
* @param port 端口号
* @param username 用户名
* @param password 密码
* @return
* @throws Exception
* @author lxy 20170518
*/
private static boolean connectFtpInfo(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;
}
/**
*
* @param file 文件名称
* @param newImageName:新的图片的名称
* @param directoryName 上传到目录文件名称
* @author lxy 20160928
* @throws Exception
*/
public static boolean uploadFileToFtpService(InputStream input,String newImageName,String directoryName,String firstdirect) throws Exception{
path=firstdirect;
connectFtpInfo(path, addr, port, username, password);
//文件上传
// FileInputStream input = new FileInputStream(file);
setFtpBaseInfo(directoryName);
ftp.enterLocalPassiveMode();
boolean isok = ftp.storeFile(newImageName, input);
input.close();
ftp.logout();
return isok;
}
public static void main(String[] args) throws Exception {
//http://192.168.3.201/presentation/CT8256220161124 //829472016120916122
// System.out.println(isImageExistsFtp("presentation/CT8256220161124","8294720161209161220.doc"));
File file = new File("D:/home/20170523/NR_CW_1.txt");
FileInputStream inp = new FileInputStream(file);
uploadFileToFtpService(inp,"sss.txt","","/zgh");
}
/**
* ftp服务器参数设置
* @param directoryName
* @author lxy 20160929
* @throws Exception
*/
private static void setFtpBaseInfo(String directoryName) throws Exception{
ftp.enterLocalPassiveMode();
int defaultTimeoutSecond=30*60 * 1000;
//设置超时时间
ftp.setDefaultTimeout(defaultTimeoutSecond);
// ftp.setConnectTimeout(defaultTimeoutSecond );
ftp.setDataTimeout(defaultTimeoutSecond);
if(directoryName!=null && !"".equals(directoryName)){
// directoryName = directoryName.substring(directoryName.lastIndexOf("\\")+1);
ftp.changeWorkingDirectory(directoryName);
}
}
/**
*
* @param remoteminfo
* @param imageName
* @return 如果图片存在则返回true,不存在则返回false
* @author lxy 20160929
* @throws Exception
*/
public static boolean isImageExistsFtp(String remoteminfo,String imageName) throws Exception{
connectFtpInfo(path, addr, port, username, password);
setFtpBaseInfo(remoteminfo);
FTPFile[] tempImageFtpImageList = ftp.listFiles();
boolean isok = false;
if(tempImageFtpImageList!=null && tempImageFtpImageList.length>0){
for (int i = 0; i < tempImageFtpImageList.length; i++) {
FTPFile fTPFileTemp = tempImageFtpImageList[i];
String imagesuffix = fTPFileTemp.getName();
if(imagesuffix!=null && imagesuffix.equals(imageName)){
isok = true;
break;
}
}
}
return isok;
}
/**
*
* @param remoteminfo 远程目录地址信息
* @param imageNameList 需要验证是否存在的图片集合
* @return 返回存在的图片集合
* @author lxy 20160929
* @throws Exception
*/
public static List<String> isImageExistsFtpList(String remoteminfo,List<String> imageNameList) throws Exception{
connectFtpInfo(path, addr, port, username, password);
setFtpBaseInfo(remoteminfo);
FTPFile[] tempImageFtpImageList = ftp.listFiles();
boolean isok = false;
List<String> returnImageInfo = new ArrayList<String>();
if(tempImageFtpImageList!=null && tempImageFtpImageList.length>0){
for (int j = 0; j < imageNameList.size(); j++) {
String oneImageInfo = imageNameList.get(j);
for (int i = 0; i < tempImageFtpImageList.length; i++) {
FTPFile fTPFileTemp = tempImageFtpImageList[i];
String imagesuffix = fTPFileTemp.getName();
if(imagesuffix!=null && imagesuffix.equals(oneImageInfo)){
returnImageInfo.add(imagesuffix);
break;
}
}
}
}
return returnImageInfo;
}
/**
*
* @param remoteminfo 远程服务器地址信息
* @param imageNameList 存放图片名称的集合信息
* @return 返回包每个图片流信息
* @author lxy 20160929
* @throws Exception
*/
public static List<InputStream> ftpDownLoadInfo(String remoteminfo,List<String> imageNameList) throws Exception{
connectFtpInfo(path, addr, port, username, password);
setFtpBaseInfo(remoteminfo);
List<InputStream> inputStreamList = new ArrayList<InputStream>();
for (int j = 0; j < imageNameList.size(); j++) {
String infoiamge = imageNameList.get(j);
inputStreamList.add(ftp.retrieveFileStream(infoiamge));
}
return inputStreamList;
}
/**
*
* @param remoteminfo远程服务器地址信息
* @param imageName 图片名称
* @return 单个图片流信息
* @throws Exception
* @author lxy 20160929
*/
public static InputStream ftpDownLoadInfo(String remoteminfo,String imageName,String changemdir) throws Exception{
path=changemdir;
connectFtpInfo(path, addr, port, username, password);
setFtpBaseInfo(remoteminfo);
return ftp.retrieveFileStream(imageName);
}
/**
* 创建文件夹
* lxy
* @param args
*/
public static boolean mkdir(String dirName,String directoryName) throws Exception{
connectFtpInfo(path, addr, port, username, password);
setFtpBaseInfo(directoryName);
return ftp.makeDirectory(dirName);
}
}
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
public class FtpUtils {
private static FTPClient ftp;
private static String path;
private static String addr;
private static int port;
private static String username;
private static String password;
static{
path = "";
addr = "192.200.5.26";//服务器地址
port = 21;//端口默认21
username = "root";//用户名
password = "root123";//密码
}
/**
*
* @param path 上传到ftp服务器哪个路径下
* @param addr 地址
* @param port 端口号
* @param username 用户名
* @param password 密码
* @return
* @throws Exception
* @author lxy 20170518
*/
private static boolean connectFtpInfo(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;
}
/**
*
* @param file 文件名称
* @param newImageName:新的图片的名称
* @param directoryName 上传到目录文件名称
* @author lxy 20160928
* @throws Exception
*/
public static boolean uploadFileToFtpService(InputStream input,String newImageName,String directoryName,String firstdirect) throws Exception{
path=firstdirect;
connectFtpInfo(path, addr, port, username, password);
//文件上传
// FileInputStream input = new FileInputStream(file);
setFtpBaseInfo(directoryName);
ftp.enterLocalPassiveMode();
boolean isok = ftp.storeFile(newImageName, input);
input.close();
ftp.logout();
return isok;
}
public static void main(String[] args) throws Exception {
//http://192.168.3.201/presentation/CT8256220161124 //829472016120916122
// System.out.println(isImageExistsFtp("presentation/CT8256220161124","8294720161209161220.doc"));
File file = new File("D:/home/20170523/NR_CW_1.txt");
FileInputStream inp = new FileInputStream(file);
uploadFileToFtpService(inp,"sss.txt","","/zgh");
}
/**
* ftp服务器参数设置
* @param directoryName
* @author lxy 20160929
* @throws Exception
*/
private static void setFtpBaseInfo(String directoryName) throws Exception{
ftp.enterLocalPassiveMode();
int defaultTimeoutSecond=30*60 * 1000;
//设置超时时间
ftp.setDefaultTimeout(defaultTimeoutSecond);
// ftp.setConnectTimeout(defaultTimeoutSecond );
ftp.setDataTimeout(defaultTimeoutSecond);
if(directoryName!=null && !"".equals(directoryName)){
// directoryName = directoryName.substring(directoryName.lastIndexOf("\\")+1);
ftp.changeWorkingDirectory(directoryName);
}
}
/**
*
* @param remoteminfo
* @param imageName
* @return 如果图片存在则返回true,不存在则返回false
* @author lxy 20160929
* @throws Exception
*/
public static boolean isImageExistsFtp(String remoteminfo,String imageName) throws Exception{
connectFtpInfo(path, addr, port, username, password);
setFtpBaseInfo(remoteminfo);
FTPFile[] tempImageFtpImageList = ftp.listFiles();
boolean isok = false;
if(tempImageFtpImageList!=null && tempImageFtpImageList.length>0){
for (int i = 0; i < tempImageFtpImageList.length; i++) {
FTPFile fTPFileTemp = tempImageFtpImageList[i];
String imagesuffix = fTPFileTemp.getName();
if(imagesuffix!=null && imagesuffix.equals(imageName)){
isok = true;
break;
}
}
}
return isok;
}
/**
*
* @param remoteminfo 远程目录地址信息
* @param imageNameList 需要验证是否存在的图片集合
* @return 返回存在的图片集合
* @author lxy 20160929
* @throws Exception
*/
public static List<String> isImageExistsFtpList(String remoteminfo,List<String> imageNameList) throws Exception{
connectFtpInfo(path, addr, port, username, password);
setFtpBaseInfo(remoteminfo);
FTPFile[] tempImageFtpImageList = ftp.listFiles();
boolean isok = false;
List<String> returnImageInfo = new ArrayList<String>();
if(tempImageFtpImageList!=null && tempImageFtpImageList.length>0){
for (int j = 0; j < imageNameList.size(); j++) {
String oneImageInfo = imageNameList.get(j);
for (int i = 0; i < tempImageFtpImageList.length; i++) {
FTPFile fTPFileTemp = tempImageFtpImageList[i];
String imagesuffix = fTPFileTemp.getName();
if(imagesuffix!=null && imagesuffix.equals(oneImageInfo)){
returnImageInfo.add(imagesuffix);
break;
}
}
}
}
return returnImageInfo;
}
/**
*
* @param remoteminfo 远程服务器地址信息
* @param imageNameList 存放图片名称的集合信息
* @return 返回包每个图片流信息
* @author lxy 20160929
* @throws Exception
*/
public static List<InputStream> ftpDownLoadInfo(String remoteminfo,List<String> imageNameList) throws Exception{
connectFtpInfo(path, addr, port, username, password);
setFtpBaseInfo(remoteminfo);
List<InputStream> inputStreamList = new ArrayList<InputStream>();
for (int j = 0; j < imageNameList.size(); j++) {
String infoiamge = imageNameList.get(j);
inputStreamList.add(ftp.retrieveFileStream(infoiamge));
}
return inputStreamList;
}
/**
*
* @param remoteminfo远程服务器地址信息
* @param imageName 图片名称
* @return 单个图片流信息
* @throws Exception
* @author lxy 20160929
*/
public static InputStream ftpDownLoadInfo(String remoteminfo,String imageName,String changemdir) throws Exception{
path=changemdir;
connectFtpInfo(path, addr, port, username, password);
setFtpBaseInfo(remoteminfo);
return ftp.retrieveFileStream(imageName);
}
/**
* 创建文件夹
* lxy
* @param args
*/
public static boolean mkdir(String dirName,String directoryName) throws Exception{
connectFtpInfo(path, addr, port, username, password);
setFtpBaseInfo(directoryName);
return ftp.makeDirectory(dirName);
}
}