2012-06-22 35 views
1

我們,我們試圖來解讀一個Java對象,將其轉換爲一個InputStream和SFTP過來使用JSCH APIjsch的InputStream的ObjectOutputStream和解組

基本上是一個場景 - >我們的目標是迫使jSCH到推對象的InputStream的形式而不將其轉換成一個文件,然後在這個例子中發送過來喜歡這裏:

// create a session 
session = jsch.getSession(ftpUserName, ftpHost, ftpPort); 
channel = session.openChannel("sftp"); 
channel.connect(); 
ChannelSftp sftpTunnel = (ChannelSftp) channel; 

// push the file you created using the channel created by the session 
File f = new File(fileToTransmit); 


>> **sftpTunnel.put(new FileInputStream(f), f.getName());** 

除了我們要轉出最後一行用普通老式的InputStream無關聯到任何文件。

按照jSCH API - 不使用一個FileInputStream是可能的:

void put(InputStream src, String dst) 

任何想法,以便我們如何會去嗎?

我們可以使用ObjectOutputStream嗎?有沒有辦法取出一個對象並解組它,然後將它提交給一個InputStream,而不需要在中間創建一個文件?

感謝

回答

0

我知道這是非常晚,但是這可能會爲人們堅持的類似十歲上下的問題

/* Code to connect as posted in your question */ 

/* 
* Getting the file from the respective directory 
* Storing it in an InputStream 
*/ 
sftpChannel.cd(Src_Directory); 
InputStream temp = sftpChannel.get(Your_filename); 
System.out.println(temp);// can view the file as an obj 

/* 
* Converting stream to string and then back to InputStream using utils 
* Needed to do this as the file being "put" was not of the right charset 
*/ 
String theString = IOUtils.toString(temp, "UTF-8"); 
InputStream temp1 = IOUtils.toInputStream(theString, "UTF-8"); 

/* 
* Putting the file back to resp directory 
* with a filename 
/* 
sftpChannel.cd(Tar_Directory); 
sftpChannel.put(temp1, filename); 

/* Code to close channel and session! */ 

這裏在未來有用,我假設我們是「放」的輸入流到同一主機。如果沒有,我們總是可以連接到所需的主機並將文件放在那裏。

希望這會有所幫助!