为什么在使用流解析return 字节数组输出流时列前面有一个't'?
Why there is a 't' in front of column while using stream resolution return byte array outputstream?
我正在使用 Spring 框架,并使用 streamresolution return 一个 .txt 文件供用户下载。
数据结果没问题,但是每列数据前面都有一个't',
除了最后一列,每列末尾都有一个'w'。
我不明白为什么,因为数据看起来很好,我没有告诉程序创建信件。
这是我的代码:
// A list of String, which are the data, it might looks like 20200810,a,b,c,100,55,.....
// the whole is a String contains comma
List<String> dataList = (List<String>) parameters.get("myData");
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
StreamingResolution streamingResolution = null;
ObjectOutputStream oos = new ObjectOutputStream(outputStream);
oos.writeObject("\n");
for (String s : dataList) {
oos.writeObject(s.trim());
oos.writeUTF("\n");
}
streamingResolution = new StreamingResolution("text/plain", new ByteArrayInputStream(outputStream.toByteArray()));
streamingResolution.setCharacterEncoding(CharEncoding.UTF_8);
String year = Integer.toString((Integer.parseInt(end.substring(0, 4));
String day = year + end.substring(4, 6);
oos.close();
return streamingResolution.setFilename(day + ".txt");
当我下载数据时,202108.txt
它可能看起来像
t ?0210810,a,b,c,100,55w
t ?0210810,d,e,f,99,60
谁能告诉我为什么前面会有一个't'
最后是 'w'?
以及如何解决这个问题?
非常感谢。
此代码使用 ObjectOutputStream
, which is used to write serialized Java data in a binary format. It is not a plain text format, and should not be used in this way. The extra characters are bytes that are defined in the Java Object Serialization Specification.
要写入纯文本,您可以使用 java.io.PrintStream
class 代替。例如:
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PrintStream printStream = new PrintStream(outputStream, false, StandardCharsets.UTF_8);
printStream.println();
for (String s : dataList) {
printStream.println(s.trim());
}
printStream.flush();
StreamingResolution streamingResolution = new StreamingResolution("text/plain", new ByteArrayInputStream(outputStream.toByteArray()));
streamingResolution.setCharacterEncoding(CharEncoding.UTF_8);
请注意,我还通过将 streamingResolution
局部变量声明移至分配它的位置来简化代码。
这是对所提供代码的直接翻译,向您展示如何使用 PrintStream
class,但它可能不是最好的编写方式。 StreamingResolution
class 似乎是 Stripes Framework 的一部分。它旨在将大量响应流式传输到客户端。然而,这个实现实际上并没有流式传输响应,而是将它累积到一个字节数组中。实现这一点的更好方法是将 class 子 StreamingResponse
class,如 Stripe 文档中所述,直接写入响应:
return new StreamingResolution("text/plain") {
public void stream(HttpServletResponse response) throws Exception {
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.println();
for (String s : dataList) {
out.println(s.trim());
}
out.flush();
}
}.setFilename(day + ".txt");
我正在使用 Spring 框架,并使用 streamresolution return 一个 .txt 文件供用户下载。
数据结果没问题,但是每列数据前面都有一个't', 除了最后一列,每列末尾都有一个'w'。
我不明白为什么,因为数据看起来很好,我没有告诉程序创建信件。
这是我的代码:
// A list of String, which are the data, it might looks like 20200810,a,b,c,100,55,.....
// the whole is a String contains comma
List<String> dataList = (List<String>) parameters.get("myData");
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
StreamingResolution streamingResolution = null;
ObjectOutputStream oos = new ObjectOutputStream(outputStream);
oos.writeObject("\n");
for (String s : dataList) {
oos.writeObject(s.trim());
oos.writeUTF("\n");
}
streamingResolution = new StreamingResolution("text/plain", new ByteArrayInputStream(outputStream.toByteArray()));
streamingResolution.setCharacterEncoding(CharEncoding.UTF_8);
String year = Integer.toString((Integer.parseInt(end.substring(0, 4));
String day = year + end.substring(4, 6);
oos.close();
return streamingResolution.setFilename(day + ".txt");
当我下载数据时,202108.txt 它可能看起来像
t ?0210810,a,b,c,100,55w
t ?0210810,d,e,f,99,60
谁能告诉我为什么前面会有一个't' 最后是 'w'? 以及如何解决这个问题?
非常感谢。
此代码使用 ObjectOutputStream
, which is used to write serialized Java data in a binary format. It is not a plain text format, and should not be used in this way. The extra characters are bytes that are defined in the Java Object Serialization Specification.
要写入纯文本,您可以使用 java.io.PrintStream
class 代替。例如:
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PrintStream printStream = new PrintStream(outputStream, false, StandardCharsets.UTF_8);
printStream.println();
for (String s : dataList) {
printStream.println(s.trim());
}
printStream.flush();
StreamingResolution streamingResolution = new StreamingResolution("text/plain", new ByteArrayInputStream(outputStream.toByteArray()));
streamingResolution.setCharacterEncoding(CharEncoding.UTF_8);
请注意,我还通过将 streamingResolution
局部变量声明移至分配它的位置来简化代码。
这是对所提供代码的直接翻译,向您展示如何使用 PrintStream
class,但它可能不是最好的编写方式。 StreamingResolution
class 似乎是 Stripes Framework 的一部分。它旨在将大量响应流式传输到客户端。然而,这个实现实际上并没有流式传输响应,而是将它累积到一个字节数组中。实现这一点的更好方法是将 class 子 StreamingResponse
class,如 Stripe 文档中所述,直接写入响应:
return new StreamingResolution("text/plain") {
public void stream(HttpServletResponse response) throws Exception {
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.println();
for (String s : dataList) {
out.println(s.trim());
}
out.flush();
}
}.setFilename(day + ".txt");