「javasftp服务器」2008ftp服务器怎么搭建

博主:adminadmin 2022-11-25 22:18:09 80

今天给各位分享javasftp服务器的知识,其中也会对2008ftp服务器怎么搭建进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java连接Sftp服务器后,使用第三方jar com.jcraft.jsch.ChannelSftp;用get()方法获取的InputStream流为空

java连接Sftp服务器后,使用第三方jar com.jcraft.jsch.ChannelSftp;用get()方法获取的InputStream流为空,检查下第3方初始化这些写对没有,或者就是没得到流,打印看看,解决问题。

Java怎么均衡访问多台ftp服务器

多次需要把文件上传到单独的服务器,而程序是在单独的服务器上部署的,在进行文件操作的时候就需要跨服务器进行操作包括:文件上传、文件下载、文件删除等。跨服务器文件操作一般是需要FTP协议和SFTP协议两种,现在就通过Java实现FTP协议的文件上传。要实现FTP操作文件需要引入jar包: commons-net-1.4.1.jar

参考资料来源:百度贴吧

java向SFTP服务器上传文件,如何判断服务器上的文件夹是否存在?

如果你的JAVA部署的tomcat,就是你要查找文件的服务器,那就用:

File file = new File("文件路径")。

如果你本地的JAVA想要访问远程的一个服务器的文件是否存在,就得用如下方法:

URL url = new URL(“文件路径:可以是本地服务器的路径,也可以是远程服务器的路径”)

HttpURLConnection urlcon = (HttpURLConnection) url.openConnection();

//message = urlcon.getHeaderField(0);

//文件存在‘HTTP/1.1 200 OK’ 文件不存在 ‘HTTP/1.1 404 Not Found’

Long TotalSize=Long.parseLong(urlcon.getHeaderField("Content-Length"));

if (TotalSize0){

return true;

}else{

return false;

}

Java是一种可以撰写跨平台应用软件的面向对象的程序设计语言。Java 技术具有卓越的通用性、高效性、平台移植性和安全性,广泛应用于PC、数据中心、游戏控制台、科学超级计算机、移动电话和互联网,同时拥有全球最大的开发者专业社群。

java sftp上传文件 ,在cd时 空指针异常,服务器上path存在

try {

s;

} catch (SftpException sException) {

if (s == sException.id) {// 如果文件夹不存在,则进行新建

s;

s;

}

}

用这个就可以解决sftp服务目录不存在问题

sftp免密用java怎么调用

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

import java.util.Properties;

import java.util.Vector;

import org.apache.commons.lang.ArrayUtils;

import com.huawei.bme.commons.om.log.DebugLog;

import com.huawei.bme.commons.om.log.LogFactory;

import com.huawei.icity.commons.constants.KeyConstant;

import com.huawei.icity.commons.constants.NumberConstant;

import com.huawei.icity.commons.exception.SPMException;

import com.huawei.icity.commons.log.IcityLogFactory;

import com.huawei.icity.commons.log.IcityRuntimeLog;

import com.huawei.icity.commons.sysconfig.StaticInitData;

import com.huawei.icity.commons.utils.StringTools;

import com.huawei.icity.omp.common.util.CommonTools;

import com.huawei.icity.omp.interfaces.hint.constant.TimetaskConstants;

import com.huawei.mdmc.bfm.cms.assist.common.domain.SingleTableModel;

import com.jcraft.jsch.Channel;

import com.jcraft.jsch.ChannelSftp;

import com.jcraft.jsch.ChannelS;

import com.jcraft.jsch.JSch;

import com.jcraft.jsch.JSchException;

import com.jcraft.jsch.Session;

import com.jcraft.jsch.SftpException;

/**

* SFTP公共处理类 〈提供SFTP文件上传,下载,获取指定目录 下文件名集合, 获取指定目录 下文件集合等方法

*

* @author mKF75022

* @version iCity Manager V100R002 2012-7-3

* @since iCity Manager V100R002C01

*/

public class SFTPTool

