为什么我无法从ftp服务器检索txt文件以进行更改,然后再次将其还原

时间:2018-07-10 05:29:17

标签: android ftp-client

我的英语不好

我要向ftp服务器中存在的txt文件添加字符串

我阅读了txt文件并添加了字符串,然后将其保存在本地存储中,但无法再次保存在ftp服务器上

我认为我的错误在于使用两个InputStream,但我无法修复

protected Boolean doInBackground(String... userName) {
    String server = "myserver";
    int port = 21;
    String user = "user";
    String pass = "pass";
    FTPClient ftpClient = new FTPClient();
    try {
        ftpClient.connect(server, port);
        boolean success = ftpClient.login(user, pass);
        if (!success) {
            return false;
        }
        ftpClient.enterLocalPassiveMode();
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        String path = Environment.getExternalStorageDirectory() + 
File.separator + "yourFolder";
        File folder = new File(path);
        String newUsersContent = readUsersFile(ftpClient, userName[0]);
        File allUsersFile = createNewTxtFile("users", 
newUsersContent,folder);
        String usersRemoteFile = "/user_list/users.txt";
        InputStream inputStream = new FileInputStream(allUsersFile);
        boolean usersDone = ftpClient.storeFile(usersRemoteFile, 
inputStream);
        Log.d("test", "doInBackground: users" + usersDone);
        inputStream.close();
    } catch (IOException ex) {
        System.out.println("Error: " + ex.getMessage());
        ex.printStackTrace();
    } finally {
        try {
            if (ftpClient.isConnected()) {
                ftpClient.logout();
                ftpClient.disconnect();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    return true;
}

在此行:

Log.d("test", "doInBackground: users" + usersDone);

usersDone为假

它的功能:

private String readUsersFile(FTPClient ftpClient, String userName) throws 
IOException {
    String remoteFile = "/user_list/users.txt";
   InputStream inputStream = ftpClient.retrieveFileStream(remoteFile);
    String message = readLine(inputStream);
    message = message + System.getProperty("line.separator") + userName;
    Log.d("readfile", "readUsersfileAndAddNewUserTo: " + message);
    return message;
}

private String readLine(InputStream inputStream) throws IOException {
    BufferedReader reader = new BufferedReader(new 
InputStreamReader(inputStream));
    String message = "";
    String line1;
    while ((line1 = reader.readLine()) != null) {
        message = message + line1 + System.getProperty("line.separator");
    }
    reader.close();
    inputStream.close();
    Log.d("TAG", "readLine: " + message);
    return message;
}

private File createNewTxtFile(String name, String content,File folder) {
    // Create the file.
    String fileName = name + ".txt";
    File file = new File(folder, fileName);
    //write something in created txt file
    try {
        file.createNewFile();
        FileOutputStream fOut = new FileOutputStream(file);
        OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
        myOutWriter.append(content);
        myOutWriter.close();
        fOut.flush();
        fOut.close();
    } catch (IOException e) {
        Log.e("Exception", "File write failed: " + e.toString());
    }
    return file;
}

0 个答案:

没有答案