英文:
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:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论