java 远程上传、下载文件
1.maven依赖
<dependencies>
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.55</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
</dependency>
</dependencies>
2.工具类
import com.jcraft.jsch.*;
import org.apache.commons.lang3.StringUtils;
import java.io.*;
import java.util.Properties;
/**
* @author 黔程似景
* @description 两台服务器之间的文件上传于下载工具类
* @date 2022/6/18 9:27
* <p>
* File 类
* public boolean exists() true:文件夹或者文件存在 ,false:文件夹或者文件不存在
* public boolean isFile() true:表示为文件 ,false:表示不是文件
* public boolean isDirectory() true: 表示为文件夹 ,false:表示不是文件夹
**/
public class FileUtil {
/**
* linux服务器的ip
*/
private String host;
/**
* 端口号:一般为22
*/
private int port;
/**
* 私钥:可以不存在
*/
private String privateKey;
/**
* 登录名称
*/
private String username;
/**
* 登录密码
*/
private String password;
/**
* 文件传输通道
*/
private ChannelSftp sftp;
public FileUtil(String username, String password, String host, int port) {
this.username = username;
this.password = password;
this.host = host;
this.port = port;
setChannel();
}
public FileUtil(String username, String host, int port, String privateKey) {
this.username = username;
this.host = host;
this.port = port;
this.privateKey = privateKey;
setChannel();
}
/**
* 检查并设置传输文件的通道
*/
private void setChannel() {
try {
JSch jSch = new JSch();
if (privateKey != null) {
jSch.addIdentity(privateKey);
}
Session session = jSch.getSession(username, host, port);
if (password != null) {
session.setPassword(password);
}
session.setTimeout(100000);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
this.sftp = (ChannelSftp) channel;
System.out.println("登录成功!");
} catch (JSchException e) {
System.out.println("登录失败!");
e.printStackTrace();
}
}
/**
* 本地文件上传到linux系统
* 需要保存到sftp系统上面的文件路径,文件名称,文件流,流操作结束后,需要自己进行关闭输入流(流不是方法本身创建的,则不能够在方法内部关闭),避免出现问题
*
* @param savePath 保存文件的路径
* @param sftpFileName 保存的文件名称
* @param inputStream 本地文件输入流
*/
public void upload(String savePath, String sftpFileName, InputStream inputStream) throws SftpException {
//进入到需要保存文件目录
sftp.cd(savePath);
sftp.put(inputStream, sftpFileName);
System.out.println("上传成功");
}
/**
* 适用于:从linux文件中拷贝文件到本地系统中,自己进行创建流
*
* @param uploadPath 需要拷贝本地文件的全路径
* @param saveDirectory 保存文件到服务器目录的路径
*/
public void upload(String uploadPath, String saveDirectory) throws Exception {
//判定文件是否存在,不存在或者不是文件抛出异常
File file = new File(uploadPath);
if (!file.exists() || !file.isFile()) {
throw new Exception(uploadPath + ":上传路径不存在,或者不是文件");
}
String fileName = file.getName();
InputStream is = new FileInputStream(file);
//进入到上传保存目录
sftp.cd(saveDirectory);
//进行io操作
sftp.put(is, fileName);
//关闭io流
is.close();
System.out.println("上传成功");
}
/**
* 从服务器下载文件到本地
*
* @param directory 下载的文件绝对路径 例如:/root/images/1.txt
* @param downloadFile 下载的文件名
* @param saveFileDirectory 保存的文件路径
*/
public void download(String directory, String downloadFile, String saveFileDirectory) throws SftpException, IOException {
String saveFile = saveFileDirectory + "\\" + downloadFile;
if (StringUtils.isNotBlank(directory)) {
downloadFile = directory + "/" + downloadFile;
}
File file = new File(saveFile);
FileOutputStream stream = new FileOutputStream(file);
sftp.get(downloadFile, stream);
stream.close();
System.out.println("下载成功");
}
/**
* 从服务器下载文件到本地目录
*
* @param filePath 下载文件的绝对路径 例如: /root/images/a.jpg
* @param saveDirectory 保存文件的绝对路径 例如:D:/images
*/
public void download(String filePath, String saveDirectory) throws Exception {
//判断下载的是否是文件
File downloadFile = new File(filePath);
if (!downloadFile.exists() && !downloadFile.isFile()) {
System.out.println(filePath + ":路径不存在或者不是一个下载文件!");
throw new Exception(filePath + ":路径不存在或者不是一个下载文件路径");
}
//判断保存地址是否为路径
File savePath = new File(saveDirectory);
if (!savePath.exists() && !savePath.isDirectory()) {
System.out.println(saveDirectory + ":路径不存在或者不是一个保存地址");
throw new Exception(saveDirectory + ":路径不存在或者不是一个保存地址");
}
//进行文件下载
String saveFile = saveDirectory + "\\" + downloadFile.getName();
savePath = new File(saveFile);
FileOutputStream stream = new FileOutputStream(savePath);
sftp.get(filePath, stream);
//关闭io流
stream.close();
System.out.println("下载成功");
}
/**
* 测试上传和下载
*/
public static void main(String[] args) throws IOException, SftpException {
FileUtil fiel = new FileUtil("root", "root", "192.168.153.128", 22);
//测试上传功能
File file = new File("D:\\images\\owl.png");
InputStream is = new FileInputStream(file);
fiel.upload("/root/images", "owl.png", is);
is.close();
//测试下载功能
fiel.download("/root/images", "owl.png", "D:\\image");
}
}