下载文件中的字符串怪异符号

发布于 2025-02-04 18:32:10 字数 1294 浏览 3 评论 0原文

我有一个字符串数组,其中包含可下载文件的内容。我将其转换为流以进行下载,但是下载文件中有一些随机值。我不知道这是否是由于编码,如果是的,我该如何更改?

var downloadButton = new DownloadLink(btn, "test.csv", () -> {
try {
   ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
   ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
   for (int i = 0; i < downloadContent.size(); i++) {
    objectOutputStream.writeUTF(downloadContent.get(i));
   }
objectOutputStream.flush();
objectOutputStream.close();
byte[] byteArray = byteArrayOutputStream.toByteArray();

ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);
ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);

objectInputStream.close();

return new ByteArrayInputStream(byteArray);

这是下载链接类。

public class DownloadLink extends Anchor {
    public DownloadLink(Button button, String fileName, InputStreamFactory fileProvider) {
        super(new StreamResource(fileName, fileProvider), "");
        getElement().setAttribute("download", fileName);
        add(button);
        getStyle().set("display", "contents");
    }
}

这是输出文件

I've got a String array that contains the content for a downloadable file. I am converting it to a Stream for the download but there are some random values in the downloadfile. I don't know if it is due to the encoding and if yes, how can I change it?

var downloadButton = new DownloadLink(btn, "test.csv", () -> {
try {
   ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
   ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
   for (int i = 0; i < downloadContent.size(); i++) {
    objectOutputStream.writeUTF(downloadContent.get(i));
   }
objectOutputStream.flush();
objectOutputStream.close();
byte[] byteArray = byteArrayOutputStream.toByteArray();

ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);
ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);

objectInputStream.close();

return new ByteArrayInputStream(byteArray);

This is the DownloadLink class.

public class DownloadLink extends Anchor {
    public DownloadLink(Button button, String fileName, InputStreamFactory fileProvider) {
        super(new StreamResource(fileName, fileProvider), "");
        getElement().setAttribute("download", fileName);
        add(button);
        getStyle().set("display", "contents");
    }
}

this is the output file

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

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

发布评论

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

评论(1

哽咽笑 2025-02-11 18:32:10

ObjectOutputStream是Java序列化系统的一部分。除了数据本身外,它还还包括有关原始Java类型等的元数据。它仅用于撰写数据,以后将使用ObjectInputStream回复。

要创建一个供其他人下载的文件,您可以使用包装原始输出流的printWriter。另一方面,您正在使用输出流来创建byte [],这意味着一种更直接但效率略低的方法是从所有数组元素中创建一个串联的字符串然后使用getBytes(standardcharsets.utf_8)直接获得字节数组。

ObjectOutputStream is part of the Java serialization system. In addition to the data itself, it also includes metadata about the original Java types and such. It's only intended for writing data that will later be read back using ObjectInputStream.

To create a file for others to download, you could instead use a PrintWriter that wraps the original output stream. On the other hand, you're using the output stream to create a byte[] which means that a more straightforward, but slightly less efficient, way would be to create a concatenated string from all the array elements and then use getBytes(StandardCharsets.UTF_8) on it to directly get a byte array.

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