java 实现Serv-U FTP 和 SFTP 上传 下载
两种ftp使用java的实现方式 ,代码都已测试
第一种:Serv-U FTP
先决条件:
1、Serv-U FTP服务器搭建成功。
2、jar包需要:版本不限制
<!--ftp上传需要的jar-->
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>1.4.1</version>
</dependency> 实现代码:
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply; import javax.swing.*;
import java.io.*;
import java.net.SocketException; public class FtpUtil { private static FTPClient ftpClient = new FTPClient(); /**
* 获取FTPClient对象
*
* @param ftpHost FTP主机服务器
* @param ftpPassword FTP 登录密码
* @param ftpUserName FTP登录用户名
* @param ftpPort FTP端口 默认为21
* @return
*/
public FTPClient getFTPClient(String ftpHost, String ftpUserName,
String ftpPassword, int ftpPort) { try { ftpClient.connect(ftpHost, ftpPort);// 连接FTP服务器
ftpClient.login(ftpUserName, ftpPassword);// 登陆FTP服务器
if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
System.out.println("未连接到FTP,用户名或密码错误。");
ftpClient.disconnect();
} else {
System.out.println("FTP连接成功。");
}
} catch (SocketException e) {
e.printStackTrace();
System.out.println("FTP的IP地址可能错误,请正确配置。");
} catch (IOException e) {
e.printStackTrace();
System.out.println("FTP的端口错误,请正确配置。");
}
return ftpClient;
} /**
* 从FTP服务器下载文件
* @param ftpPath FTP服务器中文件所在路径
* @param localPath 下载到本地的位置
* @param fileName 文件名称
*/
public void downloadFtpFile( String ftpPath, String localPath,
String fileName) { try { ftpClient.setControlEncoding("UTF-8"); // 中文支持
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
ftpClient.changeWorkingDirectory(ftpPath); File localFile = new File(localPath + File.separatorChar + fileName);
OutputStream os = new FileOutputStream(localFile);
ftpClient.retrieveFile(fileName, os);
os.close();
ftpClient.logout(); } catch (FileNotFoundException e) {
System.out.println("没有找到" + ftpPath + "文件");
e.printStackTrace();
} catch (SocketException e) {
System.out.println("连接FTP失败.");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
System.out.println("文件读取错误。");
e.printStackTrace();
} } /**
* Description: 向FTP服务器上传文件
* @param ftpPath FTP服务器中文件所在路径 格式: ftptest/aa
* @param fileName ftp文件名称
* @param input 文件流
* @return 成功返回true,否则返回false
*/
public boolean uploadFile( String ftpPath,
String fileName,InputStream input) {
boolean success = false; try {
int reply; reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
return success;
}
ftpClient.setControlEncoding("UTF-8"); // 中文支持
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
ftpClient.changeWorkingDirectory(ftpPath); ftpClient.storeFile(fileName, input); input.close();
ftpClient.logout();
success = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException ioe) {
}
}
}
return success;
} public static void main(String args[]){ String ftpHost = "127.0.0.1";
String ftpUserName = "servUFtp";
String ftpPassword = "14oioppii";
Integer port = 21;
String ftpPath = "/";
String localPath = "F:\\";
String fileName = "text.jpg";
//上传一个文件
try{ FileInputStream in=new FileInputStream(new File(localPath));
FtpUtil ftpUtil = new FtpUtil(); ftpUtil.getFTPClient(ftpHost, ftpUserName, ftpPassword, port);
// 上传文件
boolean flag = ftpUtil.uploadFile( ftpPath, fileName,in);
// 下载文件
ftpUtil.downloadFtpFile( "/000.jpg", localPath, fileName);
if(flag){
System.out.println("文件上传ftp成功,请检查ftp服务器。"); }else{
System.out.println("文件上传ftp异常。请重试!"); }
} catch (FileNotFoundException e){
e.printStackTrace();
System.out.println(e);
}
} }
第二种:SFTP
先决条件:
1、SFTP服务器搭建成功
2、jar需要:版本不限
<!--sftp上传需要的jar-->
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.55</version>
</dependency> 实现代码:
import com.jcraft.jsch.*; import java.io.*;
import java.util.Properties; /**
* @Author : guoyanan
* @Title : Sftp工具类
* @Time : 2019/04/18 14:52
* @Document : 提供文件上传功能
*/
public class SFtpUtils { // 初始化单例对象
private static SFtpUtils sFtpUtils = new SFtpUtils();
private String host;//服务器连接ip
private String username;//用户名
private String password;//密码
private int port = 22;//端口号
private ChannelSftp sftp = null;
private Session sshSession = null; /**
* 初始化sftp的单例对象
* @return
*/
public static SFtpUtils getInstance()
{
return sFtpUtils;
} /**
* 初始化sft链接信息,必须先做这个
* @param host 远程主机ip
* @param port 端口号
* @param username 账号
* @param password 密码
*/
public void init(String host, int port, String username, String password)
{
this.host = host;
this.username = username;
this.password = password;
this.port = port; } /**
* 通过SFTP连接服务器
*/
public void connect()
{
try
{
JSch jsch = new JSch();
jsch.getSession(username, host, port);
sshSession = jsch.getSession(username, host, port);
sshSession.setPassword(password);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
sshSession.connect();
Channel channel = sshSession.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel; }
catch (Exception e)
{
e.printStackTrace();
}
} /**
* 关闭连接
*/
public void disconnect()
{
if (this.sftp != null)
{
if (this.sftp.isConnected())
{
this.sftp.disconnect(); }
}
if (this.sshSession != null)
{
if (this.sshSession.isConnected())
{
this.sshSession.disconnect(); }
}
} /**
* sftp下载文件
* @param remoteFielPath 远程文件路径
* @param localFilePath 本地下载路径
* @return
* @throws SftpException
* @throws FileNotFoundException
*/
public boolean downLoadFile(String remoteFielPath,String localFilePath) throws SftpException, FileNotFoundException {
// 检查文件是否存在
SftpATTRS sftpATTRS = sftp.lstat(remoteFielPath);
if(!sftpATTRS.isReg()){
// 不是一个文件,返回false
return false;
}
// 下载文件到本地
sftp.get(remoteFielPath,localFilePath); return true;
} /**
* 下载文件放回文件数据
* @param remoteFielPath
* @return
* @throws SftpException
* @throws IOException
*/
public boolean downLoadFileTwo(String remoteFielPath, String localFilePath) throws SftpException, IOException {
// 检查文件是否存在
SftpATTRS sftpATTRS = sftp.lstat(remoteFielPath);
// 判断是否是一个文件
if(sftpATTRS.isReg()){
// 下载文件到本地
InputStream inputStream = sftp.get(remoteFielPath);
/**今天想写下从sftp下载文件到本地,虽然sftp提供了get(String remotePath,String LocalPath)方法,将远程文件写入到本地。
* 但还是想属性下从远程获取InputStream对象写入到本地的方式。
* 遇到的问题:刚开始只想这实现,就是获取byte对象写入到本地文件,先用ByteArrayInputStream怎么转都无法获取到bytes对象
* 放入到FileOutputStream对象中。搞了老半天都没有搞定,或许有ByteArrayInputStream对象下载的方式但没有找到。
* 正解:如下*/
// 通过BufferedInputStream对象缓存输入流对象获取远程的输入流
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
// 创建本地文件信息
File file = new File("F:\\456.jpg");
// 将本地文件放入到 本地文件输出流
FileOutputStream fileOutputStream = new FileOutputStream(file);
// 将本地文件输出流 放入到 缓存输出流对象
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
// 声明每次获取的byte长度
int len = 2048;
// 初始化byte[]
byte[] bytes = new byte[len];
/**通过BufferedInputStream对象获取 远程文件 InputStream的bytes字节信息,并循环添加到BufferedOutputStream缓存输出流对象中,
* 将远程bytes数据写入到本地文件中
* 遇到的问题:这里我一直纠结于为什么这样写会实现想要的效果,是如何写入到本地的
* 其实这不是开发中遇到的问题的事情了,是自己困于自己设定的一个纠结情绪中了。也就是俗称的牛角尖。
* 切记,一定不可以钻牛角尖。因为开发都是有语言规则的。按照规则来就能实现效果,脱离规则即使神仙也无能为力。
* */
while ((len = bufferedInputStream.read(bytes)) != -1){
bufferedOutputStream.write(bytes,0,len);
}
bufferedOutputStream.flush();
bufferedInputStream.close();
bufferedOutputStream.close();
fileOutputStream.close();
inputStream.close();
return true;
}
return false; }
/**
* 上传单个文件,通过文件路径上传
* @param remotePath:远程保存目录
* @param remoteFileName:保存文件名
* @param localPath:本地上传目录(以路径符号结束)
* @param localFileName:上传的文件名
* @return
*/
public boolean uploadFile(String remotePath, String remoteFileName,String localPath, String localFileName)
{
FileInputStream in = null;
try
{ // 创建目录
createDir(remotePath);
File file = new File(localPath + localFileName);
in = new FileInputStream(file);
sftp.put(in, remoteFileName);
return true;
}catch (FileNotFoundException e)
{
e.printStackTrace();
}catch (SftpException e)
{
e.printStackTrace();
}
finally
{
if (in != null)
{
try
{
in.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
return false;
} /**
* 上传文件到sftp,通过输入流上传
* @param remotePath
* @param remoteFileName
* @param inputStream
* @return
*/
public boolean uploadFile(String remotePath, String remoteFileName,InputStream inputStream)
{
FileInputStream in = null;
try
{ // 创建目录
createDir(remotePath);
sftp.put(inputStream, remoteFileName);
return true;
}catch (SftpException e)
{
e.printStackTrace();
}
finally
{
if (in != null)
{
try
{
in.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
return false;
} /**
* 创建目录
* @param createpath
* @return
*/
public boolean createDir(String createpath)
{
try
{
if (isDirExist(createpath))
{
// 有时候,开发在填写路径的时候第一个位置会忘记加"/"的根路径
// 这回引发cd操作是发生:NO Such File 异常,所以这里处理下
if(!(createpath.substring(0,1)=="/")){
createpath="/"+createpath;
}
this.sftp.cd(createpath);
return true;
}
String pathArry[] = createpath.split("/");
StringBuffer filePath = new StringBuffer("/");
for (String path : pathArry)
{
if (path.equals(""))
{
continue;
}
filePath.append(path + "/");
if (isDirExist(filePath.toString()))
{
sftp.cd(filePath.toString());
}
else
{
// 建立目录
sftp.mkdir(filePath.toString());
// 进入并设置为当前目录
sftp.cd(filePath.toString());
} }
this.sftp.cd(createpath);
return true;
}
catch (SftpException e)
{
e.printStackTrace();
}
return false;
} /**
* 判断目录是否存在,linux目录必须最前方带有"/"
* @param directory
* @return
*/
public boolean isDirExist(String directory)
{
boolean isDirExistFlag = false;
try
{
// 有时候,开发在填写路径的时候第一个位置会忘记加"/"的根路径
// 这回引发cd操作是发生:NO Such File 异常,所以这里处理下
if(!(directory.substring(0,1)=="/")){
directory="/"+directory;
}
SftpATTRS sftpATTRS = sftp.lstat(directory);
isDirExistFlag = true;
return sftpATTRS.isDir();
}
catch (Exception e)
{
if (e.getMessage().toLowerCase().equals("no such file"))
{
isDirExistFlag = false;
}
}
return isDirExistFlag;
} public static void main(String arg[]) throws IOException, SftpException {
// 获取图片的InputStream对象,并将图片生成到sftp上
SFtpUtils sFtpUtils= SFtpUtils.getInstance();
sFtpUtils.init("127.0.0.1",22, "sftpuser", "UIOPOopi");
sFtpUtils.connect();
// 上传文件
/*File file = new File("F:\\OK.jpg");
InputStream in = new FileInputStream(file);
boolean flag = sFtpUtils.uploadFile("/app/xwapp/Test","1111.jpg",in);*/ // 下载文件到本地方法1
// boolean flag = sFtpUtils.downLoadFile("/app/xwapp/Test/1111.jpg","F:\\OKbak.jpg");
// 下载文件到本地方法2
boolean flag = sFtpUtils.downLoadFileTwo("/app/xwapp/Test/1111.jpg","F:\\OKbak.jpg");
if(flag){
System.out.println("处理成功");
}else {
System.out.println("处理失败");
}
sFtpUtils.disconnect();
}
}
java 实现Serv-U FTP 和 SFTP 上传 下载的更多相关文章
- java:工具(汉语转拼音,压缩包,EXCEL,JFrame窗口和文件选择器,SFTP上传下载,FTP工具类,SSH)
1.汉语转拼音: import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.pinyin4j.format.HanyuP ...
- Java Sftp上传下载文件
需要使用jar包 jsch-0.1.50.jar sftp上传下载实现类 package com.bstek.transit.sftp; import java.io.File; import ja ...
- THINKPHP 3.2 PHP SFTP上传下载 代码实现方法
一.SFTP介绍:使用SSH协议进行FTP传输的协议叫SFTP(安全文件传输)Sftp和Ftp都是文件传输协议.区别:sftp是ssh内含的协议(ssh是加密的telnet协议), 只要sshd服 ...
- Xshell5下利用sftp上传下载传输文件
sftp是Secure File Transfer Protocol的缩写,安全文件传送协议.可以为传输文件提供一种安全的加密方法.sftp 与 ftp 有着几乎一样的语法和功能.SFTP 为 SSH ...
- SFTP上传下载文件、文件夹常用操作
SFTP上传下载文件.文件夹常用操作 1.查看上传下载目录lpwd 2.改变上传和下载的目录(例如D盘):lcd d:/ 3.查看当前路径pwd 4.下载文件(例如我要将服务器上tomcat的日志文 ...
- python使用ftplib模块实现FTP文件的上传下载
python已经默认安装了ftplib模块,用其中的FTP类可以实现FTP文件的上传下载 FTP文件上传下载 # coding:utf8 from ftplib import FTP def uplo ...
- java之SFTP上传下载
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.ut ...
- java实操之使用jcraft进行sftp上传下载文件
sftp作为临时的文件存储位置,在某些场合还是有其应景的,比如对账文件存放.需要提供一个上传的工具类.实现方法参考下: pom.xml中引入类库: <dependency> <gro ...
- SFTP上传下载(C#)
sftp是ftp协议的升级版本,是牺牲上传速度为代价,换取安全性能,本人开始尝试使用Tamir.SharpSSH.dll但它对新版本的openssh 不支持,所有采用Ssh.Net方式 需要依赖:Re ...
随机推荐
- C# Remoting双向通信
闲来无事想玩玩双向通信,实现类似QQ的互发消息的功能.于是乎开始学习.Net Remoting. .Net Remoting 是由客户端通过Remoting,访问通道以获得服务端对象,再通过代理解析为 ...
- libprotobuf 编译错误处理
1. 编译完链接的时候报错undefined reference to well_known_types_js' 出现这个错误的原因是升级gcc导致的,是程序依赖的include文件和实际链接的文件不 ...
- django--admin模型层
django amdin是django提供的一个后台管理页面,改管理页面提供完善的html和css,使得你在通过Model创建完数据库表之后,就可以对数据进行增删改查,而使用django admin ...
- django的序列化问题
Django中的序列化主要应用在将数据库中检索的数据返回给客户端用户,特别的Ajax请求一般返回的为Json格式 1.serializers from django.core import seria ...
- Redis一(Redis-py与String操作)
Redis 介绍 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(so ...
- Linux基础整理 + 注释
1.Linux的常用命令: ls 显示当前文件夹目录 ll -->详细信息 ls -a 显示所有文件 ls -lhSr w 查看登录的帐号,还可以查看cpu负载情况,who am i ,who ...
- 什么是web接口
当我们在请求一个页面的时候,会显示服务器返回的资源,其中包含了HTML.CSS和JS,除此之外,服务器还可以返回图片.视频.字体和插件等类型的资源.这些资源全部由HTTP协议传输. 如果把HTTP协议 ...
- 在python列表中删除所有空元素
今天在测试数据的时候偶然发现一个问题,如下: test = ['a','','b','','c','',''] for i in test: if i == '': test.remove(i) pr ...
- 主机名 域名 网站名 URL
举几个域名的例子:google.com,baidu.com,163.com可以明确的告诉你,加上www,就不再是域名了! 以http://mail.163.com/index.html为例进行说明:1 ...
- Unity,android和IOS 防止八门神器注入
八门神器主要是不断筛选,来获取关键属性(比如金币)在内存中的地址,再根据该地址来修改指向的数据就可以成功. 因此,我们需要在金币读取和设置的时候,使用一个偏移量,来达到干扰的目的就可以了 未经仔细测试 ...