Browse Source

ftp/sftp程序移植

wanghc 3 years ago
parent
commit
972ce37b94

+ 234 - 0
ipfcst-console/src/main/java/com/jiayue/ipfcst/ftpsftp/controller/FTPController.java

@@ -0,0 +1,234 @@
+package com.jiayue.ipfcst.ftpsftp.controller;
+
+import com.jiayue.ipfcst.common.core.web.vo.ResponseVO;
+import com.jiayue.ipfcst.ftpsftp.dto.FTPSFTPDto;
+import com.jiayue.ipfcst.ftpsftp.service.ContinueFTPService;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.net.ftp.FTPFile;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.*;
+import java.math.BigDecimal;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.List;
+
+@RestController
+@RequestMapping("FTP")
+@Slf4j
+public class FTPController {
+  private String remoteIp;
+  private String remotePort;
+  private String uploadUserName;
+  private String uploadPassword;
+  @Autowired
+  private final ContinueFTPService continueFTPService;
+
+  public FTPController(ContinueFTPService continueFTPService){
+    this.continueFTPService = continueFTPService;
+  }
+  /**
+   * 连接FTP
+   * @param ftpsftpDtos
+   * @return 返回所有目录
+   */
+  @PostMapping(value = "/continueFTP")
+  public ResponseVO continueFTP(@RequestBody FTPSFTPDto ftpsftpDtos) throws IOException {
+    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+    List<FTPSFTPDto> FTPSFTPDtoList = new ArrayList<>();
+    try {
+      //登陆
+      continueFTPService.initFtpClient(ftpsftpDtos.getRemoteIp(), Integer.parseInt(ftpsftpDtos.getRemotePort()), ftpsftpDtos.getUploadUserName(), ftpsftpDtos.getUploadPassword());
+      //获取目录下所有文件
+      FTPFile[] files =continueFTPService.listFiles(ftpsftpDtos.getServicePath());
+      remoteIp = ftpsftpDtos.getRemoteIp();
+      remotePort = ftpsftpDtos.getRemotePort();
+      uploadUserName = ftpsftpDtos.getUploadUserName();
+      uploadPassword = ftpsftpDtos.getUploadPassword();
+      for (int i = 0; i < files.length; i++) {
+        FTPSFTPDto ftpsftpDto = new FTPSFTPDto();
+        String name = new String(files[i].getName().getBytes("iso-8859-1"),"UTF-8");
+        String s = files[i].getRawListing();
+        String subStrings = s.substring(0,10);
+        BigDecimal fileSize = new BigDecimal(files[i].getSize());
+        String fileSizes = fileSize.divide(new BigDecimal(1024)).divide(new BigDecimal(1024)).setScale(2, BigDecimal.ROUND_CEILING).toString();
+        Long lastTime = files[i].getTimestamp().getTimeInMillis()+files[i].getTimestamp().getTimeZone().getOffset(0);
+
+        ftpsftpDto.setName(name);
+        if(files[i].isFile()){
+          ftpsftpDto.setFileSize(fileSizes+"MB");
+        }else{
+          ftpsftpDto.setFileSize("--");
+        }
+        ftpsftpDto.setLastModified(sdf.format(lastTime));
+        ftpsftpDto.setJurisdiction(subStrings);
+        ftpsftpDto.setDirectory(files[i].isDirectory());
+        FTPSFTPDtoList.add(ftpsftpDto);
+      }
+
+      return ResponseVO.success(FTPSFTPDtoList);
+    }catch (Exception e){
+      return ResponseVO.fail("连接失败,请检查配置信息");
+    }
+
+  }
+
+  /**
+   * 根据传入的路径 返回上一级/进入下一级
+   * @param ftpsftpDtos
+   * @return
+   */
+  @PostMapping(value = "/openServicePath")
+  public ResponseVO openServicePath(@RequestBody FTPSFTPDto ftpsftpDtos){
+    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+    List<FTPSFTPDto> FTPSFTPDtoList = new ArrayList<>();
+    try{
+      FTPFile[] files =continueFTPService.listFiles(ftpsftpDtos.getServicePath());
+      for (int i = 0; i < files.length; i++) {
+        FTPSFTPDto ftpsftpDto = new FTPSFTPDto();
+        String name = new String(files[i].getName().getBytes("iso-8859-1"),"UTF-8");
+
+        String s = files[i].getRawListing();
+        String subStrings = s.substring(0,10);
+        BigDecimal fileSize = new BigDecimal(files[i].getSize());
+        String fileSizes = fileSize.divide(new BigDecimal(1024)).divide(new BigDecimal(1024)).setScale(2, BigDecimal.ROUND_CEILING).toString();
+        Long lastTime = files[i].getTimestamp().getTimeInMillis()+files[i].getTimestamp().getTimeZone().getOffset(0);
+
+        ftpsftpDto.setName(name);
+        if(files[i].isFile()){
+          ftpsftpDto.setFileSize(fileSizes);
+        }else{
+          ftpsftpDto.setFileSize("--");
+        }
+        ftpsftpDto.setLastModified(sdf.format(lastTime));
+        ftpsftpDto.setJurisdiction(subStrings);
+        ftpsftpDto.setDirectory(files[i].isDirectory());
+        FTPSFTPDtoList.add(ftpsftpDto);
+      }
+      return ResponseVO.success(FTPSFTPDtoList);
+    }catch (Exception e){
+      return ResponseVO.fail("进入下一级目录失败");
+    }
+  }
+
+  /**
+   * 下载文件
+   * @param request
+   * @param response
+   * @param path 路径
+   * @param name 文件名
+   * @return
+   */
+  @PostMapping(value = "/downloadFile")
+  public void download(HttpServletRequest request,
+                             HttpServletResponse response,
+                             String path,
+                             String name) throws IOException {
+    response.setContentType("text/html;charset=utf-8");
+    try {
+      request.setCharacterEncoding("UTF-8");
+    } catch (UnsupportedEncodingException e) {
+      log.error("系统错误:" + e.getMessage(), e);
+    }
+    try{
+      InputStream ins = continueFTPService.downLoad(name);
+      BufferedInputStream bins = new BufferedInputStream(ins);
+      OutputStream outs = response.getOutputStream();
+      BufferedOutputStream bouts = new BufferedOutputStream(outs);
+      response.setHeader("Content-disposition", "attachment;filename=" + java.net.URLEncoder.encode(name, "UTF-8"));
+      response.setContentType("application/x-msdownload");
+      int bytesRead = 0;
+      byte[] buffer = new byte[8192];
+      while ((bytesRead = bins.read(buffer, 0, 8192)) != -1) {
+        bouts.write(buffer, 0, bytesRead);
+      }
+      bouts.flush();
+      ins.close();
+      bins.close();
+      bouts.close();
+      flush(remoteIp,remotePort, uploadUserName, uploadPassword);
+    }catch (Exception e){
+      log.error("系统错误:" + e.getMessage(), e);
+    }
+
+  }
+
+  @PostMapping(value = "/upload")
+  public ResponseVO upload(@RequestParam("path") String path,@RequestParam("file") MultipartFile file){
+    try{
+      continueFTPService.upload(path,file.getOriginalFilename(),file.getInputStream());
+      FTPSFTPDto ftpsftpDto = new FTPSFTPDto();
+      ftpsftpDto.setUploadPassword(uploadPassword);
+      ftpsftpDto.setUploadUserName(uploadUserName);
+      ftpsftpDto.setRemotePort(remoteIp);
+      ftpsftpDto.setRemoteIp(remoteIp);
+      ftpsftpDto.setServicePath(path);
+      return openServicePath(ftpsftpDto);
+    }catch (Exception e){
+      return ResponseVO.fail("上传文件失败"+e);
+    }
+  }
+
+  /**
+   * 关闭连接
+   * @return
+   * @throws IOException
+   */
+  @GetMapping(value = "/closeContinue")
+  public ResponseVO closeContinue() throws IOException {
+    try {
+      continueFTPService.close();
+      return ResponseVO.success("连接已关闭");
+    }catch (Exception e){
+      return ResponseVO.fail("无法关闭连接");
+    }
+
+  }
+
+  /**
+   * 删除文件
+   * @param name 文件名称
+   * @param path 服务器路径
+   * @return
+   */
+  @PostMapping(value = "/deleteFile")
+  public ResponseVO deleteFile(@RequestParam("name") String name,@RequestParam("path") String path){
+    try{
+      Boolean flag = false;
+      if(path.equals("/")){
+       flag = continueFTPService.delete(path,name);
+      }else{
+       flag = continueFTPService.delete(path+File.separator,name);
+      }
+      if(!flag){
+        return  ResponseVO.fail("删除文件失败");
+      }
+      FTPSFTPDto ftpsftpDto = new FTPSFTPDto();
+      ftpsftpDto.setUploadPassword(uploadPassword);
+      ftpsftpDto.setUploadUserName(uploadUserName);
+      ftpsftpDto.setRemotePort(remoteIp);
+      ftpsftpDto.setRemoteIp(remoteIp);
+      ftpsftpDto.setServicePath(path);
+      return openServicePath(ftpsftpDto);
+    }catch (Exception e){
+      return ResponseVO.fail("删除文件失败"+e);
+    }
+
+  }
+  /**
+   * 刷新连接
+   * @param host ip地址
+   * @param port 端口
+   * @param userName 用户名
+   * @param password 密码
+   * @throws IOException
+   */
+  public void flush(String host, String port, String userName, String password) throws IOException {
+    continueFTPService.close();
+    continueFTPService.initFtpClient(host,Integer.parseInt(port),userName,password);
+  }
+}

