我是 Flutter 的新手,所以如果您在代码中遇到任何愚蠢的错误,请耐心等待。我当前正在处理的问题与处理二进制文件(特别是 wav 文件)有关。 文本文件的示例代码运行得非常顺利 - 我可以毫无问题地连接到 FTP、下载和上传文件。然而,当下载 wav 文件时,结果音频听起来失真。
经过进一步调查,我发现FTP服务器上的原始文件(名为message2.wav)中的空格(0x20)在下载的版本(我们称之为DownloadedStepByStep.wav)中被转换为0x0D。
我已分享文件以供参考here。感谢您的理解!
import 'dart:ffi';
import 'dart:io';
import 'dart:typed_data';
import 'package:ftpconnect/ftpconnect.dart';
import 'dart:convert' show Encoding, LineSplitter, Utf8Codec, Utf8Decoder, Utf8Encoder, utf8;
void main() async {
final FTPConnect _ftpConnect = new FTPConnect( "ftp.xxxxxx.com", user: "[email protected]", pass: "xxxxx", showLog: true , port: 21 );
///an auxiliary function that manage showed log to UI
Future<void> _log(String log) async {
print(log);
await Future.delayed(Duration(seconds: 1));
}
///mock a file for the demonstration example
const X=<int>[]; // added by me as input of List<int> to function below
Future<File> _fileMockwav({fileName = 'FlutterTest.wav' , content =X }) async {
final Directory directory = Directory('/test')..createSync(recursive: true);
final File file = File('${directory.path}/$fileName');
await file.writeAsBytes(content ); // changed by me
return file;
}
Future<void> _downloadStepByStep() async {
try {
await _log('Connecting to FTP ...');
await _ftpConnect.connect();
await _log('Downloading ...');
String fileName = '../message2.wav';
//here we just prepare a file as a path for the downloaded file
File downloadedFile = await _fileMockwav(fileName: 'DownloadedStepByStep.wav');
await _ftpConnect.downloadFile(fileName, downloadedFile);
await _log('file downloaded path: ${downloadedFile.path}');
await _ftpConnect.disconnect();
} catch (e) {
await _log('Downloading FAILED: ${e.toString()}');
}
}
await _downloadStepByStep();
}
wav 文件不应该被扭曲,并且空格转换为 0x0d 是问题所在。 不知道如何避免这种转换...
那些不是空格。这些是丢失的字节。
0A
(LF) 转换为 0D
+0A
(CRLF) – 文本模式转换的标志。
我不知道 flutter,但看来你需要用
setTransferType
来调用 TransferType.binary
。