序列化文本文件缺少新行

发布于 2024-12-11 14:37:04 字数 626 浏览 0 评论 0原文

我正在尝试通过 XML-RPC Base64 编码/解码将本地文件放到远程主机上。这对于二进制文件来说非常有效,但是当我尝试发送文本文件时,所有行结尾都被删除。为什么会出现这种情况?

在客户端,

my $buf;
my $encoded = '';
while (read($FILE, $buf, 60 * 57)) {
   $encoded .= encode_base64($buf);
}

然后将其发送到我的 Redstone XML-RPC 服务器,该服务器接收并将其写出:

// Create file
   File file = new File(path);
   file.createNewFile();

// Decode the encoded data sent over into bytes
   byte[] bytes = Base64.decode(data.getBytes());

// Write them out to the file
    FileOutputStream os = new FileOutputStream(file);
    os.write(bytes);
    os.flush();
    os.close();

I'm trying to put a local file onto a remote host via XML-RPC Base64 encode/decode. This works perfectly fine for binary files, but when I try to send over the text file, all the line endings are removed. Why's this happening?

On the client side,

my $buf;
my $encoded = '';
while (read($FILE, $buf, 60 * 57)) {
   $encoded .= encode_base64($buf);
}

To which it then sends over to my Redstone XML-RPC server, which takes it and writes it out:

// Create file
   File file = new File(path);
   file.createNewFile();

// Decode the encoded data sent over into bytes
   byte[] bytes = Base64.decode(data.getBytes());

// Write them out to the file
    FileOutputStream os = new FileOutputStream(file);
    os.write(bytes);
    os.flush();
    os.close();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑红尘 2024-12-18 14:37:04

尝试以二进制模式设置$FILE,需要在open命令后指定:

open my $FILE, '<', 'the_file_name.extension';
binmode $FILE;

# your code ...

Try to set the $FILE in binary mode, you need to specify after the open command:

open my $FILE, '<', 'the_file_name.extension';
binmode $FILE;

# your code ...
可爱咩 2024-12-18 14:37:04

问题是我在记事本中打开,它无法识别 CRLF 结尾。

The problem was that I was opening in Notepad, which wasn't recognizing CRLF endings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文