簡體   English   中英

無法使用Java中的FTPClient在FTP服務器上寫入文件

[英]Could Not Write File on FTP server using FTPClient in Java

我正在讀取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());
            }
        }
}

幾點, is讀取由最終br 新文件具有相同的名稱。 使用讀取器(文本)代替流(二進制數據)依賴於讀取編碼中的文本。 更好地使用流。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM