2016-08-08 5 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(); 

는 참조 또는 문서가 원격 컴퓨터에서 같은를 만들 현지에서 파일을 읽기가 극복하는 것입니다.

고마워요

답변

0

내가 제대로 문제를 이해한다면, 로컬 파일을 읽지 않고 원격 컴퓨터에 일부 문자열 데이터를 복사 할 수 있도록하고 싶습니다. javadoc을 보면 putInputStream을 허용합니다. 그래서 당신이 할 : 당신은 또한 put(String dst) 당신이 반환되는 OutputStream에 기록 할 수 있습니다

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

참고. 그러나 나는 그것을 보여주지 않았다.