文件上传(二) 搭建 xftp 服务器,并实现上传工具类
程序员文章站
2022-05-20 09:29:49
...
package com.taotao.common.utils;
import com.jcraft.jsch.*;
import com.taotao.common.pojo.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileInputStream;
import java.lang.reflect.Array;
import java.util.*;
public class FtpUtils {
private static final Logger LOG = LoggerFactory.getLogger(FtpUtils.class);
private ChannelSftp sftp;
private Session session;
private Channel channel;
private void init() {
String host = "xxxx";
int port = 22;
String username = "xxx";
String passwd = "xxx";
// 若文件存在
session = getSession(host, port, username, passwd);
channel = getChannel(session);
sftp = (ChannelSftp) channel;
}
private void close() {
closeAll(); //关闭连接
}
/**
* upload
* @param fileName 上传的文件 C:/img/0.jpg
* @return
*/
public R upload(String fileName) {
File file = new File(fileName);
if ( !file.exists()) {
return R.error("上传的文件不存在!");
}
String uploadMapping = "/home/ftpuser/img/";
init();
String upload = uploadFile(uploadMapping,file);
close(); //关闭连接
// 构建返回参数
Map<String, String> data = new HashMap<String, String>();
data.put("url", uploadMapping+file.getName());
return R.success(upload, data);
}
/**
* 下载文件
*
* @param directory
* 下载目录
* @param downloadFile
* 下载的文件
* @param saveFile
* 存在本地的路径
*/
public R download(String directory, String downloadFile,
String saveFile) {
init();
try {
sftp.cd(directory);
sftp.get(downloadFile, saveFile);
return R.success("下载成功!");
} catch (Exception e) {
System.out.println(e);
return R.success("下载失败!");
} finally {
close();
}
}
public Channel getChannel(Session session) {
Channel channel = null;
try {
channel = session.openChannel("sftp");
channel.connect();
LOG.info("get Channel success!");
} catch (JSchException e) {
LOG.info("get Channel fail!", e);
}
return channel;
}
public Session getSession(String host, int port, String username,
final String password) {
Session session = null;
try {
JSch jsch = new JSch();
jsch.getSession(username, host, port);
session = jsch.getSession(username, host, port);
session.setPassword(password);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
session.setConfig(sshConfig);
session.connect();
LOG.info("Session connected!");
} catch (JSchException e) {
LOG.info("get Channel failed!", e);
}
return session;
}
/**
* @param
* @param remoteDir
* 上传目录
* @param file
* 上传文件
* @return
*/
public String uploadFile(String remoteDir, File file) {
String result = "";
try {
String[] folders = remoteDir.split( "/" );
ArrayList<String> lists = new ArrayList<>();
for (String folder : folders) {
lists.add(folder);
}
try {
sftp.cd( "/" );
sftp.cd( remoteDir );
}
catch ( SftpException e ) {
System.out.println("error!!!!! e = " + e);
// 若进入失败,则调用安全进入目录的方法,此时目录没有则会被创建
safeCDRemoteDir("", 0, lists);
}
if (file != null) {
sftp.put(new FileInputStream(file), file.getName());
result = "上传成功!";
} else {
result = "文件为空!不能上传!";
}
} catch (Exception e) {
LOG.info("上传失败!", e);
result = "上传失败!";
}
return result;
}
/**
* 删除文件
*
* @param directory
* 要删除文件所在目录
* @param deleteFile
* 要删除的文件
* @param sftp
*/
public String delete(String directory, String deleteFile, ChannelSftp sftp) {
String result = "";
try {
sftp.cd(directory);
sftp.rm(deleteFile);
result = "删除成功!";
} catch (Exception e) {
result = "删除失败!";
LOG.info("删除失败!", e);
}
return result;
}
private void closeChannel(Channel channel) {
if (channel != null) {
if (channel.isConnected()) {
channel.disconnect();
}
}
}
private void closeSession(Session session) {
if (session != null) {
if (session.isConnected()) {
session.disconnect();
}
}
}
public void closeAll() {
try {
closeChannel(sftp);
closeChannel(channel);
closeSession(session);
} catch (Exception e) {
LOG.info("closeAll", e);
}
}
public int safeCDRemoteDir(String currPath, int index, ArrayList<String> pathList) throws SftpException {
int size = pathList.size();
for (int i=index; i < size; i++) {
System.out.println(pathList.get(i));
if (pathList.get(i).length() > 0) {
try {
// 逐层进入
sftp.cd( pathList.get(i) );
// 若成功,则更改当前路径
currPath += "/" + pathList.get(i);
}
catch ( SftpException e ) {
// 创建对应的目录
sftp.mkdir( currPath + "/" + pathList.get(i));
System.out.println(pathList.toString());
return safeCDRemoteDir(currPath,i,pathList);
}
}
}
return 1;
}
}
上一篇: Xftp更改权限问题
下一篇: 7-24 树种统计