相关疑难解决方法(0)

如何在JSF中保存上传的文件

我在JSF上传文件.我正在使用Tomahawk <t:inputFileUpload>,但同样的问题适用于例如PrimeFaces <p:fileUpload>和JSF 2.2 <h:inputFile>.

我有以下支持bean代码:

private UploadedFile uploadedFile; // +getter+setter

public String save() throws IOException {
    String name = uploadedFile.getName();
    System.out.println("File name: " + name);

    String type = uploadedFile.getContentType();
    System.out.println("File type: " + type);

    long size = uploadedFile.getSize();
    System.out.println("File size: " + size);  

    InputStream stream = uploadedFile.getInputStream();
    byte[] buffer = new byte[(int) size];  
    stream.read(buffer, 0, (int) size);  
    stream.close();  
}
Run Code Online (Sandbox Code Playgroud)

我能够获取文件名,类型和大小,但我无法将此文件保存在特定路径中.我无法找出保存上传文件的正确方法.我怎样才能做到这一点?

jsf file-upload save

18
推荐指数
1
解决办法
6万
查看次数

标签 统计

file-upload ×1

jsf ×1

save ×1