JSF 2.0 文件上传 [英] JSF 2.0 File upload
问题描述
我正在浏览一些博客,试图找到如何使用 JSF 2.0 上传文件但是所有的解决方案都让我感到困惑.我想知道我究竟需要什么才能成功上传文件(MP3、PDF、视频...任何类型)并将其作为@Lob 存储在数据库中.这是我到目前为止所做的:
I am looking around a few blogs, to try to find how to upload files using JSF 2.0 But all the solutions kind of confuse me. I would like to know what do I exactly need to be able to successfully upload a file(MP3, PDF, video... what ever type) and store it in a database as a @Lob. This is what I have done so far:
我创建了一个实体,该实体具有 byte[] 类型的属性,并且还使用了 @Lob 注释.
I created an entity that has an attribute of type byte[] and it is also annotated with a @Lob annotation.
我创建了一个 EJB,该 EJB 将使用以 byte[] 作为参数的方法引入实体,并使用 EntityManager 类(persist 方法)将其插入到数据库中.
I created an EJB that will introduce the entity with with a method that has a byte[] as a parameter and inserts it into the database using the EntityManager class( persist method).
我创建了一个 JSF 页面,其中包含一个文件"类型的输入标签和一个提交按钮
I created a JSF page with an input tag of type "file" and a submit button
我准备了一个托管 bean,用于与 JSF 页面交换有关文件的信息.
I prepared a managed bean to interchange information about the file with the JSF page.
现在我卡住了,我有很多疑问:
Now I am stuck, and I have lots of doubts:
我应该怎么做才能将文件从 JSF 传递到托管 bean,然后将其转换为字节 [](以便能够将其处理到 EJB)?
What should I do to pass the file from the JSF to the managed bean and then transform it to a byte[](To be able to handle it over to the EJB)?
servlet 如何帮助我?
How can a servlet help me?
我是否需要 servlet 来执行此操作?
Do I need a servlet to do this?
我还发现在一些博客中提到了一些关于 servlets 3.0 的内容,但我不知道我的工作环境是否在使用它,如果我使用的是 servlets 3.0(我使用的是 JEE6)怎么办?
Also I found that in some blog it mentions something about servlets 3.0, but I don't know if my working environment is using it, how can if I am using servlets 3.0 (I am using JEE6)?
我以前从未上传过文件,而且我对 servlet 也不是很熟悉.我很困惑,有人可以给我一些入门提示吗?
I never did file upload before and also I am not very familiar with servlets. I am confused, someone could give me some starting tips, please?
推荐答案
首先,这个(旧的)问题和答案假设 JSF 2.0/2.1.从 JSF 2.2 开始,就有了一个原生的
组件,不需要第三方组件库.另请参阅 如何使用 JSF 2.2 <h:inputFile> 上传文件?保存的文件在哪里?
First of all, this (old) question and answer assumes JSF 2.0/2.1. Since JSF 2.2 there's a native <h:inputFile>
component without the need for 3rd party component libraries. See also How to upload file using JSF 2.2 <h:inputFile>? Where is the saved File?
最简单的方法是使用 Tomahawk for JSF 2.0.它提供了一个
组件.
The easiest way would be using Tomahawk for JSF 2.0. It offers a <t:inputFileUpload>
component.
以下是分步教程:
为 Servlet 3.0 和 JSF 2.0 创建一个空白的动态 Web 项目.
web.xml
必须符合 Servlet 3.0 规范并且已经包含 JSF servlet:
Create a blank dynamic web project for Servlet 3.0 and JSF 2.0. The
web.xml
must comply Servlet 3.0 spec and already contain the JSF servlet:
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="YourProjectName" version="3.0">
<display-name>Your Project Name</display-name>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
</web-app>
faces-config.xml
必须符合 JSF 2.0 规范:
The faces-config.xml
must comply JSF 2.0 spec:
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">
</faces-config>
下载战斧1.1.10 对于 JSF 2.0.解压 zip 文件,转到 /lib
文件夹并将所有 *.jar
文件复制到您的 /WEB-INF/lib
中.
Download Tomahawk 1.1.10 for JSF 2.0. Extract the zip file, go to the /lib
folder and copy all *.jar
files into your /WEB-INF/lib
.
共18个文件,其中batik*.jar
和xml*.jar
不需要单独使用t:inputFileUpload
组件.你可以离开他们.
It are 18 files, of which batik*.jar
and xml*.jar
are unnecessary for using alone the t:inputFileUpload
component. You could leave them away.
在 web.xml
中配置 Tomahawk 扩展过滤器.它负责处理 multipart/form-data
请求,而这些请求是通过 HTTP 发送文件所必需的.
Configure the Tomahawk extensions filter in web.xml
. It's the one who's responsible for handling multipart/form-data
requests which is required to be able to send files over HTTP.
<filter>
<filter-name>MyFacesExtensionsFilter</filter-name>
<filter-class>org.apache.myfaces.webapp.filter.ExtensionsFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>MyFacesExtensionsFilter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
请注意,
必须与 FacesServlet
的
完全匹配在 web.xml
中定义.
Note that the <servlet-name>
must match the exact <servlet-name>
of the FacesServlet
as you've definied in web.xml
.
创建一个简单的 Facelet,upload.xhtml
:
Create a simple Facelet, upload.xhtml
:
<!DOCTYPE html>
<html lang="en"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:t="http://myfaces.apache.org/tomahawk"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<title>Tomahawk file upload demo</title>
</h:head>
<h:body>
<h:form enctype="multipart/form-data">
<t:inputFileUpload value="#{bean.uploadedFile}" />
<h:commandButton value="submit" action="#{bean.submit}" />
<h:messages />
</h:form>
</h:body>
</html>
注意 <h:form>
上的 enctype="multipart/form-data"
属性,这对于能够发送带有HTTP.
Note the enctype="multipart/form-data"
attribute on <h:form>
, this is very important in order to be able to send files with HTTP.
创建一个简单的托管 bean,com.example.Bean
:
Create a simple managed bean, com.example.Bean
:
package com.example;
import java.io.IOException;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import org.apache.commons.io.FilenameUtils;
import org.apache.myfaces.custom.fileupload.UploadedFile;
@ManagedBean
@RequestScoped
public class Bean {
private UploadedFile uploadedFile;
public void submit() throws IOException {
String fileName = FilenameUtils.getName(uploadedFile.getName());
String contentType = uploadedFile.getContentType();
byte[] bytes = uploadedFile.getBytes();
// Now you can save bytes in DB (and also content type?)
FacesContext.getCurrentInstance().addMessage(null,
new FacesMessage(String.format("File '%s' of type '%s' successfully uploaded!", fileName, contentType)));
}
public UploadedFile getUploadedFile() {
return uploadedFile;
}
public void setUploadedFile(UploadedFile uploadedFile) {
this.uploadedFile = uploadedFile;
}
}
应该是这样.通过 http://localhost:8080/projectname/upload.xhtml 打开.
That should be it. Open it by http://localhost:8080/projectname/upload.xhtml.
至于你的具体问题:
我应该怎么做才能将文件从 JSF 传递到托管 bean,然后将其转换为字节 [](以便能够将其处理到 EJB)?
这是上面的回答.
servlet 如何帮助我?
它能够处理和控制 HTTP 请求/响应.在 JSF 环境中,FacesServlet
已经完成了所有工作.
It is able to process and control HTTP requests/responses. In a JSF environment, the FacesServlet
already does all the work.
我需要一个 servlet 来做到这一点吗?
在 JSF 环境中,FacesServlet
是必需的.但是API已经提供了,不用自己写.但是,为了能够从数据库下载文件,另一个 servlet 绝对有用.您可以在此处找到一个基本示例:用于提供静态内容的 Servlet.
In a JSF environment, the FacesServlet
is mandatory. But it's already provided by the API, you don't need to write one yourself. However, to be able to download files from a database, another servlet is definitely useful. You can find a basic example here: Servlet for serving static content.
我还发现在一些博客中提到了一些关于 servlets 3.0 的内容,但我不知道我的工作环境是否在使用它,如果我使用的是 servlets 3.0(我使用的是 JEE6)怎么办?
如果您使用的是 Servlet 3.0 容器,如 Glassfish 3、JBoss AS 6、Tomcat 7 等,并且 web.xml
被声明为 Servlet 3.0,那么您肯定使用的是 Servlet 3.0.Servlet 3.0 是 Java EE 6 的一部分.
If you're using Servlet 3.0 container like Glassfish 3, JBoss AS 6, Tomcat 7, etc and the web.xml
is declared as Servlet 3.0, then you're definitely using Servlet 3.0. Servlet 3.0 is part of Java EE 6.
这篇关于JSF 2.0 文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!