我尝试使用FTPclient将.txt文件上传到我网站的FTP服务器。以下代码只是一个片段,编译并运行正常。
System.out.println("Connecting to the FTP server...");
client.connect("---.com"); //Host and login information excluded
client.login("---", "---");//for security reasons
System.out.println("Login successful.");
System.out.println("Uploading file.");
client.setFileType(FTP.ASCII_FILE_TYPE);
OutputStream o = client.storeFileStream("test.csv");
fis = new FileInputStream(new File(filePath));
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String s;
//Attempting to use BufferedReader to read a line and write it to OutputStream
while((s = br.readLine()) != null) {
o.write(s.getBytes());
String nl = "\r\n"; //Writing the new line character.
o.write(nl.getBytes());
}
o.close();
System.out.println("Done! Terminating connection.");
client.completePendingCommand();
client.logout();
变量" filePath"将路径设置为包含以下内容的临时.txt文件:
1. Line one.
2. Line two.
3. Line three.
但是,当我将文件FTP到我的服务器时,下载它并查看它。它打印如下:
1. Line one.2. Line two.3.Line three.
我在这里怀疑的是,它只是读取文件的所有字节并完全忽略换行符。我需要在输出文件中包含换行符,因为正如您所看到的,我的导出格式是.csv,它要求每个产品(即衬衫的属性用逗号分隔)都有自己的行并用逗号分隔。 / p>
我知道如何使用InputStream和OutputStream来实现这个目标吗?