2016-08-08 2 views
1

リモートファイルに文字列コンテンツを配置する必要があります。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(); 

リモートマシンで同じことを作成するために、ローカルでファイルを読み込む克服するための任意の参照やドキュメントがあります。

-Thankあなた

答えて

0

私が正しくあなたの問題を理解していれば、あなたは、ローカルのファイルを読み込むことなく、リモートマシンにいくつかの文字列データをコピーできるようにしたいと思います。 javadocを見ると、putInputStreamを受け入れます。だから、やる:あなたもput(String dst)あなたが返されOutputStreamに書き込むことができ

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

注意を。しかし、私はそれを示していませんでした。

関連する問題