2017-09-05 119 views
-1

你好我已經做了一個程序,我解開XML文件,獲取這些信息並使用它們連接到服務器並將文件移動到另一個文件夾。 程序如下: 1.從xml 獲取信息2.連接並上傳到服務器 3.移動已上傳到本地其他文件夾的文件。Java將文件移動到另一個目錄問題

問題是當我把對象和方法的上傳。上傳是可以的,文件上傳,文件創建新的文件夾移動到那裏,但文件不移動,他們在同一個目錄。

如果我把上面的移動對象和方法放在它的對面。這些文件被移動到另一個文件夾(本地),但他們不上傳...

什麼可以是問題,以及如何解決這個問題? 謝謝!

這裏是我的沼澤方法,有我使用這兩種方法(上傳和移動):

public void unmarshallList() { 

    try { 
     JAXBContext jc = JAXBContext.newInstance(ListNodes.class); 
     Unmarshaller ums = jc.createUnmarshaller(); 
     ListNodes ln = (ListNodes) ums.unmarshal(new File("C:\\Users\\Desktop\\marshList.xml")); 
     System.out.println("INFORMATIONS"); 
     System.out.println("-------------------------------"); 
     for (Node p : ln.getListNode()) { 
      System.out.println("Hostname: " + p.getHostname()); 
      System.out.println("Username: " + p.getUsername()); 
      System.out.println("Password: " + p.getPassword()); 
      System.out.println("Port: " + p.getPort()); 
      System.out.println("Pc Directory: " + p.getPcDirectory()); 
      System.out.println("Node Directory: " + p.getNodeDirectory()); 
      System.out.println("Time interval: " + p.getTimeInterval()); 
      System.out.println("Move Directory" + p.getMoveDir()); 
      System.out.println("-------------------------------"); 



      Upload up = new Upload(); 
      up.connection(p.getHostname(), p.getPort(), p.getUsername(), p.getPassword(), p.getNodeDirectory(), p.getPcDirectory(), p.getTimeInterval(), p.getMoveDir()); 

      Move mv = new Move(); 
      mv.moveFiles(p.getPcDirectory(), p.getMoveDir()); 


     } 

    } catch (Exception e) { 
     // TODO: handle exception 
     System.out.println(e.getMessage()); 
    } 
} 

這裏是我的招法:

public static void moveFiles (String oldLocation, String newLocation) { 
    File source = new File(oldLocation); 
    File dest = new File(newLocation); 

    new File(newLocation).mkdir(); 

    File[] files = source.listFiles(); 

    for (File file : source.listFiles()) { 

     System.out.println(source + "\\" + file.getName()); 

     String x = (source + "\\" + file.getName()); 
     String y = (dest + "\\" + file.getName()); 


     File f1 = new File(x); 
     f1.renameTo(new File(y)); 
     System.out.println("This file is moved "+x); 
    } 

    System.out.println("The files are moved"); 
} 

這裏是上傳方法:

private static void recursiveFolderUpload(String sourcePath, String 
destinationPath) throws SftpException, FileNotFoundException { 

    File sourceFile = new File(sourcePath); 
    if (sourceFile.isFile()) { 

     // copy if it is a file 
     channelSftp.cd(destinationPath); 
     if (!sourceFile.getName().endsWith(".xml")); 
      channelSftp.put(new FileInputStream(sourceFile), sourceFile.getName(), ChannelSftp.OVERWRITE); 

    } else { 

     System.out.println("inside " + sourceFile.getName()); 
     File[] files = sourceFile.listFiles(); 

     if (files != null && !sourceFile.getName().endsWith(".xml")) { 

      channelSftp.cd(destinationPath); 
      SftpATTRS attrs = null; 

      // check if the directory is already existing 
      try { 
       attrs = channelSftp.stat(destinationPath + "/" + sourceFile.getName()); 
      } catch (Exception e) { 
       System.out.println(destinationPath + "/" + sourceFile.getName() + " not found"); 
      } 

      // else create a directory 
      if (attrs != null) { 
       System.out.println("Directory exists IsDir=" + attrs.isDir()); 
      } else { 
       System.out.println("Creating dir " + sourceFile.getName()); 
       channelSftp.mkdir(sourceFile.getName()); 
      } 

      for (File f: files) { 
       recursiveFolderUpload(f.getAbsolutePath(), destinationPath + "/" + sourceFile.getName()); 
      } 

     } 
    } 
+0

你試過調試嗎?正在打印什麼? –

+0

是的,我試圖調試它,但我沒有注意到一些奇怪的東西。關於打印出來沒有錯誤... – xmlParser

+0

歡迎來到Stackoverflow!請按照指導[如何問](https://stackoverflow.com/help/how-to-ask),你應該提供一個可測試的例子,exeption,你的問題到底在哪裏?只是一個想法 - 不要在試圖移動文件時保持文件打開(inputstream)? – gusto2

回答

0

我的猜測是源文件的InputStream未關閉。

我想嘗試

InputStream in = new FileInputStream(sourceFile) 
try { 
    channelSftp.put(in, sourceFile.getName(), ChannelSftp.OVERWRITE); 
} finally { 
    in.close(); 
} 

或關閉的InputStream uppon退出try語句的另一種方式

try (InputStream in = new FileInputStream(sourceFile)) { 
    channelSftp.put(in, sourceFile.getName(), ChannelSftp.OVERWRITE); 
} 

但是 - 沒有調試/任何異常消息它幾乎是不可能的肯定

+0

當我添加這個,文件被移動,但沒有上傳。它說(文件夾)訪問被拒絕。 – xmlParser

相關問題