ftp上传实例代码
程序员文章站
2022-04-14 10:25:11
...
/**
* FTP上传工具类
*
*/
public class FtpUtil {
static Logger logger = Logger.getLogger(FtpUtil.class.getName());
private String ip;
private String username;
private String password;
private int port = -1;
private String path;
private OutputStream os = null;
private FileInputStream is = null;
private FTPClient ftpClient;
public FTPClient getftpClient() { return ftpClient; }
public void setftpClient(FTPClient ftpClient) {
this.ftpClient = ftpClient;
}
public FtpUtil(){
}
public FtpUtil(String serverIP, String username, String password) {
this.ip = serverIP;
this.username = username;
this.password = password;
}
public FtpUtil(String serverIP, int port, String username, String password) {
this.ip = serverIP;
this.username = username;
this.password = password;
this.port = port;
this.ftpClient= new FTPClient();
}
/**
*
* @return 是否连接服务器
*/
public boolean ftpLogin() {
boolean isLogin = false;
FTPClientConfig ftpClientConfig = new FTPClientConfig();
ftpClientConfig.setServerTimeZoneId(TimeZone.getDefault().getID());
this.ftpClient.setControlEncoding("GBK");
this.ftpClient.configure(ftpClientConfig);
try {
if (this.port > 0) {
this.ftpClient.connect(this.ip, this.port);
}else {
this.ftpClient.connect(this.ip);
}
int reply = this.ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
this.ftpClient.disconnect();
logger.info("登录FTP服务失败!");
return isLogin;
}
if(this.ftpClient.login(this.username, this.password)){
this.ftpClient.enterLocalPassiveMode(); // 设置传输协议
this.ftpClient.setFileType(ftpClient.BINARY_FILE_TYPE);
logger.info(this.username + "成功登陆FTP服务器");
isLogin = true;
}else{
logger.info(this.username + "登录FTP服务失败!");
}
}catch (Exception e) {
e.printStackTrace();
logger.error( e.getMessage());
}
this.ftpClient.setBufferSize(1024 * 2);
this.ftpClient.setDataTimeout(30 * 1000);
return isLogin;
}
/**
* @退出关闭服务器链接
*/
public void ftpLogOut() {
if (null != this.ftpClient && this.ftpClient.isConnected()) {
try {
boolean reuslt = this.ftpClient.logout();// 退出FTP服务器
if (reuslt) {
logger.info("成功退出服务器");
}
}catch (IOException e) {
e.printStackTrace();
logger.warn("退出FTP服务器异常!" + e.getMessage());
}finally {
try {
this.ftpClient.disconnect();
}catch (IOException e) {
e.printStackTrace();
logger.error("关闭FTP服务器的连接异常!");
}
}
}
}
/**
* 上传Ftp文件
* @param localFile 当地文件
* @param romotUpLoadePath 上传服务器路径
*/
public boolean uploadFile(File localFile, String romotUpLoadePath) {
BufferedInputStream inStream = null;
boolean success = false;
try {
this.ftpClient.changeWorkingDirectory(romotUpLoadePath);
inStream = new BufferedInputStream(new FileInputStream(localFile));
success = this.ftpClient.storeFile(localFile.getName(), inStream);
if (success == true) {
System.out.println(localFile.getName() + "上传成功");///
return success;
}
}catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally {
if (inStream != null) {
try {
inStream.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
return success;
}
/**上传文件夹
* @param localDirectory 本地文件夹
* @param remoteDirectoryPath 远程路径
*/
public boolean uploadDirectory(String localDirectory,String remoteDirectoryPath) {
File src = new File(localDirectory);
System.out.println(src.getName());
try {
remoteDirectoryPath = remoteDirectoryPath + src.getName() + "/";
boolean makeDirFlag = ftpClient.makeDirectory(remoteDirectoryPath);
System.out.println("localDirectory : " + localDirectory);
System.out.println("remoteDirectoryPath : " + remoteDirectoryPath);
System.out.println("src.getName() : " + src.getName());
System.out.println("remoteDirectoryPath : " + remoteDirectoryPath);
System.out.println("makeDirFlag : " + makeDirFlag);
// ftpClient.listDirectories();
}catch (IOException e) {
e.printStackTrace();
}
File[] allFile = src.listFiles();
if(allFile.length==0||allFile.length>0){
for (int currentFile = 0;currentFile < allFile.length;currentFile++) {
if (!allFile[currentFile].isDirectory()) {
String srcName = allFile[currentFile].getPath().toString();
uploadFile(new File(srcName), remoteDirectoryPath);
}
}
for (int currentFile = 0;currentFile < allFile.length;currentFile++) {
if (allFile[currentFile].isDirectory()) {
uploadDirectory(allFile[currentFile].getPath().toString(), //递归
remoteDirectoryPath);
}
}
}
/*for (int currentFile = 0;currentFile < allFile.length;currentFile++) {
if (!allFile[currentFile].isDirectory()) {
String srcName = allFile[currentFile].getPath().toString();
uploadFile(new File(srcName), remoteDirectoryPath);
}
}
for (int currentFile = 0;currentFile < allFile.length;currentFile++) {
if (allFile[currentFile].isDirectory()) {
uploadDirectory(allFile[currentFile].getPath().toString(), //递归
remoteDirectoryPath);
}
} */
return true;
}
* FTP上传工具类
*
*/
public class FtpUtil {
static Logger logger = Logger.getLogger(FtpUtil.class.getName());
private String ip;
private String username;
private String password;
private int port = -1;
private String path;
private OutputStream os = null;
private FileInputStream is = null;
private FTPClient ftpClient;
public FTPClient getftpClient() { return ftpClient; }
public void setftpClient(FTPClient ftpClient) {
this.ftpClient = ftpClient;
}
public FtpUtil(){
}
public FtpUtil(String serverIP, String username, String password) {
this.ip = serverIP;
this.username = username;
this.password = password;
}
public FtpUtil(String serverIP, int port, String username, String password) {
this.ip = serverIP;
this.username = username;
this.password = password;
this.port = port;
this.ftpClient= new FTPClient();
}
/**
*
* @return 是否连接服务器
*/
public boolean ftpLogin() {
boolean isLogin = false;
FTPClientConfig ftpClientConfig = new FTPClientConfig();
ftpClientConfig.setServerTimeZoneId(TimeZone.getDefault().getID());
this.ftpClient.setControlEncoding("GBK");
this.ftpClient.configure(ftpClientConfig);
try {
if (this.port > 0) {
this.ftpClient.connect(this.ip, this.port);
}else {
this.ftpClient.connect(this.ip);
}
int reply = this.ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
this.ftpClient.disconnect();
logger.info("登录FTP服务失败!");
return isLogin;
}
if(this.ftpClient.login(this.username, this.password)){
this.ftpClient.enterLocalPassiveMode(); // 设置传输协议
this.ftpClient.setFileType(ftpClient.BINARY_FILE_TYPE);
logger.info(this.username + "成功登陆FTP服务器");
isLogin = true;
}else{
logger.info(this.username + "登录FTP服务失败!");
}
}catch (Exception e) {
e.printStackTrace();
logger.error( e.getMessage());
}
this.ftpClient.setBufferSize(1024 * 2);
this.ftpClient.setDataTimeout(30 * 1000);
return isLogin;
}
/**
* @退出关闭服务器链接
*/
public void ftpLogOut() {
if (null != this.ftpClient && this.ftpClient.isConnected()) {
try {
boolean reuslt = this.ftpClient.logout();// 退出FTP服务器
if (reuslt) {
logger.info("成功退出服务器");
}
}catch (IOException e) {
e.printStackTrace();
logger.warn("退出FTP服务器异常!" + e.getMessage());
}finally {
try {
this.ftpClient.disconnect();
}catch (IOException e) {
e.printStackTrace();
logger.error("关闭FTP服务器的连接异常!");
}
}
}
}
/**
* 上传Ftp文件
* @param localFile 当地文件
* @param romotUpLoadePath 上传服务器路径
*/
public boolean uploadFile(File localFile, String romotUpLoadePath) {
BufferedInputStream inStream = null;
boolean success = false;
try {
this.ftpClient.changeWorkingDirectory(romotUpLoadePath);
inStream = new BufferedInputStream(new FileInputStream(localFile));
success = this.ftpClient.storeFile(localFile.getName(), inStream);
if (success == true) {
System.out.println(localFile.getName() + "上传成功");///
return success;
}
}catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally {
if (inStream != null) {
try {
inStream.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
return success;
}
/**上传文件夹
* @param localDirectory 本地文件夹
* @param remoteDirectoryPath 远程路径
*/
public boolean uploadDirectory(String localDirectory,String remoteDirectoryPath) {
File src = new File(localDirectory);
System.out.println(src.getName());
try {
remoteDirectoryPath = remoteDirectoryPath + src.getName() + "/";
boolean makeDirFlag = ftpClient.makeDirectory(remoteDirectoryPath);
System.out.println("localDirectory : " + localDirectory);
System.out.println("remoteDirectoryPath : " + remoteDirectoryPath);
System.out.println("src.getName() : " + src.getName());
System.out.println("remoteDirectoryPath : " + remoteDirectoryPath);
System.out.println("makeDirFlag : " + makeDirFlag);
// ftpClient.listDirectories();
}catch (IOException e) {
e.printStackTrace();
}
File[] allFile = src.listFiles();
if(allFile.length==0||allFile.length>0){
for (int currentFile = 0;currentFile < allFile.length;currentFile++) {
if (!allFile[currentFile].isDirectory()) {
String srcName = allFile[currentFile].getPath().toString();
uploadFile(new File(srcName), remoteDirectoryPath);
}
}
for (int currentFile = 0;currentFile < allFile.length;currentFile++) {
if (allFile[currentFile].isDirectory()) {
uploadDirectory(allFile[currentFile].getPath().toString(), //递归
remoteDirectoryPath);
}
}
}
/*for (int currentFile = 0;currentFile < allFile.length;currentFile++) {
if (!allFile[currentFile].isDirectory()) {
String srcName = allFile[currentFile].getPath().toString();
uploadFile(new File(srcName), remoteDirectoryPath);
}
}
for (int currentFile = 0;currentFile < allFile.length;currentFile++) {
if (allFile[currentFile].isDirectory()) {
uploadDirectory(allFile[currentFile].getPath().toString(), //递归
remoteDirectoryPath);
}
} */
return true;
}
以上就是ftp上传实例代码的详细内容,更多请关注其它相关文章!
上一篇: PHP注册码/序列号生成实例程序
下一篇: 总结coffeescript使用的方式