[英]Transfer string content to a file in remote machine using java
我需要在遠程文件中放置一個字符串內容。
理想情況下,我曾經在本地創建一個文件,然后將該文件傳輸到遠程機器。
下面是我使用的代碼片段,用於將文件復制到遠程。
ChannelSftp sftpChannel = (ChannelSftp) channel;
File file = new File(filePathWithName);//To read the file in local machine
try {
sftpChannel.cd(location);//Remote location
//Transferring the file to RemoteLocation.
sftpChannel.put(new FileInputStream(file), file.getName());//.(Here I don't want read a file.) //Instead I want copy a content which is in string variable, something like below two lines, to the remote location.
String content = "abcdefg";
sftpChannel.put(content,"someFileName")
} catch (SftpException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
sftpChannel.exit();
是否有任何參考或文檔可以克服在本地讀取文件以在遠程機器上創建相同的文件。
-謝謝你
如果我正確理解您的問題,您希望能夠將一些字符串數據復制到遠程機器,而無需在本地讀取文件。 如果您查看javadoc , put
接受InputStream
。 所以你也是:
InputStream stream = new ByteArrayInputStream(content.getBytes());
sftpChannel.put(stream, "name.txt");
請注意,您還可以put(String dst)
放在可以寫入返回的OutputStream
位置。 但我沒有表現出來。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.