首页 > 解决方案 > 在远程服务器java上读/写文件

问题描述

嗨,我想写入远程服务器中存在的文件我尝试使用此代码我无法读取文件的内容,但我找不到任何代码来编写任何帮助

FTPClient ftpClient = new FTPClient();
            ftpClient.connect(server, port);
            ftpClient.login(user, pass);
            ftpClient.enterLocalPassiveMode();
            ftpClient.setFileType(2);
            List<String> list = new ArrayList<>();
            String remoteFile1 = "/anomalies_t24/conf/trac.ini";
            InputStream inputStream = ftpClient.retrieveFileStream(remoteFile1);
            InputStreamReader isr = new InputStreamReader(inputStream);
            BufferedReader br = new BufferedReader(isr);


                list = br.lines().collect(Collectors.toList());


                //list.forEach(System.out::println);
                for (int i = 0; i < list.size(); i++) {
                      if (list.get(i).contains("projet.options =")) {
                          list.set(i, list.get(i) + "|" + nom);

                          break;
                      }
                  }



            inputStream.close();

标签: spring

解决方案


如果您正在使用或想要使用 Apache Commons Net 3.6 FTPClient API,则使用以下方法附加文件:

public boolean appendFile(String remote, InputStream local) throws IOException

或者您可以从下面的 Apache Commons Net API 的 javadoc 链接中找到其他有用的方法:

https://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/ftp/FTPClient.html


推荐阅读