使用Java将字符串内容传输到远程计算机中的文件 [英] Transfer string content to a file in remote machine using java

查看:151
本文介绍了使用Java将字符串内容传输到远程计算机中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将字符串内容放置在远程文件中.

I need to place a string content in a remote file.

理想情况下,我曾经在本地创建文件,然后将该文件传输到远程计算机.

Ideally, I used to create a file in local and then transfer that file to remote machine.

下面是我用来将文件复制到远程的代码段.

        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();

是否有任何参考或文档可以克服在本地读取文件以在远程计算机上创建相同文件的问题.

Is there any reference or documentation to overcome reading a file in local to create the same in remote machine.

-谢谢

推荐答案

如果我正确理解了您的问题,则希望能够将某些字符串数据复制到远程计算机上,而无需在本地读取文件.如果您查看

If I understand your problem correctly, you'd like to be able to copy some string data to a remote machine without reading a file locally. If you look at the javadoc, put accepts InputStream. So you do:

InputStream stream = new ByteArrayInputStream(content.getBytes());
sftpChannel.put(stream, "name.txt");

请注意,您还可以在put(String dst)上写入返回的OutputStream.但是我没有证明.

Note that you can also put(String dst) where you can write to the OutputStream that is returned. But I didn't show that.

这篇关于使用Java将字符串内容传输到远程计算机中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