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

huangapple go评论73阅读模式
英文:

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

问题

我上传了一个文件,可以获取文件名、类型、大小和内容。内容以字节数组的形式呈现。我希望将内容以其原始格式,即文本文件(字符串),获取出来。是否有办法像io中的File那样读取primefaces的uploadedFile,或者有没有办法将内容以字符串格式读取出来。

英文:

I uploaded a file and i can get the file name, type, size and content. The content is in a format of byte array. I want the cotnet in it's original format, which is text file(String). Is there a way to read the uploadedFile of the primefaces like the File in io. or is there a way i can read the content in a string format.

答案1

得分: 1

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

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

另请参阅:

英文:

It's simple. The uploaded file will contain a byte[] of the contents of the uploaded file, so you can simply construct a String from it:

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

See also:

huangapple
  • 本文由 发表于 2020年10月4日 21:35:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/64195290.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定