当前位置:首页 > 代码 > 正文

java连接ftp代码(java ftpserver)

admin 发布:2022-12-19 16:47 150


本篇文章给大家谈谈java连接ftp代码,以及java ftpserver对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

如何用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);

}

如何用java代码实现ftp文件上传

import java.io.File;

import java.io.FileInputStream;

import org.apache.commons.net.;

import org.apache.commons.net.;

public class test {

private FTPClient ftp;

/**

*

* @param path 上传到ftp服务器哪个路径下

* @param addr 地址

* @param port 端口号

* @param username 用户名

* @param password 密码

* @return

* @throws Exception

*/

private boolean connect(String path,String addr,int port,String username,String password) throws Exception {

boolean result = false;

ftp = new FTPClient();

int reply;

;

;

;

reply = ;

if (!FTPReply.isPositiveCompletion(reply)) {

;

return result;

}

;

result = true;

return result;

}

/**

*

* @param file 上传的文件或文件夹

* @throws Exception

*/

private void upload(File file) throws Exception{

if(file.isDirectory()){

(file.getName());

(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);

;

}else{

File file2 = new File(file.getPath()+"\\"+files[i]);

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访问

在主函数中,完成服务器端口的侦听和服务线程的创建。我们利用一个静态字符串变量initDir 来保存服务器线程运行时所在的工作目录。服务器的初始工作目录是由程序运行时用户输入的,缺省为C盘的根目录。

具体的代码如下:

public class ftpServer extends Thread{

private Socket socketClient;

private int counter;

private static String initDir;

public static void main(String[] args){

if(args.length != 0) {

initDir = args[0];

}else{ initDir = "c:";}

int i = 1;

try{

System.out.println("ftp server started!");

//监听21号端口

ServerSocket s = new ServerSocket(21);

for(;;){

//接受客户端请求

Socket incoming = s.accept();

//创建服务线程

new ftpServer(incoming,i).start();

i++;

}

}catch(Exception e){}

}

Java 代码操作带SSL的FTP服务器

参考

client = new FTPSClient(implictSSL);

KeyManagerFactory kmf = KeyManagerFactory.getInstance("X509");

kmf.init(KeyStore.getInstance("BKS"), "wshr.ut".toCharArray());

client.setTrustManager(new X509TrustManager() {

public X509Certificate[] getAcceptedIssuers() { return null; }

public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { }

public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { }

});

client.setKeyManager(kmf.getKeyManagers()[0]);

client.setNeedClientAuth(false);

client.setUseClientMode(false);

java连接ftp代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java ftpserver、java连接ftp代码的信息别忘了在本站进行查找喔。

版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;

本文地址:http://ahzz.com.cn/post/13882.html


取消回复欢迎 发表评论:

分享到

温馨提示

下载成功了么?或者链接失效了?

联系我们反馈

立即下载