+ 231 - 0
ipfcst-console/src/main/java/com/jiayue/ipfcst/ftpsftp/controller/SFTPController.java

@@ -0,0 +1,231 @@
+package com.jiayue.ipfcst.ftpsftp.controller;
+
+import com.jcraft.jsch.ChannelSftp;
+import com.jcraft.jsch.SftpATTRS;
+import com.jiayue.ipfcst.common.core.web.vo.ResponseVO;
+import com.jiayue.ipfcst.ftpsftp.dto.FTPSFTPDto;
+import com.jiayue.ipfcst.ftpsftp.service.ContinueSFTPService;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.*;
+import java.math.BigDecimal;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.List;
+
+@RestController
+@RequestMapping("SFTP")
+@Slf4j
+public class SFTPController {
+  private String remoteIp;
+  private String remotePort;
+  private String uploadUserName;
+  private String uploadPassword;
+  @Autowired
+  private final ContinueSFTPService continueSFTPService;
+
+  public SFTPController(ContinueSFTPService continueSFTPService){
+    this.continueSFTPService = continueSFTPService;
+  }
+  /**
+   * 连接SFTP
+   * @param ftpsftpDtos
+   * @return 返回所有目录
+   */
+  @PostMapping(value = "/continueSFTP")
+  public ResponseVO continueSFTP(@RequestBody FTPSFTPDto ftpsftpDtos) throws IOException {
+    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+    List<FTPSFTPDto> FTPSFTPDtoList = new ArrayList<>();
+    try {
+      //登陆
+      continueSFTPService.login(ftpsftpDtos.getUploadUserName(), ftpsftpDtos.getUploadPassword(), ftpsftpDtos.getRemoteIp(),Integer.parseInt(ftpsftpDtos.getRemotePort()),"");
+      //获取目录下所有文件
+      ArrayList<ChannelSftp.LsEntry> ftpFileArr= continueSFTPService.listFiles(ftpsftpDtos.getServicePath());
+      remoteIp = ftpsftpDtos.getRemoteIp();
+      remotePort = ftpsftpDtos.getRemotePort();
+      uploadUserName = ftpsftpDtos.getUploadUserName();
+      uploadPassword = ftpsftpDtos.getUploadPassword();
+      for (int i = 0; i < ftpFileArr.size(); i++) {
+        FTPSFTPDto ftpsftpDto = new FTPSFTPDto();
+        String name = ftpFileArr.get(i).getFilename();
+        SftpATTRS attrs = ftpFileArr.get(i).getAttrs();
+        BigDecimal fileSize = new BigDecimal(attrs.getSize());
+        String fileSizes = fileSize.divide(new BigDecimal(1024)).divide(new BigDecimal(1024)).setScale(2, BigDecimal.ROUND_CEILING).toString();
+        ftpsftpDto.setName(name);
+        ftpsftpDto.setJurisdiction(attrs.getPermissionsString());
+        if(attrs.isDir()){
+          ftpsftpDto.setFileSize("--");
+        }else{
+          ftpsftpDto.setFileSize(fileSizes+"MB");
+        }
+        Long time = attrs.getMTime()*1000l;
+        ftpsftpDto.setLastModified(sdf.format(time));
+        ftpsftpDto.setDirectory(attrs.isDir());
+        FTPSFTPDtoList.add(ftpsftpDto);
+      }
+
+      return ResponseVO.success(FTPSFTPDtoList);
+    }catch (Exception e){
+      return ResponseVO.fail("连接失败,请检查配置信息");
+    }
+
+  }
+
+  /**
+   * 根据传入的路径 返回上一级/进入下一级
+   * @param ftpsftpDtos
+   * @return
+   */
+  @PostMapping(value = "/openServicePath")
+  public ResponseVO openServicePath(@RequestBody FTPSFTPDto ftpsftpDtos){
+    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+    List<FTPSFTPDto> FTPSFTPDtoList = new ArrayList<>();
+    try{
+      ArrayList<ChannelSftp.LsEntry> ftpFileArr =continueSFTPService.listFiles(ftpsftpDtos.getServicePath());
+      for (int i = 0; i < ftpFileArr.size(); i++) {
+        FTPSFTPDto ftpsftpDto = new FTPSFTPDto();
+        String name = ftpFileArr.get(i).getFilename();
+        SftpATTRS attrs = ftpFileArr.get(i).getAttrs();
+        BigDecimal fileSize = new BigDecimal(attrs.getSize());
+        String fileSizes = fileSize.divide(new BigDecimal(1024)).divide(new BigDecimal(1024)).setScale(2, BigDecimal.ROUND_CEILING).toString();
+        ftpsftpDto.setName(name);
+        ftpsftpDto.setJurisdiction(attrs.getPermissionsString());
+        if(attrs.isDir()){
+          ftpsftpDto.setFileSize("--");
+        }else{
+          ftpsftpDto.setFileSize(fileSizes+"MB");
+        }
+        Long time = attrs.getMTime()*1000l;
+        ftpsftpDto.setLastModified(sdf.format(time));
+        ftpsftpDto.setDirectory(attrs.isDir());
+        FTPSFTPDtoList.add(ftpsftpDto);
+      }
+      return ResponseVO.success(FTPSFTPDtoList);
+    }catch (Exception e){
+      return ResponseVO.fail("进入下一级目录失败");
+    }
+  }
+
+  /**
+   * 下载文件
+   * @param request
+   * @param response
+   * @param path 路径
+   * @param name 文件名
+   * @return
+   */
+  @PostMapping(value = "/downloadFile")
+  public void download(HttpServletRequest request,
+                       HttpServletResponse response,
+                       String path,
+                       String name) throws IOException {
+    response.setContentType("text/html;charset=utf-8");
+    try {
+      request.setCharacterEncoding("UTF-8");
+    } catch (UnsupportedEncodingException e) {
+      log.error("系统错误:" + e.getMessage(), e);
+    }
+    try{
+      InputStream ins = continueSFTPService.download(path,name);
+
+      BufferedInputStream bins = new BufferedInputStream(ins);
+      OutputStream outs = response.getOutputStream();
+      BufferedOutputStream bouts = new BufferedOutputStream(outs);
+      response.setHeader("Content-disposition", "attachment;filename=" + java.net.URLEncoder.encode(name, "UTF-8"));
+      response.setContentType("application/x-msdownload");
+      int bytesRead = 0;
+      byte[] buffer = new byte[8192];
+      while ((bytesRead = bins.read(buffer, 0, 8192)) != -1) {
+        bouts.write(buffer, 0, bytesRead);
+      }
+      bouts.flush();
+      ins.close();
+      bins.close();
+      bouts.close();
+      flush(remoteIp,remotePort, uploadUserName, uploadPassword);
+    }catch (Exception e){
+      log.error("系统错误:" + e.getMessage(), e);
+    }
+
+  }
+
+  @PostMapping(value = "/upload")
+  public ResponseVO upload(@RequestParam("path") String path,@RequestParam("file") MultipartFile file){
+    try{
+      continueSFTPService.upload(path,file.getOriginalFilename(),file.getInputStream());
+      FTPSFTPDto ftpsftpDto = new FTPSFTPDto();
+      ftpsftpDto.setUploadPassword(uploadPassword);
+      ftpsftpDto.setUploadUserName(uploadUserName);
+      ftpsftpDto.setRemotePort(remoteIp);
+      ftpsftpDto.setRemoteIp(remoteIp);
+      ftpsftpDto.setServicePath(path);
+      return openServicePath(ftpsftpDto);
+    }catch (Exception e){
+      return ResponseVO.fail("上传文件失败"+e);
+    }
+  }
+
+  /**
+   * 关闭连接
+   * @return
+   * @throws IOException
+   */
+  @GetMapping(value = "/closeContinue")
+  public ResponseVO closeContinue() throws IOException {
+    try {
+      continueSFTPService.logout();
+      return ResponseVO.success("连接已关闭");
+    }catch (Exception e){
+      return ResponseVO.fail("无法关闭连接");
+    }
+
+  }
+
+  /**
+   * 删除文件
+   * @param name 文件名称
+   * @param path 服务器路径
+   * @return
+   */
+  @PostMapping(value = "/deleteFile")
+  public ResponseVO deleteFile(@RequestParam("name") String name,@RequestParam("path") String path){
+    try{
+      Boolean flag = false;
+      if(path.equals("/")){
+        flag = continueSFTPService.delete(path,name);
+      }else{
+        flag = continueSFTPService.delete(path+File.separator,name);
+      }
+      if(!flag){
+        return  ResponseVO.fail("删除文件失败");
+      }
+      FTPSFTPDto ftpsftpDto = new FTPSFTPDto();
+      ftpsftpDto.setUploadPassword(uploadPassword);
+      ftpsftpDto.setUploadUserName(uploadUserName);
+      ftpsftpDto.setRemotePort(remoteIp);
+      ftpsftpDto.setRemoteIp(remoteIp);
+      ftpsftpDto.setServicePath(path);
+      return openServicePath(ftpsftpDto);
+    }catch (Exception e){
+      return ResponseVO.fail("删除文件失败"+e);
+    }
+
+  }
+  /**
+   * 刷新连接
+   * @param host ip地址
+   * @param port 端口
+   * @param userName 用户名
+   * @param password 密码
+   * @throws IOException
+   */
+  public void flush(String host, String port, String userName, String password) throws IOException {
+    continueSFTPService.logout();
+    continueSFTPService.login(userName,password,host,Integer.parseInt(port),"");
+  }
+}

