代码改变世界

ftp上传下载

2017-11-20 17:20  青涊  阅读(353)  评论(0编辑  收藏  举报

package cn.talent.com.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.SocketException;
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.apache.commons.net.ftp.FTPReply;

public class FtpUpfile {
public static void main(String[] args) throws Exception {
File file = new File("f:/aa");
// downFile("openaccount1", fileName, localPath);
upload(file);
System.out.println(1);
}

/**
*
* @param mapfilePath key是下载的文件名,value是下载到本地的路径
* @return
*/
public static boolean downFile(Map<String, String> mapfilePath) {
boolean success = false;
ftp = new FTPClient();
try {
int reply;
ftp.connect("192.168.8.57", 21);
// 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
ftp.login("Administrator", "123456");// 登录
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
System.out.println("连接失败");
ftp.disconnect();
return success;
}
System.out.println("连接成功");
ftp.setControlEncoding("gbk");
ftp.changeWorkingDirectory("check");// 转移到FTP服务器目录
OutputStream outputStream = null;
for (Map.Entry<String, String> string : mapfilePath.entrySet()) {
if (isDir(ftp, string.getKey())) {
ftp.changeWorkingDirectory("check" + string.getKey());
FTPFile[] fs = ftp.listFiles();
for (FTPFile ftpFile : fs) {
System.out.println(ftpFile.getName());
outputStream = new FileOutputStream(string.getValue() + "/" + ftpFile.getName());
ftp.retrieveFile(ftpFile.getName(), outputStream);
}
} else {
FTPFile[] fs = ftp.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(string.getKey())) {
outputStream = new FileOutputStream(string.getValue());
ftp.retrieveFile(ff.getName(), outputStream);
}
if (string.getKey().equals("/")) {
outputStream = new FileOutputStream(string.getValue());
ftp.retrieveFile(ff.getName(), outputStream);
}
}
}
}
ftp.logout();
success = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return success;
}

private static FTPClient ftp;

public static boolean upload(File file) {
ftp = new FTPClient();
try {
int reply;
ftp.connect("192.168.8.57", 21);
// 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
ftp.login("Administrator", "123456");// 登录
ftp.setFileType(FTP.BINARY_FILE_TYPE);
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
}
ftp.changeWorkingDirectory("check");
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);
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());
FileInputStream input = new FileInputStream(file2);
ftp.storeFile(file2.getName(), input);
input.close();
}
} catch (SocketException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}

// 判断是否是目录
public static boolean isDir(FTPClient ftpClient, String fileName) {
try {
// 切换目录,若当前是目录则返回true,否则返回true。
boolean falg = ftpClient.changeWorkingDirectory(fileName);
return falg;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}