ftp上传下载文件到服务器

今天公司写了ftp代码上传下载 用的apache提供的common-net包
package com.zte.xh.fund.util;

import java.io.BufferedOutputStream;
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.nio.channels.FileChannel;
import java.util.Properties;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

import com.jfinal.kit.PropKit;
import com.mysql.jdbc.StringUtils;

/**
* 用来对文件操作的一些方法
*
* @author Jay_Lee
*
*/
public class FileUtil {
// ftp的propertis信息
private static Properties properties = PropKit.use(
"config/ftpclient.properties", "utf-8").getProperties();
// ftp上传的信息
private static final String IP = properties.getProperty("ip"); // 服务器IP地址
private static final String USER_NAME = properties.getProperty("userName"); // 用户名
private static final String PWD = properties.getProperty("pwd"); // 密码
private static final int PORT = Integer.valueOf(properties
.getProperty("port")); // 端口号
public static final String DOWN_PATH = properties.getProperty("filepath");
public static FTPClient ftpClient = null;

// copy图片文件
public static void copyFile(File sourceFile, File toFile) {
FileInputStream fi = null;
FileOutputStream fo = null;
FileChannel in = null;
FileChannel out = null;
try {
fi = new FileInputStream(sourceFile);
fo = new FileOutputStream(toFile);
// 得到对应的文件通道
in = fi.getChannel();
// 得到对应的文件通道
out = fo.getChannel();
// 连接两个通道,并且从in通道读取,然后写入out通道
in.transferTo(0, in.size(), out);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fi.close();
in.close();
fo.close();
out.close();
} catch (IOException e) {

e.printStackTrace();

}
}
}


// ftp上传文件到服务器
public static String uploadToFtp(String ftpPath, String filePath,
String fileName) throws SocketException, IOException {
String returnMessage = "0";
try {
ftpConne();
int returnCode = ftpClient.getReplyCode();
FileInputStream fis = null;
if (FTPReply.isPositiveCompletion(returnCode)) {
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
// 登陆成功设置上传目录
File f = new File(filePath);
createPath(ftpPath);
ftpClient.changeWorkingDirectory(ftpPath);
fis = new FileInputStream(f);
ftpClient.storeFile(fileName, fis);
returnMessage = "1";
System.out.println(returnMessage + "ok");
} else {
returnMessage = "0";
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("FTP客户端出错!", e);
} finally {
// 关闭链接
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
}
return returnMessage;
}

// ftp判断是否存在目录,不存在则创建
public static void createPath(String paths) {
try {
// 解析path的地址是否包含子路劲
String[] ps = paths.split("/");
String rp = "";
boolean isexist = false;
for (String path : ps) {
if (StringUtils.isNullOrEmpty(path)) {
continue;
}
rp += "/" + path;
isexist = ftpClient.changeWorkingDirectory(rp);
if (!isexist) {
ftpClient.makeDirectory(path);
ftpClient.changeWorkingDirectory(rp);
}
}
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("创建文件夹出错!", e);
}
}

/**
* 从ftp下载文件
*
* @param localFileName
* 本地文件名
* @param remoteFileName
* 远程文件名
*/
public static String downFromFtp(String localPath, String remotePath,
String fileName) {
BufferedOutputStream buffOut = null;
String result = "";
try {
ftpConne();
buffOut = new BufferedOutputStream(new FileOutputStream(localPath));
boolean isExist = ftpClient.changeWorkingDirectory(remotePath);
if (!isExist) {
result = "3";
System.out.println("路径不存在");
}
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.retrieveFile(fileName, buffOut);
result = "1";
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (buffOut != null)
buffOut.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}

// ftp的链接
public static void ftpConne() {
try {
ftpClient = new FTPClient();
ftpClient.setBufferSize(1024 * 1024 * 2);
ftpClient.setControlEncoding("UTF-8");
ftpClient.enterLocalPassiveMode();
ftpClient.connect(IP, PORT);
ftpClient.login(USER_NAME, PWD);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("FTP链接出错!", e);
}
}

// 测试demo
public static void main(String[] args) {
FileUtil.ftpConne();
/*
* try { FileUtil.uploadToFtp(PATH, "f:" + File.separator +
* "checkAccount.xls", "checkAccount.xls"); } catch (IOException e) { //
* TODO Auto-generated catch block e.printStackTrace(); }
*/

FileUtil.downFromFtp("f:" + File.separator + "xxxx.xls", DOWN_PATH,"checkAccount.xls");

}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值