「javasftp下载」java ftp sftp
今天给各位分享javasftp下载的知识,其中也会对java ftp sftp进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、用java实现sftp下载,报2: No such file
- 2、用java写一个sftp客户端程序 能够上一个sftp的目录下载文件到本地指定目录
- 3、sftp免密用java怎么调用
- 4、用java实现sftp的客户端,channel.connect()的时候,抛出异常,收到信息过长,然后就没连上。怎么回事?
- 5、java FTP下载文件在代码中如何实现知道下载完成?
用java实现sftp下载,报2: No such file
String directory = ".";
目测 /home/httpd/test/ 是*nix的路径,而你用的是Windows,一来,没有这个目录,二来SFTP服务器分配的目录应该也不是这个,所以改".",即当前目录试试,如果不行就留空,或者改"/"
用java写一个sftp客户端程序 能够上一个sftp的目录下载文件到本地指定目录
不需要装软件,下载需要执行sftp服务器目录和本地目录
你说的directory空指针,应该是你没有指定从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;
}
}
用java实现sftp的客户端,channel.connect()的时候,抛出异常,收到信息过长,然后就没连上。怎么回事?
首先,不太明确你要问什么?
J2EE是java企业级应用,它里面关于安全方面的东西很多!
权限管理?信息安全?授权服务?访问控制?数据机密性?
要是你想全部都了解!
在这里提问是不行的!太庞杂了!
如果 你要问的是 java ftp传输过程中的一些安全注意事项!
这里建议 你使用 apache的开源项目。他们把基于java的ftp操作都封装好了!
安全相关都有保证。
我下面可以给你复制一段 ftp下载远程终端的java代码!你可以参考一下!
需要下载 org.apache.commons.net包
可以到 网站下载!
/**
* Description: 从FTP服务器下载文件
* @param ip FTP服务器的ip地址
* @param port FTP服务器端口,默认为:21
* @param username FTP登录账号
* @param password FTP登录密码
* @param remotePath FTP服务器上的相对路径
* @param fileName 要下载的文件名
* @param localPath 下载后保存到本地的路径
* @return
*/
public static boolean downFile(String ip, int port,String username, String password, String remotePath,String fileName,String localPath,String localfile) {
boolean success = false;
FTPClient ftp = new FTPClient();
try {
int reply;
(ip, port);
//处理中文转码操作
("GBK");
FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);
conf.setServerLanguageCode("zh");
(username, password);//登录
reply = ;
if (!FTPReply.isPositiveCompletion(reply)) {
;
return success;
}
;//转移到FTP服务器目录
FTPFile[] fs = ;
for(int i = 0; i fs.length; i++){
FTPFile ff = fs[i];
if(ff.getName().equals(fileName)){
String localfilename = localfile;//按规则设置文件名这里你要下载文件,可以自己定义下载之后的文件名
File localFile = new File(localPath+File.separator+localfilename);
OutputStream is = new FileOutputStream(localFile);
//此处retrieveFile的第一个参数由GBK转为ISO-8859-1编码。否则下载后的文件内容为空。
(new String(ff.getName().getBytes("GBK"),"ISO-8859-1"), is);
is.close();
}
}
;
success = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if () {
try {
;
} catch (IOException ioe) {
}
}
}
return success;
}
java FTP下载文件在代码中如何实现知道下载完成?
public static void downloadFileFtp(KmConfig kmConfig,String fileName, String clientFileName, OutputStream outputStream){
try {
String ftpHost = kmConfig.getFtpHost();
int port = kmConfig.getFtpPort();
String userName = kmConfig.getFtpUser();
String passWord = kmConfig.getFtpPassword();
String path = kmConfig.getFtpPath();
FtpClient ftpClient = new FtpClient(ftpHost, port);// ftpHost为FTP服务器的IP地址,port为FTP服务器的登陆端口,ftpHost为String型,port为int型。
ftpClient.login(userName, passWord);// userName、passWord分别为FTP服务器的登陆用户名和密码
ftpClient.binary();
ftpClient.cd(path);// path为FTP服务器上保存上传文件的路径。
try {
TelnetInputStream in = ftpClient.get(fileName);
byte[] bytes = new byte[1024];
int cnt=0;
while ((cnt=in.read(bytes,0,bytes.length)) != -1) {
outputStream.write(bytes, 0, cnt);
}
//##############################################
//这里文件就已经下载完了,自己理解一下
//#############################################
outputStream.close();
in.close();
} catch (Exception e) {
ftpClient.closeServer();
e.printStackTrace();
}
ftpClient.closeServer();
} catch (Exception e) {
System.out.println("下载文件失败!请检查系统FTP设置,并确认FTP服务启动");
}
}
javasftp下载的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java ftp sftp、javasftp下载的信息别忘了在本站进行查找喔。
发布于:2022-12-13,除非注明,否则均为
原创文章,转载请注明出处。