0

FTP サーバー上のファイルを読み取り、そのデータを別のファイルに書き込んでいます。しかし、データを適切に読み取った後、ファイルを FTP サーバーに書き込むことができませんでした。

「ファイル取得」でファイルを取得できますが、storefile機能でファイルを保存できません。

私のコードは次のとおりです。

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;

public class FtpDemo {

public static void main(String args[]){

FTPClient client=new FTPClient();       
try {
                if(client.isConnected()){
                    client.disconnect();
                    Boolean isLog=client.logout();
                    System.out.println(isLog);
                }

                client.connect("server");
                Boolean isLogin=client.login("user","password");

                if(isLogin){
                    System.out.println("Login has been successfully");
                    FTPFile[] files=client.listFiles();
                    System.out.println("Login has been successfully"+files.length);
                    for(int i=0;i<files.length;i++){
                        if(files[i].getName().equals("rajesh.txt")){
                            System.out.println("match has been successfully");
                            InputStream is=client.retrieveFileStream("/rajesh.txt");
                            BufferedReader br=new BufferedReader(new InputStreamReader(is));
                            String str;
                            String content="";
                            str=br.readLine();

                            while(str!=null){
                                content+=str;
                                str=br.readLine();
                            }

                            System.out.println(content);
                            Boolean isStore=client.storeFile("/rajesh.txt",is);
                            System.out.println(isStore);
                        }
                    }
                }
            }
            catch(Exception e){
                System.out.println(e.getMessage());
            }
        }
}
4

1 に答える 1

0

数点、isで最後まで読むbr。新しいファイルは同じ名前でした。Stream (バイナリ データ) の代わりに Reader (テキスト) を使用すると、エンコーディングでのテキストの読み取りに依存します。ストリームを使用することをお勧めします。

                        InputStream is=client.retrieveFileStream("/rajesh.txt");
                        final String encoding = "UTF-8"; // "Cp1252"?
                        BufferedReader br=new BufferedReader(new InputStreamReader(is, encoding));
                        String str;
                        String content="";
                        str=br.readLine();

                        while(str!=null){
                            content+=str + "\n"; // "\r\n"?
                            str=br.readLine();
                        }
                        br.close();

                        InputStream is2 = new ByteArrayInputStream(content.getBytes(encoding));

                        System.out.println(content);
                        Boolean isStore=client.storeFile("/rajesh2.txt",is2);
于 2012-05-02T07:13:04.547 に答える