+ 112 - 0
ipfcst-console/src/main/java/com/jiayue/ipfcst/ftpsftp/dto/FTPSFTPDto.java

@@ -0,0 +1,112 @@
+package com.jiayue.ipfcst.ftpsftp.dto;
+/**
+ * FTPSFTP DTO
+ *
+ * @author whc
+ * @version 2.0
+ * @since 2021/4/13 15:36
+ */
+public class FTPSFTPDto {
+    // 名称
+    private String name;
+    // 大小
+    private String fileSize;
+    // 最后修改日期
+    private String lastModified;
+    // 权限
+    private String jurisdiction;
+    // 是否是文件夹
+    private Boolean directory;
+    // 对方路径
+    private String servicePath;
+    // ip
+    private String remoteIp;
+    // 端口
+    private String remotePort;
+    // 用户名
+    private String uploadUserName;
+    // 密码
+    private String uploadPassword;
+  public String getRemoteIp() {
+    return remoteIp;
+  }
+
+  public void setRemoteIp(String remoteIp) {
+    this.remoteIp = remoteIp;
+  }
+
+  public String getRemotePort() {
+    return remotePort;
+  }
+
+  public void setRemotePort(String remotePort) {
+    this.remotePort = remotePort;
+  }
+
+  public String getUploadUserName() {
+    return uploadUserName;
+  }
+
+  public void setUploadUserName(String uploadUserName) {
+    this.uploadUserName = uploadUserName;
+  }
+
+  public String getUploadPassword() {
+    return uploadPassword;
+  }
+
+  public void setUploadPassword(String uploadPassword) {
+    this.uploadPassword = uploadPassword;
+  }
+
+
+  public String getServicePath() {
+    return servicePath;
+  }
+
+  public void setServicePath(String servicePath) {
+    this.servicePath = servicePath;
+  }
+
+
+
+  public Boolean getDirectory() {
+    return directory;
+  }
+
+  public void setDirectory(Boolean directory) {
+    this.directory = directory;
+  }
+
+  public String getName() {
+    return name;
+  }
+
+  public void setName(String name) {
+    this.name = name;
+  }
+
+  public String getFileSize() {
+    return fileSize;
+  }
+
+  public void setFileSize(String fileSize) {
+    this.fileSize = fileSize;
+  }
+
+  public String getLastModified() {
+    return lastModified;
+  }
+
+  public void setLastModified(String lastModified) {
+    this.lastModified = lastModified;
+  }
+
+  public String getJurisdiction() {
+    return jurisdiction;
+  }
+
+  public void setJurisdiction(String jurisdiction) {
+    this.jurisdiction = jurisdiction;
+  }
+}

