使用原始格式字符串(文本)读取上传文件的内容

Read uploadedFile's content with it's original format string(text)

我上传了一个文件,我可以得到文件名、类型、大小和内容。内容为字节数组格式。我想要原始格式的 cotnet,即文本文件(字符串)。有没有办法像 io.data 中的文件一样读取 primefaces 的上传文件?或者有什么方法可以读取字符串格式的内容。

很简单。上传的文件将包含上传文件内容的byte[],因此您可以简单地从中构造一个String

public void handleUpload(final FileUploadEvent event) throws IOException {
  String content = new String(event.getFile().getContent(), StandardCharsets.UTF_8);
}

另请参阅:

  • How to convert byte array to string and vice versa?