上传 .pdf、.doc 或 .docx 时出现 HTTP 错误。使用 primefaces 上传工具上传文件
我从昨天开始使用 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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
与安装 Web 服务器的根磁盘上的文件夹
/uploaded
丢失。创建它。The folder
/uploaded
on the same root disk as where your webserver is installed is missing. Create it.