相关疑难解决方法(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万
查看次数

uploadFile.getInputstream() 抛出 java.nio.file.NoSuchFileException

我使用 JSF 2.2 和 Primefaces 5.3。我想将上传的文件保存在磁盘上。下面你可以看到我是如何尝试做到这一点的。当我按下一个Send按钮时,我得到一个java.nio.file.NoSuchFileException异常(在我的帖子末尾我还放了一个完整的堆栈跟踪):

错误 [stderr](默认任务 24)java.nio.file.NoSuchFileException: [PATH]\Wildfly_10\WILDFLY_HOME\standalone\tmp\MasterProject.war\undertow1357918758070690245upload

当我跟踪堆栈跟踪时,我看到异常是由这一行引起的(我认为是这样):

try(InputStream input = uploadFile.getInputstream()){

这对我来说很奇怪。我可以获取上传文件的名称 (by uploadFile.getFileName()),但我无法获取文件的大小 (by uploadFile.getSize()),也无法获取InputStream(by uploadFile.getInputstream())。在这两种情况下,我都得到了NoSuchFileException例外。

附加信息:我必须上传大文件(即 200 MB 或更多)。当然,当我上传小文件时,我得到了同样的例外。

您知道为什么我会遇到此异常以及如何解决此问题吗?

这是我上传文件的页面的一部分:

<h:form>
    <p:growl id="messages" showDetail="true" />
    <p:panelGrid id="panel" columns="2" styleClass="ui-noborder" columnClasses="rightalign,leftalign">

        <p:outputLabel for="file" value="File:" />
        <p:fileUpload id="file" fileLimit="1"
                    fileUploadListener="#{dataController.handleFileUpload}" 
                    mode="advanced" dragDropSupport="true" sizeLimit="1000000000" 
                    uploadLabel="Upload" cancelLabel="Delete" allowTypes="/(\.|\/)(csv|binetflow)$/" />                                             
        <p:commandButton id="buttonSend" value="Send" 
                        action="#{dataController.send()}" update="messages"/>                       

    </p:panelGrid>                                              
</h:form>
Run Code Online (Sandbox Code Playgroud)

这是 CDI bean,它是上述页面的控制器:

@Named
@ViewScoped
public class DataController implements Serializable …
Run Code Online (Sandbox Code Playgroud)

jsf file-upload primefaces wildfly

4
推荐指数
1
解决办法
4602
查看次数

标签 统计

file-upload ×2

jsf ×2

primefaces ×1

save ×1

wildfly ×1