FTP上传
来源:互联网 发布:midi 入门 软件 编辑:程序博客网 时间:2024/06/16 18:16
private static ExecutorService fixedThreadPool = Executors.newFixedThreadPool(50);
private static String ip = "";
private static String port = "";
private static String username = "";
private static String password = "";
private static FTPClient ftpClient = null;
private static String LclDir = "C:\\Users\\dell\\Desktop\\";
// private String merId;
// private String LclFil;
// private String ObjFil;
// private String Floder;
//LclFil 本地文件名+路径
//ObjFil 目标文件名+路径
public static int uploadFile1(String LclFil,String ObjFil) {
try{
ftpClient = new FTPClient();
System.out.println("开始");
ftpClient.connect(ip, Integer.parseInt(port));
// 连接后检测返回码来校验连接是否成功
int reply = ftpClient.getReplyCode();
if (FTPReply.isPositiveCompletion(reply)) {
// 登陆到ftp服务器
if (ftpClient.login(username, password)) {
System.out.println("登陆成功:" + FTP.BINARY_FILE_TYPE);
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
}
} else {
System.out.println("登录失败");
return -2;
}
// 设置为passive模式
ftpClient.enterLocalPassiveMode();
ftpClient.setSoTimeout(600000);
//获取文件日期
// String filedate =LclFil;
// String date = filedate.substring(0, 8);
// String YYYYMM = date.substring(0, 6) ;
// String DD = date.substring(6,8);
//
if(!existFile(ObjFil)){
System.out.println(makeDirectory(ObjFil.replace("/",""))+"");
}
// System.out.println(changeWorkingDirectory(YYYYMM)+"");
// if(!existFile(DD)){
// System.out.println(makeDirectory(DD.replace("/",""))+"");
// }
// System.out.println(changeWorkingDirectory(DD)+"");
if(!uploadFile(LclFil,ObjFil)){
System.out.println("文件传输失败");
return -3;
}
return 0;
}catch (Exception e){
System.out.println(e.getMessage());
return -1;
}finally{
try {
if(ftpClient!=null){
ftpClient.logout();
if(ftpClient.isConnected()){
ftpClient.disconnect();// 断开连接
}
}
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
/**
* 进入到服务器的某个目录下
*
* @param directory
*/
public static boolean changeWorkingDirectory(String directory) {
boolean flag = true;
try {
System.out.println(directory+"目录");
flag = ftpClient.changeWorkingDirectory(directory);
if (flag) {
System.out.println("进入文件夹"+ directory + " 成功!");
}else {
System.out.println("进入文件夹"+ directory + " 失败!");
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
return flag;
}
// 检查路径是否存在,存在返回true,否则false
public static boolean existFile(String path) throws IOException {
boolean flag = false;
FTPFile[] ftpFileArr = ftpClient.listFiles(path);
/* for (FTPFile ftpFile : ftpFileArr) {
if (ftpFile.isDirectory()
&& ftpFile.getName().equalsIgnoreCase(path)) {
flag = true;
break;
}
} */
if(ftpFileArr.length > 0){
flag = true;
}
return flag;
}
/**
* 在服务器上创建一个文件夹
*
* @param dir 文件夹名称,不能含有特殊字符,如 \ 、/ 、: 、* 、?、 "、 <、>...
*/
public static boolean makeDirectory(String dir) {
boolean flag = true;
try {
flag = ftpClient.makeDirectory(dir);
if (flag) {
System.out.println("创建文件夹"+ dir + " 成功!");
} else {
System.out.println("创建文件夹"+ dir + " 失败!");
}
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
/**
* 上传单个文件,并重命名
*
* @param localFile--本地文件路径
* @param localRootFile--本地文件父文件夹路径
* @param distFolder--新的文件名,可以命名为空""
* @return true 上传成功,false 上传失败
* @throws IOException
*/
public static boolean uploadFile(String local, String remote) throws IOException {
boolean flag = true;
String remoteFileName = remote;
if (remote.contains("/")) {
remoteFileName = remote.substring(remote.lastIndexOf("/") + 1);
System.out.println(remote+"原因");
// 创建服务器远程目录结构,创建失败直接返回
if (!CreateDirecroty(remote)) {
return false;
}
}
FTPFile[] files = ftpClient.listFiles(new String(remoteFileName));
File f = new File(local);
if(!uploadFile(remoteFileName, f)){
flag = false;
}
return flag;
}
public static boolean CreateDirecroty(String remote) throws IOException {
boolean success = true;
String directory = remote.substring(0, remote.lastIndexOf("/") + 1);
// 如果远程目录不存在,则递归创建远程服务器目录
if (!directory.equalsIgnoreCase("/")&& !changeWorkingDirectory(new String(directory))) {
int start = 0;
int end = 0;
if (directory.startsWith("/")) {
start = 1;
} else {
start = 0;
}
end = directory.indexOf("/", start);
while (true) {
String subDirectory = new String(remote.substring(start, end).getBytes("UTF-8"),"UTF-8");
System.out.println(subDirectory+"是啥");
if (changeWorkingDirectory(subDirectory)) {
if (makeDirectory(subDirectory)) {
changeWorkingDirectory(subDirectory);
} else {
System.out.println("创建目录["+subDirectory+"]失败");
System.out.println("创建目录["+subDirectory+"]失败");
success = false;
return success;
}
}
start = end + 1;
end = directory.indexOf("/", start);
// 检查所有目录是否创建完毕
if (end <= start) {
break;
}
}
}
return success;
}
/**
* 上传文件
*
* @param remoteFile
* 远程文件路径,支持多级目录嵌套
* @param localFile
* 本地文件名称,绝对路径
*
*/
public static boolean uploadFile(String remoteFile, File localFile)
throws IOException {
boolean flag = false;
InputStream in = new FileInputStream(localFile);
String remote = new String(remoteFile.getBytes("UTF-8"),"UTF-8");
if(ftpClient.storeFile(remote, in)){
flag = true;
System.out.println(localFile.getAbsolutePath()+"上传文件成功!");
}else{
flag = false;
System.out.println(localFile.getAbsolutePath()+"上传文件失败!");
}
in.close();
return flag;
}
阅读全文
0 0
- ftp上传
- ftp上传
- ftp上传
- ftp上传
- ftp上传
- FTP上传
- ftp上传
- ftp上传
- FTP上传
- FTP上传
- Ftp上传
- FTP上传
- FTP上传
- ftp上传
- ftp上传
- FTP上传
- FTP上传
- Ftp上传
- JavaScript/JavaScript对象
- Shader实现头像遮罩效果
- JavaScript
- Faster R-CNN 数据集的文件格式
- 子进程的环境变量问题
- FTP上传
- eclipse显示行号和Tab、空格、回车
- Java可变参数demo
- poi读取excel
- ajax的简单实例 XMLHttpRequest对象
- 05-树8 File Transfer (25分)---并查集
- 分布式集群高并发问题
- File类及其方法
- codeforces 810A Straight <<A>> 暴力