2017-04-24 45 views
0

我試圖使用scp使用PEM文件如何使用scp使用PEM file.My程序顯示錯誤,但顯示在服務器

我的整個程序正在執行我的文件夾傳輸什麼(我的文件)傳輸文件夾罰款但文件不在服務器上顯示。 任何人都可以告訴我在哪裏,我在做錯誤

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.text.SimpleDateFormat; 
import java.util.Arrays; 
import java.util.Date; 
import java.util.List; 

import com.jcraft.jsch.Channel; 
import com.jcraft.jsch.JSch; 
import com.jcraft.jsch.JSchException; 
import com.jcraft.jsch.Session; 


public class jschclass { 

public static void main(String[] args) { 

     String SFTPHOST = "xx.xxx.xx.xxx"; 
     int SFTPPORT = 22; 
     String SFTPUSER = "myusername"; 
    //  String SFTPPASS = "password"; 
     String SFTPWORKINGDIR = "/var/www/html/projects/demoo_reports/"; 

     String Pemfilepath=System.getProperty("user.dir")+File.separator+"src"+File.separator+"lib"+File.separator+"airtel.pem"; 
     String targetFolder = System.getProperty("user.dir")+File.separator+"test-output"; 
    //String Pemfilepath="./src/lib/airtel.pem"; 

    //String targetFolder = "./test-output"; 
    File folder = new File("./test-output"); 

    System.out.println("folder=" + folder); 
    if (folder.listFiles() != null) { 
     List<File> listOfFiles = Arrays.asList(folder.listFiles()); 
     for (File file : listOfFiles) { 
      // System.out.println("File name: " + file.getName()); 
     // System.out.println("Full path: " + file.getAbsolutePath()); 
      System.out.println("Target path: " + targetFolder + file.getName()); 
      System.out.println(); 
     } 
    } 

    ///////////////////// 
    final JSch jsch = new JSch(); 
    try { 
     jsch.addIdentity(Pemfilepath); 
     System.out.println("identity added "); 


      Session session = jsch.getSession(SFTPUSER,SFTPHOST,22); 
      System.out.println("session created."); 

      java.util.Properties config = new java.util.Properties(); 
      config.put("StrictHostKeyChecking", "no"); 
      session.setConfig(config); 

      session.connect(); 
      System.out.println("Connected"); 



     //////////// 



     SimpleDateFormat dateFormatForFoldername = new SimpleDateFormat("yyyy-MM-dd");//dd/MM/yyyy 
     Date currentDate = new Date(); 
     String folderDateFormat = dateFormatForFoldername.format(currentDate); 
     String command = "scp -i "+Pemfilepath+" -r "+targetFolder+" "+"[email protected]:/var/www/html/projects/demo_reports/"+folderDateFormat+"/"; 
     System.out.println(command); 
     Channel channel = session.openChannel("exec"); 

     //channel.setCommand(command); 
     //channel.setErrStream(System.err); 
     channel.connect(); 

     System.out.println("Files is up"); 

       channel.disconnect(); 
       session.disconnect(); 
       System.out.println("Disconnected"); 
     //////////// 
    } catch (JSchException e) { 
     e.printStackTrace(); 
    }  
    } 
} 
+0

嘗試重命名該文件。也許有空白問題 – Jens

+0

我已經嘗試,但得到同樣的問題 –

+0

你在命令行嘗試過'ssh'嗎? – Jens

回答

-1

在我已經直接用sh exector從我的地方,無需連接到服務器的第一

"echo "+pass+" | "+"sudo -S爲我工作的另一個計算策略

我完整的代碼看起來如下: -

String pass = "\"Yourpassword\""; 
       out.println("echo "+pass+" | "+"sudo -S scp -i "+Pemfilepath+" -r "+targetFolder+" "+"[email protected]:/var/www/html/projects/demoproject"); 

希望它會幫助你:)

0

,你在做什麼,不能工作。只能在遠程服務器上執行命令才能上傳文件。服務器將從哪裏獲取文件內容?它不能神奇地訪問你的本地文件。

查看official JSch ScpTo.java example以正確執行SCP上傳。

+0

我對此很陌生。所以是的,不知道背景中發生了什麼。我正在使用窗口操作系統。我試過你的示例代碼,因爲它不適合我。該代碼在第一階段退出: - if(arg.length!= 2)System.err.println(「usage:java ScpTo file1 user @ remotehost:file2」); System.exit(-1); } –

+0

我是否缺少一些步驟。你能不能更清楚一點,以便我可以相應地執行命令 –

+0

只需刪除對'arg'的所有引用,並將它們替換爲要使用的路徑和憑證。查看變量'lfile','user','host'和'rfile' +調用'session.setPassword'來提供密碼,而不是使用交互式登錄(移除'session.setUserInfo(ui)')。 - 當然代碼上傳單個文件。你必須循環它來上傳文件夾中的所有文件。 –

相關問題