{

/**

* 调测日志记录器。

*/

private static final DebugLog DEBUGGER = LogFactory.getDebugLog(SFTPTool.class);

/**

* 运行日志记录器。

*/

private static final IcityRuntimeLog RUNTIMELOGGER = IcityLogFactory

.getRuntimeLog(SFTPTool.class);

/**

* Sftp客户端对象

*/

private ChannelSftp sftp = null;

/**

* SFTP IP地址

*/

private String ip;

/**

* SFTP 端口

*/

private String port;

/**

* SFTP 用户名

*/

private String userName;

/**

* SFTP 密码

*/

private String password;

/**

* SFTP上传模式:BINARY

*/

// private static final int BINARY_FILE_TYPE = 2;

/**

*

* 获取实例

*

* @return SFTPTool newinstance实例

*

*/

public static SFTPTool getNewInstance()

{

return new SFTPTool();

}

/**

* 初始化连接参数

*

* @param sftpIP

* IP

* @param sftpPort

* 端口

* @param sftpUsername

* 用户名

* @param sftpPassword

* 密码

*/

public void init(String sftpIP, String sftpPort, String sftpUsername, String sftpPassword)

{

// 获取SFTP连接信息

this.ip = sftpIP;

this.port = sftpPort;

this.userName = sftpUsername;

this.password = sftpPassword;

}

/**

* 从SFTP将符合约定命名的文件都下载到本地 .

*

* @param sftpDir

* SFTP服务器文件存放路径

* @param locDir

* 本地文件存放路径

* @param regex

* 指定文件名的格式

* @param needBackup

* 是否需要文件备份(true:是;false:否)

* @param needFullMatch

* 是否要求全局匹配(true:是;false:否)

* @param deleteFtpFile

* the delete ftp file

* @return 下载到本地的文件列表

*/

public ListFile synSFTPFileToLocal(String sftpDir, String locDir, String regex,

boolean needBackup, boolean needFullMatch, boolean deleteFtpFile)

{

ListFile files = new ArrayListFile(KeyConstant.INITIAL_NUMBER);

try

{

this.connect(ip, Integer.parseInt(this.port), userName, password);

// 获得FTP上文件名称列表

ListString ftpFileNameList = this.listFiles(sftpDir, regex, needFullMatch);

File localFile = null;

int size = ftpFileNameList.size();

// 根据每个FTP文件名称创建本地文件。

for (int i = 0; i size; i++)

{

// 下载源文件

localFile = this.download(sftpDir, locDir, ftpFileNameList.get(i), deleteFtpFile);

if (localFile.exists())

{

files.add(localFile);

}

if (needBackup)

{

// 备份源文件生成默认备份文件路径(据请求文件路径,生成同级目录的备份文件夹绝对路径)

String parentDir = sftpDir.substring(NumberConstant.INT_0,

sftpDir.lastIndexOf("/") + 1);

String backupDir = parentDir + TimetaskConstants.DEFAULT_BACKUP_DIRNAME;

boolean bakExists = openDir(backupDir);

if (bakExists)

{

this.uploadFile(backupDir, localFile);

}

else

{

boolean parentExists = openDir(parentDir);

if (parentExists)

{

s;

this.uploadFile(backupDir, localFile);

}

else

{

DEBUGGER.error("sftp parentDir no exisits ");

}

}

}

}

}

catch (Exception e)

{

DEBUGGER.error("synSFTPFileToLocal Exception", e);

}

finally

{

this.disconnect();

}

return files;

}

/**

* 连接sftp服务器

*

* @param sftpip

* ip地址

* @param sftpport

* 端口

* @param sftpusername

* 用户名

* @param sftppassword

* 密码

* @return channelSftp

* @throws SPMException

*/

public ChannelSftp connect(String sftpip, int sftpport, String sftpusername, String sftppassword)

{

sftp = new ChannelSftp();

try

{

JSch jsch = new JSch();

jsch.getSession(sftpusername, sftpip, sftpport);

Session sshSession = jsch.getSession(sftpusername, sftpip, sftpport);

RUNTIMELOGGER.info("Session created");

sshSession.setPassword(sftppassword);

Properties sshConfig = new Properties();

sshConfig.put("StrictHostKeyChecking", "no");

sshSession.setConfig(sshConfig);

// 设置超时时间为

sshSession.setTimeout(Integer.parseInt(StaticInitData.getFtpConnectTimeOut())

* NumberConstant.INT_1000);

sshSession.connect();

Channel channel = sshSession.openChannel("sftp");

channel.connect();

sftp = (ChannelSftp) channel;

// 设置文件类型

// ftpClient.setFileType(BINARY_FILE_TYPE);

// 与防火墙相关

// ftpClient.enterLocalPassiveMode();

}

catch (JSchException e)

{

DEBUGGER.error("JSchException : {}", e);

}

return sftp;

}

// /**

// * 创建指定文件夹

// *

// * @param dirName

// * dirName

// */

// public void mkDir(String dirName)

// {

// try

// {

// s;

// }

// catch (SftpException e)

// {

// DEBUGGER.error("mkDir Exception : " + e);

// }

// }

/**

* 创建指定文件夹

*

* @param dirName

* dirName

*/

public void mkDir(String dirName)

{

String[] dirs = dirName.split("/");

try

{

String now = s;

for (int i = 0; i dirs.length; i++)

{

boolean dirExists = openDir(dirs[i]);

if (!dirExists)

{

s(dirs[i]);

s(dirs[i]);

}

}

s;

}

catch (SftpException e)

{

DEBUGGER.error("mkDir Exception : " + e);

}

}

/**

* 打开指定目录

*

* @param directory

* directory

* @return 是否打开目录

*/

public boolean openDir(String directory)

{

try

{

s;

return true;

}

catch (SftpException e)

{

DEBUGGER.error("openDir Exception : " + e);

return false;

}

}

关于javasftp服务器和2008ftp服务器怎么搭建的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-11-25,除非注明,否则均为首码项目网原创文章,转载请注明出处。