+ 194 - 0
ipfcst-console/src/main/java/com/jiayue/ipfcst/ftpsftp/service/ContinueFTPService.java

@@ -0,0 +1,194 @@
+package com.jiayue.ipfcst.ftpsftp.service;
+
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.net.ftp.FTPClient;
+import org.apache.commons.net.ftp.FTPFile;
+import org.springframework.stereotype.Service;
+
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
+
+@Service
+@Slf4j
+public class ContinueFTPService {
+  public FTPClient client;
+
+  /*public ContinueFTPService(String host,int port ,String username,  String password) throws IOException {
+    initFtpClient(host, port, username, password);
+  }
+  public ContinueFTPService(String host, String userName, String password)
+    throws SocketException, IOException {
+    initFtpClient(host, 21, userName, password);
+  }*/
+
+  /**
+   * 登录
+   * @param host
+   * @param port
+   * @param userName
+   * @param password
+   * @throws IOException
+   */
+  public void initFtpClient(String host, int port, String userName,
+                            String password) throws IOException {
+
+    client =new FTPClient();//s生成一个新的client
+    client.connect(host,port);//链接
+    client.login(userName,password);//通过用户名和密码登录
+
+  }
+
+  /**
+   * 得到所有目录
+   * @param remotepath
+   * @return
+   */
+  public FTPFile[] listFiles(String remotepath) throws IOException {
+    client.enterLocalPassiveMode();
+    remotepath = new String(remotepath.getBytes(StandardCharsets.UTF_8),"iso-8859-1");
+    if (client == null){
+      return null;
+    }
+    client.changeWorkingDirectory(remotepath);//获取所有的目录
+    return client.listFiles();//返回获取到的目录信息
+  }
+
+  /**
+   * 上传
+   * @param remotepath(ftp的路径)
+   * @return上传是否成功
+   * @throws FileNotFoundException
+   */
+  public boolean upload(String remotepath,String filename,InputStream inputStream) throws IOException {
+    if (null == client){//如果获取到的信息为空则返回false
+      return false;
+    }
+    boolean res = false;
+    client.setFileType(FTPClient.BINARY_FILE_TYPE);//设置上传文件的类型
+    client.changeWorkingDirectory(remotepath);//获取上传的路径地址
+    String charSet = System.getProperties().get("file.encoding").toString();
+    String name = new String(filename.getBytes(charSet),"ISO-8859-1");
+    res=client.storeFile(name,inputStream);
+    return res;
+  }
+
+  /**
+   * 下载
+   * @param name 文件名称
+   * @return 下载是否成功
+   */
+  public InputStream downLoad(String name) throws IOException {
+    InputStream ins = null;
+
+    try {
+      client.setFileType(FTPClient.BINARY_FILE_TYPE);//设置下载文件的类型
+      //获取系统字符集
+      String charSet = System.getProperties().get("file.encoding").toString();
+      String fileName = new String(name.getBytes(charSet),"ISO-8859-1");
+      ins = client.retrieveFileStream(fileName);
+    }catch (Exception e){
+      log.error("FTP下载错误");
+    }
+    return ins;
+  }
+
+  /**
+   * 删除文件
+   * @param remotepath ftp服务端路径
+   * @return
+   */
+  public boolean delete(String remotepath,String name) throws IOException {
+    client.changeWorkingDirectory(remotepath);
+    String charSet = System.getProperties().get("file.encoding").toString();
+    String fileName = new String(name.getBytes(charSet),"ISO-8859-1");
+    return  client.deleteFile(remotepath+fileName);//删除文件是否成功
+  }
+
+  /**
+   * 创建目录
+   * @param remotepath ftp服务端路径
+   * @return
+   */
+  public boolean makeDirectory(String remotepath) throws IOException {
+    boolean res = false;
+
+    if (null == client){
+      return res;
+    }
+
+    String[] item = remotepath.split("/");//以‘/’分割成字符串数组
+    String currentPath="";
+    for (int i = 0; i <item.length-1 ; i++) {
+
+      currentPath = currentPath +"/"+item[i];//创建目录
+      client.makeDirectory(currentPath);
+    }
+    return client.makeDirectory(remotepath);
+  }
+
+  /**
+   * 删除文件
+   * @param remotepath ftp端路径
+   * @return
+   */
+  private boolean deleteDirectory(String remotepath) throws IOException {
+    boolean res= false;
+    FTPFile[] files= listFiles(remotepath);//获取文件数组
+    for (int i = 0; i <files.length ; i++) {
+      if (files[i].isDirectory()){ //如果是删除目录
+        deleteDirectory(remotepath+"/"+files[i].getName());//删除目录
+      }else {
+        client.deleteFile(remotepath+"/"+files[i].getName());//删除文件
+      }
+
+    }
+    return client.removeDirectory(remotepath);
+  }
+
+  /**
+   * 重命名
+   * @param remoteOldPath ftp旧名字文件
+   * @param remoteNewPath ftp新名字文件
+   * @return 是否修改名字成功
+   */
+  public boolean replaceName(String remoteOldPath,String remoteNewPath) throws IOException {
+
+    if (null == client){
+      return false;
+    }
+    return client.rename(remoteOldPath,remoteNewPath);
+  }
+
+  /**
+   * 退出登录
+   * @throws IOException
+   */
+  public void close() throws IOException {
+    if (null != client)
+      client.logout();
+  }
+
+  public static void main (String[] args) {
+    try {
+
+      //ContinueFTPService ft = new ContinueFTPService("43.254.3.182", 21, "syjy", "^YHN7ujm");
+      //   System.out.print( ft.upload("D:\\7za.exe","/ftp"));
+      System.out.println("连接成功");
+      //FTPFile[] files = ft.listFiles("/");
+
+      //System.out.println(ft.upload("/Users/wanghongchen/Desktop/test.txt","/"));
+      /*for (int i = 0; i < files.length; i++) {
+        String name = new String(files[i].getName().getBytes("iso-8859-1"),"UTF-8");
+        System.out.println("内容有:" + name);
+      }*/
+      //ft.close();
+    }catch (Exception e){
+      System.out.println("连接失败"+e);
+    }
+    //System.out.println("删除文件:"+ft.delete("222.png"));
+  }
+
+
+}

