上传 .pdf、.doc 或 .docx 时出现 HTTP 错误。使用 primefaces 上传工具上传文件

发布于 2024-10-27 19:22:50 字数 2674 浏览 0 评论 0原文

我从昨天开始使用 Primefaces 上传工具,但今天我开始使用不同的文件扩展名来测试它。让我惊讶的是,我唯一可以成功上传的文件是 .txt 我不明白为什么会这样。我在网上看到了代码片段,我认为我的代码几乎是相同的。我错过了什么吗?

这里有更多信息:

这是错误:

在此处输入图像描述

警告:StandardWrapperValve[Faces Servlet]:PWC1406:servlet Faces Servlet 的 Servlet.service() 引发异常 java.io.IOException:处理多部分/表单数据请求失败。 \uploaded\upload_3be1503c_12f00f7e117__7ffb_00000007.tmp (系统找不到指定的路径) 在 org.primefaces.webapp.MultipartRequest.parseRequest(MultipartRequest.java:67) 在 org.primefaces.webapp.MultipartRequest.(MultipartRequest.java:49)

这是 JSF 的代码

<h:form enctype="multipart/form-data">

  <!-- New Upload tool -->
        <p:fileUpload fileUploadListener="#{uploadController.handleFileUpload}"   
        allowTypes="*.doc;*.docx;*.pdf;*.odt;" description="Text"/>

</h:form>

这是托管 bean 的代码的一部分

            public void handleFileUpload(FileUploadEvent event) {

    uploadedFile = event.getFile();
    String fileName = FilenameUtils.getName(uploadedFile.getFileName());
    String contentType = uploadedFile.getContentType();
    byte[] bytes = uploadedFile.getContents();

    Garbage garbage = new Garbage();
    garbage.setFilename(fileName);
    garbage.setFile(bytes);
    garbage.setDescription("info about the file");
    garbage.setFileType("File extension");
    fileUploaderEJB.uploadGarbage(garbage);

    FacesContext.getCurrentInstance().addMessage(
            null,
            new FacesMessage(String.format(
                    "File '%s' of type '%s' successfully uploaded!",
                    fileName, contentType)));
}

以防万一,primefaces 相关的内容位于 web.xml

<servlet>
<servlet-name>Resource Servlet</servlet-name>
<servlet-class>org.primefaces.resource.ResourceServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Resource Servlet</servlet-name>
<url-pattern>/primefaces_resource/*</url-pattern>
</servlet-mapping> 
 <filter>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <filter-class>org.primefaces.webapp.filter.FileUploadFilter
    </filter-class>
    <init-param>
        <param-name>uploadDirectory</param-name>
        <param-value>/uploaded</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
</filter-mapping>

I am using the Primefaces upload tool since yesterday, but today i started to test it with different file extensions. My surprise was that the only file that i can succesfully upload is .txt I dont understand why is that. I saw code snipets around the web and i think my code is almost the same. Am i missing something?

Here a bit more info:

This is the error:

enter image description here

WARNING: StandardWrapperValve[Faces Servlet]: PWC1406: Servlet.service() for servlet Faces Servlet threw exception
java.io.IOException: Processing of multipart/form-data request failed. \uploaded\upload_3be1503c_12f00f7e117__7ffb_00000007.tmp (The system cannot find the path specified)
at org.primefaces.webapp.MultipartRequest.parseRequest(MultipartRequest.java:67)
at org.primefaces.webapp.MultipartRequest.(MultipartRequest.java:49)

This is the code at the JSF

<h:form enctype="multipart/form-data">

  <!-- New Upload tool -->
        <p:fileUpload fileUploadListener="#{uploadController.handleFileUpload}"   
        allowTypes="*.doc;*.docx;*.pdf;*.odt;" description="Text"/>

</h:form>

This is part of the code at the managed bean

            public void handleFileUpload(FileUploadEvent event) {

    uploadedFile = event.getFile();
    String fileName = FilenameUtils.getName(uploadedFile.getFileName());
    String contentType = uploadedFile.getContentType();
    byte[] bytes = uploadedFile.getContents();

    Garbage garbage = new Garbage();
    garbage.setFilename(fileName);
    garbage.setFile(bytes);
    garbage.setDescription("info about the file");
    garbage.setFileType("File extension");
    fileUploaderEJB.uploadGarbage(garbage);

    FacesContext.getCurrentInstance().addMessage(
            null,
            new FacesMessage(String.format(
                    "File '%s' of type '%s' successfully uploaded!",
                    fileName, contentType)));
}

Just in case, the primefaces related sutuff at web.xml

<servlet>
<servlet-name>Resource Servlet</servlet-name>
<servlet-class>org.primefaces.resource.ResourceServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Resource Servlet</servlet-name>
<url-pattern>/primefaces_resource/*</url-pattern>
</servlet-mapping> 
 <filter>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <filter-class>org.primefaces.webapp.filter.FileUploadFilter
    </filter-class>
    <init-param>
        <param-name>uploadDirectory</param-name>
        <param-value>/uploaded</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
</filter-mapping>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱格式化 2024-11-03 19:22:50

\uploaded\upload_3be1503c_12f00f7e117__7ffb_00000007.tmp(系统找不到指定的路径)

与安装 Web 服务器的根磁盘上的文件夹 /uploaded 丢失。创建它。

\uploaded\upload_3be1503c_12f00f7e117__7ffb_00000007.tmp (The system cannot find the path specified)

The folder /uploaded on the same root disk as where your webserver is installed is missing. Create it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文