使用java将字符串内容传输到远程计算机中的文件
我需要在远程文件中放置一个字符串内容 理想情况下,我通常在本地创建一个文件,然后将该文件传输到远程计算机 下面是我用来将文件复制到远程的代码片段。使用java将字符串内容传输到远程计算机中的文件,java,jsch,Java,Jsch,我需要在远程文件中放置一个字符串内容 理想情况下,我通常在本地创建一个文件,然后将该文件传输到远程计算机 下面是我用来将文件复制到远程的代码片段。 ChannelSftp sftpChannel = (ChannelSftp) channel; File file = new File(filePathWithName);//To read the file in local machine try { sftpChan
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();
是否有任何参考或文档可以克服在本地读取文件以在远程计算机中创建相同文件的问题
-谢谢如果我正确理解您的问题,您希望能够将一些字符串数据复制到远程计算机,而无需在本地读取文件。如果查看,
put
接受InputStream
。所以你会:
InputStream stream = new ByteArrayInputStream(content.getBytes());
sftpChannel.put(stream, "name.txt");
请注意,您还可以put(String dst)
写入返回的OutputStream
。但我没有表现出来