chendayu 2 years ago
parent
commit
b8f0d337e9

+ 1 - 1
src/main/java/com/example/fms/service/FileFactoryService.java

@@ -108,7 +108,7 @@ public class FileFactoryService {
     public byte[] downloadFromFSS(DownloadSysFileDTO downloadSysFileDTO, ConfigSettingVO configSettingVO) throws Exception {
         //下载目录,也是存储路径
         String filePath = downloadSysFileDTO.getFilePath();
-        String directory = filePath.substring(0, filePath.lastIndexOf("\\"));
+        String directory = filePath.substring(0, filePath.lastIndexOf("/"));
         //下载的文件,也就是文件名
         String downloadFile = downloadSysFileDTO.getFileName();
         //存在本地的路径,随机生成

+ 21 - 17
src/main/java/com/example/fms/service/SftpService.java

@@ -98,17 +98,17 @@ public class SftpService {
      * @param sftp
      * @throws Exception
      */
-    public static void disConn(Session session,Channel channel,ChannelSftp sftp)throws Exception{
-        if(null != sftp){
+    public static void disConn(Session session, Channel channel, ChannelSftp sftp) throws Exception {
+        if (null != sftp) {
             sftp.disconnect();
             sftp.exit();
             sftp = null;
         }
-        if(null != channel){
+        if (null != channel) {
             channel.disconnect();
             channel = null;
         }
-        if(null != session){
+        if (null != session) {
             session.disconnect();
             session = null;
         }
@@ -116,9 +116,10 @@ public class SftpService {
 
     /**
      * 上传文件
-     * @param directory 上传的目录-相对于SFPT设置的用户访问目录,
-     * 为空则在SFTP设置的根目录进行创建文件(除设置了服务器全磁盘访问)
-     * @param multipartFile 要上传的文件
+     *
+     * @param directory       上传的目录-相对于SFPT设置的用户访问目录,
+     *                        为空则在SFTP设置的根目录进行创建文件(除设置了服务器全磁盘访问)
+     * @param multipartFile   要上传的文件
      * @param configSettingVO 配置的信息
      * @return systemFileDTO
      */
@@ -132,12 +133,12 @@ public class SftpService {
         //sftp操作类
         ChannelSftp sftp = s.getSftp();
         try {
-            try{
+            try {
                 //进入目录
                 sftp.cd(directory);
-            }catch(SftpException sException){
+            } catch (SftpException sException) {
                 //指定上传路径不存在
-                if(sftp.SSH_FX_NO_SUCH_FILE == sException.id){
+                if (sftp.SSH_FX_NO_SUCH_FILE == sException.id) {
                     //创建目录
                     sftp.mkdir(directory);
                     //进入目录
@@ -150,7 +151,7 @@ public class SftpService {
             String fileName = IdUtil.simpleUUID();
             //重命名
             file = FileUtil.rename(file, fileName, true, true);
-            InputStream in= new FileInputStream(file);
+            InputStream in = new FileInputStream(file);
             sftp.put(in, file.getName());
             in.close();
             //获取文件的后缀,不带“.”,必须是multipartFile类型
@@ -161,7 +162,7 @@ public class SftpService {
             systemFileDTO.setFileLength(Long.toString(file.length()));
             systemFileDTO.setIsDelete(0);
         } catch (Exception e) {
-            throw new Exception(e.getMessage(),e);
+            throw new Exception(e.getMessage(), e);
         } finally {
             disConn(session, channel, sftp);
         }
@@ -170,9 +171,10 @@ public class SftpService {
 
     /**
      * 下载文件
-     * @param directory 下载目录 根据SFTP设置的根目录来进行传输
-     * @param downloadFile 下载的文件名
-//     * @param saveFile 存在本地的路径
+     *
+     * @param directory       下载目录 根据SFTP设置的根目录来进行传输
+     * @param downloadFile    下载的文件名
+     *                        //     * @param saveFile 存在本地的路径
      * @param configSettingVO
      * @throws Exception
      */
@@ -187,6 +189,7 @@ public class SftpService {
         try {
             //进入目录
             sftp.cd(directory);
+            //sftp.cd(directory.substring(0, directory.indexOf("/pas")));
 //            File file = new File(saveFile);
 //            boolean bFile;
 //            bFile = false;
@@ -212,12 +215,13 @@ public class SftpService {
 
     /**
      * 删除文件
+     *
      * @param directory
      * @param deleteFile
      * @param configSettingVO
      * @throws Exception
      */
-    public static void delete(String directory, String deleteFile, ConfigSettingVO configSettingVO) throws Exception{
+    public static void delete(String directory, String deleteFile, ConfigSettingVO configSettingVO) throws Exception {
         SFTP s = new SFTP();
         //建立连接
         getConnect(s, configSettingVO);
@@ -230,7 +234,7 @@ public class SftpService {
             sftp.cd(directory);
             //删除目录
             sftp.rm(deleteFile);
-        } catch (Exception e){
+        } catch (Exception e) {
             throw new Exception(e.getMessage(), e);
         } finally {
             disConn(session, channel, sftp);