本文目录一览:
- 1、java如何测试连接ftp是否通
- 2、java连接ftp是主动模式还是被动模式
- 3、JAVA编写FTP连接报错java.net.ConnectException: Connection refused: connect FTP
- 4、如何用java连接到ftp上
java如何测试连接ftp是否通
java测试连接ftp是否连通可以使用判断是否有异常来决定,实例如下:
/**
* connectServer
* 连接ftp服务器
* @throws java.io.IOException
* @param path 文件夹,空代表根目录
* @param password 密码
* @param user 登陆用户
* @param server 服务器地址
*/
public void connectServer(String server, String user, String password, String path)
throws IOException
{
// server:FTP服务器的IP地址;user:登录FTP服务器的用户名
// password:登录FTP服务器的用户名的口令;path:FTP服务器上的路径
ftpClient = new FtpClient();
ftpClient.openServer(server);
ftpClient.login(user, password);
//path是ftp服务下主目录的子目录
if (path.length() != 0) ftpClient.cd(path);
//用2进制上传、下载
ftpClient.binary();
}
/**
* upload
* 上传文件
* @throws java.lang.Exception
* @return -1 文件不存在
* -2 文件内容为空
* 0 成功上传,返回文件的大小
* @param newname 上传后的新文件名
* @param filename 上传的文件
*/
public long upload(String filename,String newname) throws Exception
{
long result = 0;
TelnetOutputStream os = null;
FileInputStream is = null;
try {
java.io.File file_in = new java.io.File(filename);
if (!file_in.exists()) return -1;
if (file_in.length()==0) return -2;
os = ftpClient.put(newname);
result = file_in.length();
is = new FileInputStream(file_in);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
os.write(bytes, 0, c);
}
} finally {
if (is != null) {
is.close();
}
if (os != null) {
os.close();
}
}
return result;
}
/**
* upload
* @throws java.lang.Exception
* @return
* @param filename
*/
public long upload(String filename)
throws Exception
{
String newname = "";
if (filename.indexOf("/")-1)
{
newname = filename.substring(filename.lastIndexOf("/")+1);
}else
{
newname = filename;
}
return upload(filename,newname);
}
/**
* download
* 从ftp下载文件到本地
* @throws java.lang.Exception
* @return
* @param newfilename 本地生成的文件名
* @param filename 服务器上的文件名
*/
public long download(String filename,String newfilename)
throws Exception
{
long result = 0;
TelnetInputStream is = null;
FileOutputStream os = null;
try
{
is = ftpClient.get(filename);
java.io.File outfile = new java.io.File(newfilename);
os = new FileOutputStream(outfile);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
os.write(bytes, 0, c);
result = result + c;
}
} catch (IOException e)
{
e.printStackTrace();
}
finally {
if (is != null) {
is.close();
}
if (os != null) {
os.close();
}
}
return result;
}
/**
* 取得某个目录下的所有文件列表
*
*/
public List getFileList(String path)
{
List list = new ArrayList();
try
{
DataInputStream dis = new DataInputStream(ftpClient.nameList(path));
String filename = "";
while((filename=dis.readLine())!=null)
{
list.add(filename);
}
} catch (Exception e)
{
e.printStackTrace();
}
return list;
}
/**
* closeServer
* 断开与ftp服务器的链接
* @throws java.io.IOException
*/
public void closeServer()
throws IOException
{
try
{
if (ftpClient != null)
{
ftpClient.closeServer();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String [] args) throws Exception
{
FtpUtil ftp = new FtpUtil();
try {
//连接ftp服务器
("10.163.7.15", "cxl", "1", "info2");
/** 上传文件到 info2 文件夹下 */
System.out.println("filesize:"+("f:/download/Install.exe")+"字节");
/** 取得info2文件夹下的所有文件列表,并下载到 E盘下 */
List list = (".");
for (int i=0;ilist.size();i++)
{
String filename = (String)list.get(i);
System.out.println(filename);
(filename,"E:/"+filename);
}
} catch (Exception e) {
///
}finally
{
;
}
}
}
java连接ftp是主动模式还是被动模式
正常情况下,默认使用主动模式 连接ftp;如果ftp仍然是登陆成功但是没有上传或下载文件,就在登陆后加入一行代码,客户端使用被动方式连接ftp服务端
ftpC.login(user, password);
// ftpC.enterLocalPassiveMode();
if (null != remotePath) {
// 打开进入指定目录
ftpC.changeWorkingDirectory(remotePath);
}
JAVA编写FTP连接报错java.net.ConnectException: Connection refused: connect FTP
你用的FTPClient引入不对吧,我们项目上都是用的
import org.apache.commons.net.;
import org.apache.commons.net.;
import org.apache.commons.net.;
下面是我们项目上用到的FTP的实现代码(FTP需要先连接,再登录,之后就是校验登录是否成功),具体代码如下:
/**
* 获取FTPClient对象
*
* @param ftpHost FTP主机服务器
* @param ftpPassword FTP 登录密码
* @param ftpUserName FTP登录用户名
* @param ftpPort FTP端口 默认为21
* @return FTPClient
* @throws Exception
*/
public static FTPClient getFTPClient(String ftpHost, String ftpUserName,
String ftpPassword, int ftpPort) throws Exception {
try {
FTPClient ftpClient = new FTPClient();
ftpClient.connect(ftpHost, ftpPort);// 连接FTP服务器
ftpClient.login(ftpUserName, ftpPassword);// 登陆FTP服务器
if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
logger.error("未连接到FTP,用户名或密码错误!");
ftpClient.disconnect();
return null;
} else {
logger.info("FTP连接成功!");
return ftpClient;
}
} catch (SocketException socketException) {
logger.error("FTP的IP地址可能错误,请正确配置!");
throw socketException;
} catch (IOException ioException) {
logger.error("FTP的端口错误,请正确配置!");
throw ioException;
}
}
如何用java连接到ftp上
现在已经封装好了的方法,不需要任何其他知识即可连接的。只需要知道ftp登录用户名、密码、端口、存储路径即可。
package zn.ccfccb.util;
import hkrt.b2b.view.util.Log;
import hkrt.b2b.view.util.ViewUtil;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import org.apache.commons.net.;
import org.apache.commons.net.;
public class CCFCCBFTP {
/**
* 上传文件
*
* @param fileName
* @param plainFilePath 明文文件路径路径
* @param filepath
* @return
* @throws Exception
*/
public static String fileUploadByFtp(String plainFilePath, String fileName, String filepath) throws Exception {
FileInputStream fis = null;
ByteArrayOutputStream bos = null;
FTPClient ftpClient = new FTPClient();
String bl = "false";
try {
fis = new FileInputStream(plainFilePath);
bos = new ByteArrayOutputStream(fis.available());
byte[] buffer = new byte[1024];
int count = 0;
while ((count = fis.read(buffer)) != -1) {
bos.write(buffer, 0, count);
}
bos.flush();
Log.info("加密上传文件开始");
Log.info("连接远程上传服务器"+CCFCCBUtil.CCFCCBHOSTNAME+":"+22);
ftpClient.connect(CCFCCBUtil.CCFCCBHOSTNAME, 22);
ftpClient.login(CCFCCBUtil.CCFCCBLOGINNAME, CCFCCBUtil.CCFCCBLOGINPASSWORD);
// Log.info("连接远程上传服务器"+"192.168.54.106:"+2021);
// ftpClient.connect("192.168.54.106", 2021);
// ftpClient.login("hkrt-CCFCCBHK", "3OLJheziiKnkVcu7Sigz");
FTPFile[] fs;
fs = ftpClient.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(filepath)) {
bl="true";
ftpClient.changeWorkingDirectory("/"+filepath+"");
}
}
Log.info("检查文件路径是否存在:/"+filepath);
if("false".equals(bl)){
ViewUtil.dataSEErrorPerformedCommon( "查询文件路径不存在:"+"/"+filepath);
return bl;
}
ftpClient.setBufferSize(1024);
ftpClient.setControlEncoding("GBK");
// 设置文件类型(二进制)
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.storeFile(fileName, fis);
Log.info("上传文件成功:"+fileName+"。文件保存路径:"+"/"+filepath+"/");
return bl;
} catch (Exception e) {
throw e;
} finally {
if (fis != null) {
try {
fis.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}
}
if (bos != null) {
try {
bos.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}
}
}
}
/**
*下载并解压文件
*
* @param localFilePath
* @param fileName
* @param routeFilepath
* @return
* @throws Exception
*/
public static String fileDownloadByFtp(String localFilePath, String fileName,String routeFilepath) throws Exception {
FileInputStream fis = null;
ByteArrayOutputStream bos = null;
FileOutputStream fos = null;
FTPClient ftpClient = new FTPClient();
String SFP = System.getProperty("file.separator");
String bl = "false";
try {
Log.info("下载并解密文件开始");
Log.info("连接远程下载服务器"+CCFCCBUtil.CCFCCBHOSTNAME+":"+22);
ftpClient.connect(CCFCCBUtil.CCFCCBHOSTNAME, 22);
ftpClient.login(CCFCCBUtil.CCFCCBLOGINNAME, CCFCCBUtil.CCFCCBLOGINPASSWORD);
// ftpClient.connect(CMBCUtil.CMBCHOSTNAME, 2021);
// ftpClient.login(CMBCUtil.CMBCLOGINNAME, CMBCUtil.CMBCLOGINPASSWORD);
FTPFile[] fs;
ftpClient.makeDirectory(routeFilepath);
ftpClient.changeWorkingDirectory(routeFilepath);
bl = "false";
fs = ftpClient.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(fileName)) {
bl = "true";
Log.info("下载文件开始。");
ftpClient.setBufferSize(1024);
// 设置文件类型(二进制)
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
InputStream is = ftpClient.retrieveFileStream(fileName);
bos = new ByteArrayOutputStream(is.available());
byte[] buffer = new byte[1024];
int count = 0;
while ((count = is.read(buffer)) != -1) {
bos.write(buffer, 0, count);
}
bos.flush();
fos = new FileOutputStream(localFilePath+SFP+fileName);
fos.write(bos.toByteArray());
Log.info("下载文件结束:"+localFilePath);
}
}
Log.info("检查文件是否存:"+fileName+" "+bl);
if("false".equals(bl)){
ViewUtil.dataSEErrorPerformedCommon("查询无结果,请稍后再查询。");
return bl;
}
return bl;
} catch (Exception e) {
throw e;
} finally {
if (fis != null) {
try {
fis.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}
}
if (bos != null) {
try {
bos.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}
}
if (fos != null) {
try {
fos.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}