Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用java将字符串内容传输到远程计算机中的文件_Java_Jsch - Fatal编程技术网

使用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
。但我没有表现出来