+ 296 - 0
ipfcst-console/src/main/java/com/jiayue/ipfcst/ftpsftp/service/ContinueSFTPService.java

@@ -0,0 +1,296 @@
+package com.jiayue.ipfcst.ftpsftp.service;
+
+import com.jcraft.jsch.*;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Service;
+
+import java.io.*;
+import java.util.ArrayList;
+import java.util.Properties;
+import java.util.Vector;
+
+@Service
+@Slf4j
+public class ContinueSFTPService {
+  private String Type = "sftp";
+
+  private ChannelSftp sftp;
+  private Session session;
+  private String username;
+  private String password;
+  private String privateKey;
+  private String host;
+  private int port;
+
+
+/*  public  ContinueSFTPService(String username, String password, String host, int port) throws JSchException {//port=22
+     this.username = username;
+     this.password = password;
+     this.privateKey = "";
+     this.host = host;
+     this.port = port;
+     login(this.username,this.password,this.host,this.port,"");
+  }*/
+
+
+ /* *
+  public ContinueSFTPService(String username, String host, int port, String privateKey) {
+
+    this.username = username;
+
+    this.host = host;
+
+    this.port = port;
+
+    this.privateKey = privateKey;
+
+  }*/
+
+
+
+
+  /**
+
+   * 连接sftp服务器
+
+   */
+
+  public void login(String username,String password,String host,int port,String privateKey){
+
+    try {
+
+      JSch jsch = new JSch();
+
+
+      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(Type);
+
+      channel.connect();
+
+      this.sftp = (ChannelSftp) channel;
+
+    } catch (JSchException e) {
+
+      e.printStackTrace();
+
+    }
+
+  }
+
+  /**
+
+   * 关闭连接 server
+
+   */
+
+  public void logout(){
+
+    if (sftp != null) {
+
+      if (sftp.isConnected()) {
+
+        sftp.disconnect();
+
+      }
+
+    }
+
+    if (session != null) {
+
+      if (session.isConnected()) {
+
+        session.disconnect();
+
+      }
+
+    }
+
+  }
+
+  /**
+
+   * 将输入流的数据上传到sftp作为文件。
+
+   * @param directory  上传到该目录
+
+   * @param sftpFileName  sftp端文件名
+
+   * @param input   输入流
+
+   */
+
+  public void upload(String directory, String sftpFileName, InputStream input) throws SftpException{
+
+    try{
+      sftp.cd(directory);
+      sftp.put(input, sftpFileName);  //上传文件
+    }catch (Exception e){
+      log.error("文件上传失败");
+    }
+  }
+
+
+
+  /**
+
+   * 下载文件。
+
+   * @param directory 下载目录
+
+   * @param downloadFile 下载的文件
+
+   * @param saveFile 存在本地的路径
+
+   */
+
+  public void download(String directory, String downloadFile, String saveFile) throws SftpException, FileNotFoundException{
+
+    if (directory != null && !"".equals(directory)) {
+
+      sftp.cd(directory);
+
+    }
+
+    File file = new File(saveFile);
+
+    sftp.get(downloadFile, new FileOutputStream(file));
+
+  }
+
+  /**
+
+   * 下载文件
+
+   * @param directory 下载目录
+
+   * @param downloadFile 下载的文件名
+
+   * @return 字节数组
+
+   */
+
+  public InputStream download(String directory, String downloadFile) throws SftpException, IOException{
+
+    if (directory != null && !"".equals(directory)) {
+
+      sftp.cd(directory);
+
+    }
+
+    InputStream is = sftp.get(downloadFile);
+
+
+
+    //byte[] fileData = IOUtils.toByteArray(is);
+
+
+
+    //return fileData;
+    return is;
+  }
+
+  /**
+
+   * 删除文件
+
+   * @param directory 要删除文件所在目录
+
+   * @param deleteFile 要删除的文件
+
+   */
+
+  public Boolean delete(String directory, String deleteFile){
+
+    try {
+      sftp.cd(directory);
+
+      sftp.rm(deleteFile);
+
+    }catch (Exception e){
+      e.printStackTrace();
+      return false;
+    }
+
+    return true;
+  }
+
+  /**
+
+   * 列出目录下的文件
+   * @param directory 要列出的目录
+   * @return
+
+
+   */
+
+  public ArrayList<ChannelSftp.LsEntry> listFiles(String directory) throws SftpException {
+    ArrayList<ChannelSftp.LsEntry> files = new ArrayList<>();
+    sftp.cd(directory);
+    Vector lss = sftp.ls("*");
+    for (int i = 0; i < lss.size(); i++)
+    {
+      Object obj = lss.elementAt(i);
+      if (obj instanceof ChannelSftp.LsEntry)
+      {
+        ChannelSftp.LsEntry entry = (ChannelSftp.LsEntry) obj;
+        if (true && !entry.getAttrs().isDir())
+        {
+          files.add(entry);
+        }
+        if (true && entry.getAttrs().isDir())
+        {
+          if (!entry.getFilename().equals(".") && !entry.getFilename().equals(".."))
+          {
+            files.add(entry);
+          }
+        }
+      }
+    }
+    return files;
+
+  }
+  public static void main (String[] args) {
+    try {
+      //String username, String password, String host, int port
+      /*ContinueSFTPService continueSFTPService=new ContinueSFTPService("root","Jydl*3377","192.168.9.103",22);
+
+      //login("root","Jydl*3377","192.168.9.103",22);
+      System.out.println("连接成功");
+      ArrayList<ChannelSftp.LsEntry> ftpFileArr = continueSFTPService.listFiles(File.separator+"usr");
+      for (int ii = 0; ii < ftpFileArr.size(); ii++) {
+        System.out.println("name == "+ ftpFileArr.get(ii).getFilename());
+        System.out.println("attrs"+ftpFileArr.get(ii).getAttrs());
+        System.out.println("Longname"+ftpFileArr.get(ii).getLongname());
+      }
+      continueSFTPService.logout();
+      System.out.println("连接已断开");*/
+    }catch (Exception e){
+      System.out.println("连接失败"+e);
+    }
+
+  }
+
+
+
+}